Page 1 of 1

[solved] OOP GD and Method problems [thanks]

Posted: Mon Oct 22, 2007 3:00 pm
by Rhys.mataira
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

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 ////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
upload files

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>
upload2.php

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();
?>

Posted: Mon Oct 22, 2007 4:10 pm
by neophyte
What errors do you see? Can you post those?

Posted: Mon Oct 22, 2007 6:27 pm
by Rhys.mataira
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 102

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 103

Warning: imagefill(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 104

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 107

Warning: imagetruecolortopalette(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 108

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 109

Warning: imagecolorat(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 116

Warning: imagecolortransparent(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 117

Warning: imagegif(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 206

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 210

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 102

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 103

Warning: imagefill(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 104

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 107

Warning: imagetruecolortopalette(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 108

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 109

Warning: imagecolorat(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 116

Warning: imagecolortransparent(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 117

Warning: imagegif(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 206

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 210

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 214

Posted: Mon Oct 22, 2007 6:32 pm
by feyd
I suspect $intWidth and $intHeight are not defined. I believe they are supposed to be $this->intWidth etc....

Posted: Mon Oct 22, 2007 6:36 pm
by Rhys.mataira
They are defined at the start of my class heres a list i think its correct...

Code: Select all

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)

...
} // full script is at start of convosation

Posted: Mon Oct 22, 2007 6:37 pm
by feyd
Those specific variables are never set. Look carefully. "$this->" is not prepended to them.

Posted: Mon Oct 22, 2007 6:58 pm
by Rhys.mataira
After a quick search for $intWidth and $intHeight i found

2 accounts of this

Code: Select all

$imgDestination = imagecreatetruecolor($intWidth, $intHeight);
corrected them to this

Code: Select all

$imgDestination = imagecreatetruecolor($this->intWidth, $this->intHeight);
Once i did this it corrected that massive page of errors, altho i still have more to work through ill come back later if i cant work em out.

Here is the errors anyway.

Code: Select all

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 207

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 210

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 207

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 210

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Scripts\Image uploader\oop_imgfunc.php on line 214
only happens on 9 / 10 images.

Thank You greatly :)

Posted: Mon Oct 22, 2007 7:08 pm
by feyd
$tmpvar is never set therefore the call to manipulate fails to set $this->imgResized properly.

Also, you don't have a function Normal_Resize(). You have a method, but that isn't being called from Resize(). ;)

Posted: Mon Oct 22, 2007 7:16 pm
by Rhys.mataira
Good pointer but im no longer using that method. I am now using the previously commented manipulate method as follows

Code: Select all

function Manipulate($intMax_Width, $intMax_Height) {
		$this->Find_Dimensions($intMax_Width, $intMax_Height);
		$this->Resize();
	}
Also i have fix up my methods because previously i didn't know how to call upon a method from another method within the same class ($this->method();) so now my resize is properly directing to Normal_Resize.