[solved] OOP GD and Method problems [thanks]
Posted: Mon Oct 22, 2007 3:00 pm
Hi i am re-coding a image uploader & resizer script in OOP now this is one of my first OOP projects and i need some help. There are 2 functions\methods called manipulate one of them is commented and is calling on 2 other methods within the same class but i dont know how to do that SO i compiled all of the methods that i wanted to call, into the other manipulate method. Now thats my Method problem done, the problem i have with OOP and GD is that i get a massssive string of errors when i try to upload a file. The errors that i get lead me to believe i cannot use the gd library correctly from within the Class.
oop_imgfunc.php
upload files
upload2.html
upload2.php
oop_imgfunc.php
Code: Select all
<?
define("IMAGE_DIRECTORY", "images/");
class image {
private $strImage_Name; // Object Name
private $strFilesName; // $_FILES['"$strFilesName"']['tmp_name']
private $strImage_Type; // Image File Type
private $strExtension; // .jpg, .gif, .png
private $intImage_Width; // Original Image Width
private $intImage_Height; // Original Image Height
private $intMax_Width; // Max Final Image Width
private $intMax_Height; // Max Final Image height
private $intMultiplier; // The amount the image will be scaled to.
private $intWidth; // The True Final Image Width
private $intHeight; // The True Final Image Height
private $imgSource; // The Image (original)
private $imgResized; // The Image (Resized)
function image($strFilesName) {
$this->strFilesName = $strFilesName;
$imgTemp = $_FILES[$this->strFilesName]['tmp_name'];
$imgType = $_FILES[$this->strFilesName]['type'];
$this->strImage_Type = $imgType;
switch ($this->strImage_Type) {
default:
die('something didnt work location -> '.$imgTemp.' file type -> '.$imgType);
break;
case 'image/gif':
$this->imgSource = imagecreatefromgif($imgTemp);
$this->strExtension = '.gif';
break;
case 'image/jpeg':
$this->imgSource = imagecreatefromjpeg($imgTemp);
$this->strExtension = '.jpg';
break;
case 'image/png':
$this->imgSource = imagecreatefrompng($imgTemp);
$this->strExtension = '.png';
break;
}
$this->intImage_Width = imagesx($this->imgSource); // gets images width and height (x and y)
$this->intImage_Height = imagesy($this->imgSource); // gets images width and height (x and y)
} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Find_Dimensions($intMax_Width, $intMax_Height) {
if (($intMax_Width != 0) && ($intMax_Height != 0)) { // checks the max x, y are both not 0
if ((($this->intImage_Width > $intMax_Width)) || ($this->intImage_Height > $intMax_Height)) { // either or both must need scaling down.
$intWidth_Multiplier =($this->intImage_Width > $intMax_Width) ? round((1 - (($this->intImage_Width - $intMax_Width) / $this->intImage_Width)), 5) : 1 ; // gets scale multiplyer
$intHeight_Multiplier =($this->intImage_Height > $intMax_Height) ? round((1 - (($this->intImage_Height - $intMax_Height) / $this->intImage_Height)), 5) : 1 ; // gets scale multiplyer
$this->intMultiplier =($intWidth_Multiplier < $intHeight_Multiplier) ? $intWidth_Multiplier : $intHeight_Multiplier ; // chooses the smallest scale multiplyer
}
}
if (empty($this->intMultiplier)) $this->intMultiplier = 1; //shouldnt need this line
$this->intWidth = round($this->intImage_Width * $this->intMultiplier, 0);//finds the new width
$this->intHeight = round($this->intImage_Height * $this->intMultiplier, 0);//finds the new height
} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Manipulate($intMax_Width, $intMax_Height) {
if (($intMax_Width != 0) && ($intMax_Height != 0)) { // checks the max x, y are both not 0
if ((($this->intImage_Width > $intMax_Width)) || ($this->intImage_Height > $intMax_Height)) { // either or both must need scaling down.
$intWidth_Multiplier =($this->intImage_Width > $intMax_Width) ? round((1 - (($this->intImage_Width - $intMax_Width) / $this->intImage_Width)), 5) : 1 ; // gets scale multiplyer
$intHeight_Multiplier =($this->intImage_Height > $intMax_Height) ? round((1 - (($this->intImage_Height - $intMax_Height) / $this->intImage_Height)), 5) : 1 ; // gets scale multiplyer
$this->intMultiplier =($intWidth_Multiplier < $intHeight_Multiplier) ? $intWidth_Multiplier : $intHeight_Multiplier ; // chooses the smallest scale multiplyer
}
}
if (empty($this->intMultiplier)) $this->intMultiplier = 1; //shouldnt need this line
$this->intWidth = round($this->intImage_Width * $this->intMultiplier, 0);//finds the new width
$this->intHeight = round($this->intImage_Height * $this->intMultiplier, 0);//finds the new height
if (($this->strImage_Type == 'image/gif') && (imagecolortransparent($this->imgSource) != (-1))) {
$tmpvar1 = true;
$imgDestination_Temp1 = imagecreate($this->intWidth, $this->intHeight);
$imgDestination_Temp2 = imagecreate($this->intWidth, $this->intHeight);
$clrBlack = imagecolorallocate($imgDestination_Temp1, 0, 0, 0);
$clrWhite = imagecolorallocate($imgDestination_Temp2, 255, 255, 255);
imagefill($imgDestination_Temp1, 0, 0, $clrBlack);
imagefill($imgDestination_Temp2, 0, 0, $clrWhite);
imagecopyresampled($imgDestination_Temp1, $this->imgSource, 0, 0, 0, 0, $this->intWidth, $this->intHeight, $this->intImage_Width, $this->intImage_Height);
imagecopyresampled($imgDestination_Temp2, $this->imgSource, 0, 0, 0, 0, $this->intWidth, $this->intHeight, $this->intImage_Width, $this->intImage_Height);
$arrTransX = array();
$arrTransY = array();
for($i=0; $i<$this->intWidth; $i++)
{
for($j=0; $j<$this->intHeight; $j++)
{
$intIndex_Temp1 = imagecolorat($imgDestination_Temp1, $i, $j);
$arrColor_Temp1 = imagecolorsforindex($imgDestination_Temp1, $intIndex_Temp1);
$intIndex_Temp2 = imagecolorat($imgDestination_Temp2, $i, $j);
$arrColor_Temp2 = imagecolorsforindex($imgDestination_Temp2, $intIndex_Temp2);
if (($arrColor_Temp1['red'] != $arrColor_Temp2['red']) || ($arrColor_Temp1['green'] != $arrColor_Temp2['green']) || ($arrColor_Temp1['blue'] != $arrColor_Temp2['blue']))
{
$arrTransX[count($arrTransX)] = $i;
$arrTransY[count($arrTransY)] = $j;
}
}
}
imagedestroy($imgDestination_Temp1);
imagedestroy($imgDestination_Temp2);
$imgDestination = imagecreatetruecolor($intWidth, $intHeight);
$clrWhite = imagecolorallocate($imgDestination, 255, 255, 255);
imagefill($imgDestination, 0, 0, $clrWhite);
//$intImage_Width = imagesx($imgSource);
//$intImage_Height = imagesy($imgSource);
imagecopyresampled($imgDestination, $this->imgSource, 0, 0, 0, 0, $this->intWidth, $this->intHeight, $this->intImage_Width, $this->intImage_Height);
imagetruecolortopalette($imgDestination, true, 256);
$clrBlack = imagecolorallocate($imgDestination, 0, 0, 0);
for($i=0; $i<count($arrTransX); $i++)
{
imagesetpixel($imgDestination, $arrTransX[$i], $arrTransY[$i], $clrBlack);
}
$clrTransparent = imagecolorat($imgDestination, $arrTransX[0], $arrTransY[0]);
imagecolortransparent($imgDestination, $clrTransparent);
$this->imgResized = $imgDestination;
}
if ($tmpvar) {
$imgDestination = imagecreatetruecolor($this->intWidth, $this->intHeight);
imagealphablending($imgDestination, false);
imagesavealpha($imgDestination, true);
imagecopyresampled($imgDestination, $this->imgSource, 0, 0, 0, 0, $this->intWidth, $this->intHeight, $this->intImage_Width, $this->intImage_Height);
$this->imgResized = $imgDestination;
}
} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*function Manipulate($intMax_Width, $intMax_Height) {
Find_Dimensions($intMax_Width, $intMax_Height);
Resize();
} //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */ // Used for testing purposes. (errors)
function Resize() {
if ($this->strImage_Type == 'image/gif') {
$this->imgResized =(imagecolortransparent($this->imgSource) != (-1)) ? Gif_Transparent_Resize() : Normal_Resize() ;
} else {
$this->imgResized = Normal_Resize();
}
} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Normal_Resize() {
$imgDestination = imagecreatetruecolor($this->intWidth, $this->intHeight);
imagealphablending($imgDestination, false);
imagesavealpha($imgDestination, true);
imagecopyresampled($imgDestination, $this->imgSource, 0, 0, 0, 0, $this->intWidth, $this->intHeight, $this->intImage_Width, $this->intImage_Height);
return $imgDestination;
} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Gif_Transparent_Resize() {
$imgDestination_Temp1 = imagecreate($this->intWidth, $this->intHeight);
$imgDestination_Temp2 = imagecreate($this->intWidth, $this->intHeight);
$clrBlack = imagecolorallocate($imgDestination_Temp1, 0, 0, 0);
$clrWhite = imagecolorallocate($imgDestination_Temp2, 255, 255, 255);
imagefill($imgDestination_Temp1, 0, 0, $clrBlack);
imagefill($imgDestination_Temp2, 0, 0, $clrWhite);
imagecopyresampled($imgDestination_Temp1, $this->imgSource, 0, 0, 0, 0, $this->intWidth, $this->intHeight, $this->intImage_Width, $this->intImage_Height);
imagecopyresampled($imgDestination_Temp2, $this->imgSource, 0, 0, 0, 0, $this->intWidth, $this->intHeight, $this->intImage_Width, $this->intImage_Height);
$arrTransX = array();
$arrTransY = array();
for($i=0; $i<$this->intWidth; $i++)
{
for($j=0; $j<$this->intHeight; $j++)
{
$intIndex_Temp1 = imagecolorat($imgDestination_Temp1, $i, $j);
$arrColor_Temp1 = imagecolorsforindex($imgDestination_Temp1, $intIndex_Temp1);
$intIndex_Temp2 = imagecolorat($imgDestination_Temp2, $i, $j);
$arrColor_Temp2 = imagecolorsforindex($imgDestination_Temp2, $intIndex_Temp2);
if (($arrColor_Temp1['red'] != $arrColor_Temp2['red']) || ($arrColor_Temp1['green'] != $arrColor_Temp2['green']) || ($arrColor_Temp1['blue'] != $arrColor_Temp2['blue']))
{
$arrTransX[count($arrTransX)] = $i;
$arrTransY[count($arrTransY)] = $j;
}
}
}
imagedestroy($imgDestination_Temp1);
imagedestroy($imgDestination_Temp2);
$imgDestination = imagecreatetruecolor($intWidth, $intHeight);
$clrWhite = imagecolorallocate($imgDestination, 255, 255, 255);
imagefill($imgDestination, 0, 0, $clrWhite);
//$intImage_Width = imagesx($imgSource);
//$intImage_Height = imagesy($imgSource);
imagecopyresampled($imgDestination, $this->imgSource, 0, 0, 0, 0, $this->intWidth, $this->intHeight, $this->intImage_Width, $this->intImage_Height);
imagetruecolortopalette($imgDestination, true, 256);
$clrBlack = imagecolorallocate($imgDestination, 0, 0, 0);
for($i=0; $i<count($arrTransX); $i++)
{
imagesetpixel($imgDestination, $arrTransX[$i], $arrTransY[$i], $clrBlack);
}
$clrTransparent = imagecolorat($imgDestination, $arrTransX[0], $arrTransY[0]);
imagecolortransparent($imgDestination, $clrTransparent);
return $imgDestination;
} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Output($strFilename) {
switch ($this->strImage_Type) {
case 'image/gif': imagegif ($this->imgResized, IMAGE_DIRECTORY.strFilename.'.gif'); break;
case 'image/jpeg': imagejpeg($this->imgResized, IMAGE_DIRECTORY.strFilename.'.jpg'); break;
case 'image/png': imagepng ($this->imgResized, IMAGE_DIRECTORY.strFilename.'.png'); break;
}
imagedestroy($this->imgResized);
} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function finish() {
imagedestroy($imgSource);
} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} // end of class ////////////////////////////////////////////////////////////////////////////////////////////////////////
?>upload2.html
Code: Select all
<form enctype="multipart/form-data" action="uploader2.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>Code: Select all
<?
include('oop_imgfunc.php');
$image = new image('uploadedfile');
$image->Manipulate(250, 250); //sets up the image height and width for output
$image->Output('test'); //name the file
$image->Manipulate(400, 400);
$image->Output('test2');
$image->Finish();
?>