Jump to content


Photo

imagedestroy()


  • Please log in to reply
4 replies to this topic

#1 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 13 October 2006 - 10:40 PM

I'm working with a script called Crop Canvas which you can dl at http://php.amnuts.com/. Unfortunately the forums associated with this script are not available due to the creator not having adequate time to respond. I was hoping someone in this community has experience with this script or is knowledgable enough to help with this issue.

I've created a simple form that allows users to upload images and save them to the server. Once an image is saved to the server I run the Crop Canvas script to create a 75x75 pixel thumbnail of the image. The lines of code I'm using are as follows:
 
require('class.cropcanvas.php');
$cc =& new CropCanvas();

if ($cc->loadImage("../uploads/".$rand_name."_t.".$file_ext)) {
  $cc->cropToSize('75', '75', ccCENTER);
  $cc->saveImage("../uploads/".$rand_name."_sq.".$file_ext);
  $cc->flushImages(true);
}
llowing url:

The above lines check to see if a image exists then run it through the Crop Canvas script. I'm encountering an error when I try to upload images. The error I recieve is:

Warning: imagedestroy(): supplied argument is not a valid Image resource in class.cropcanvas.php on line 436

Line 436 is contained in the following function:

function flushImages($original = true)
{
  imagedestroy($this->_imgFinal); // line 436
  $this->_imgFinal = null;
  if ($original) {
    imagedestroy($this->_imgOrig);
  $this->_imgOrig = null;
  }
}

I've read the information at http://us3.php.net/imagedestroy but I do not completely understand why I'm encountering this error. The error seems to be associated with specific images I'm tring to upload.

Any help is much appreciated. If you need additional information please ask.

Best,

Fezzik

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 October 2006 - 11:09 PM

$this->_imgFinal needs to be an image created by one of the imagecreateXXXXXX() functions
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 13 October 2006 - 11:50 PM

Thanks for posting! Can you explain further? I don't quite understand your post. This issue occurs with random images. I have one image in a group that recieves this error every time, while the remaining group's images do not. saveImage() creates a .jpg using imagejpeg and .png is created using imagepng(). So shouldn't the line:

imagedestroy($this->_imgFinal);

be ok? All the images I'm uploading are .jpg

The function for saveImage() is:

function saveImage($filename, $quality = 90, $forcetype = '')
    {
        if ($this->_imgFinal == null) {
            $this->_debug('There is no cropped image to save.');
            return false;
        }

        $ext  = ($forcetype == '') ? $this->_getExtension($filename) : strtolower($forcetype);
        $func = 'image' . ($ext == 'jpg' ? 'jpeg' : $ext);
        if (!$this->_isSupported($filename, $ext, $func, true)) {
            return false;
        }

        $saved = false;
        switch($ext) {
            case 'gif':
                if ($this->gdInfo['Truecolor Support'] && imageistruecolor($this->_imgFinal)) {
                    imagetruecolortopalette($this->_imgFinal, false, 255);
                }
            case 'png':
                $saved = $func($this->_imgFinal, $filename);
                break;
            case 'jpg':
                $saved = $func($this->_imgFinal, $filename, $quality);
                break;
        }

        if ($saved == false) {
            $this->_debug("The image could not be saved to the '$filename' file as the file type '$ext' using the '$func' function.");
            return false;
        }

        return true;
    }


Cheers,

Fezzik

#4 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 14 October 2006 - 12:20 AM

My last post may have been incorrect about the saveImage() function using an imagecreatexxxxx function. The cropToSize() function does use a imagecreatexxxx function though:

function cropToSize($x, $y, $position = ccCENTRE)
    {
        return ($this->_cropSize(-1, -1, ($x <= 0 ? 1 : $x), ($y <= 0 ? 1 : $y), $position));
    }

function _cropSize($ox, $oy, $nx, $ny, $position)
    {
        if ($this->_imgOrig == null) {
            $this->_debug('The original image has not been loaded.');
            return false;
        }
        if (($nx <= 0) || ($ny <= 0)) {
            $this->_debug('The image could not be cropped because the size given is not valid.');
            return false;
        }
        if (($nx > imagesx($this->_imgOrig)) || ($ny > imagesy($this->_imgOrig))) {
            $this->_debug('The image could not be cropped because the size given is larger than the original image.');
            return false;
        }
        if ($ox == -1 || $oy == -1) {
            list($ox, $oy) = $this->_getCopyPosition($nx, $ny, $position);
        }
        if ($this->gdInfo['Truecolor Support']) {
            $this->_imgFinal = imagecreatetruecolor($nx, $ny);
            imagecopyresampled($this->_imgFinal, $this->_imgOrig, 0, 0, $ox, $oy, $nx, $ny, $nx, $ny);
        } else {
            $this->_imgFinal = imagecreate($nx, $ny);
            imagecopyresized($this->_imgFinal, $this->_imgOrig, 0, 0, $ox, $oy, $nx, $ny, $nx, $ny);
        }
        return true;
    }


#5 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 14 October 2006 - 12:46 AM

I think I have remedied this issue. It seems that imagecreatexxxx function was not being called due to the _imgFinal dimensions being larger than the _imgOrg dimensions. I noticed that some of the thumbnail images I was cropping were less than 75 pixels on one axis. The _cropSize function validates this:

if (($nx > imagesx($this->_imgOrig)) || ($ny > imagesy($this->_imgOrig))) {
  $this->_debug('The image could not be cropped because the size given is larger than the original image.');
  return false;
}

Unfortunately I did not have the script printing errors. Thanks to those that posted. Sorry to waste your time.

Cheers,

Fezzik




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users