Jump to content

Archived

This topic is now archived and is closed to further replies.

fezzik

imagedestroy()

Recommended Posts

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:
 
[code]
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);
}
[/code]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

Share this post


Link to post
Share on other sites
$this->_imgFinal needs to be an image created by one of the imagecreateXXXXXX() functions

Share this post


Link to post
Share on other sites
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:

[code]
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;
    }
[/code]


Cheers,

Fezzik

Share this post


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

[code]
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;
    }
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.