GD and Image 函数
在线手册:中文 英文
PHP手册

imagedestroy

(PHP 4, PHP 5)

imagedestroy销毁一图像

说明

bool imagedestroy ( resource $image )

imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,例如 imagecreatetruecolor()


GD and Image 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 销毁一图像

用户评论:

newman dot imotion at gmail dot com (03-Nov-2011 02:11)

Using PHP 5.3.8., when passing an image to a function for processing and then using imagedestroy() on the passed image inside the function will result in the original image being destroyed!

Make very sure you are completely done with an image before using imagedestroy().

Try this simple code:

<?php

$filename
= "http://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Golfball.jpg/260px-Golfball.jpg";
list(
$width, $height) = getimagesize($filename);
$sourceIMAGE = imagecreatefromjpeg($filename);

imagejpeg($sourceIMAGE, 'test_destroy_1.JPG', 100); //writes source image to file as JPEG
imagejpeg(iconifyIMAGE($sourceIMAGE, $width, $height), 'test_destroy_2.JPG', 100); //writes iconified image to file as JPEG
imagejpeg($sourceIMAGE, 'test_destroy_3.JPG', 100); //<--- this will fail because imagedestroy() was called in the function iconifyIMAGE()

function iconifyIMAGE($srcI, $srcX, $srcY)
{
   
$iconIMAGE = imagecreatetruecolor(20, 20);
   
imagecopyresampled($iconIMAGE, $srcI, 0, 0, 0, 0, 20, 20, $srcX, $srcY);
   
imagedestroy($srcI); //<--- this also destroys $sourceIMAGE
   
return ($iconIMAGE);
}
?>

The code returns the following error: Warning: imagejpeg(): 6 is not a valid Image resource in C:\xampp\htdocs\bitmap_tester_destroy.php on line 9

Line 9 is... imagejpeg($sourceIMAGE, 'test_destroy_3.JPG', 100);

corpus-deus at softhome dot net (13-Feb-2010 02:33)

In theory creating an image object and calling imagedestroy in your destructor should be a good way of doing it; something like:

<?php
final class My_Image() {

  private
$img;

  public function
__construct() {
   
$this->img = imagecreatetruecolor();
   
// ... other stuff ...
 
}

  public function
__destruct() {
    if(
is_resource($this->img)) {
     
imagedestroy($this->img);
    }
  }

 
// ... other methods...

}
?>

I check that $this->img is a resource in case imagecreatetruecolor() fails or you null the value somewhere down the line from a method call meaning that $this->img is NOT a resource, in which case imagedestroy is an unecessary function call that just fails with a warning message.

webmaster at codefisher dot org (06-Oct-2007 06:26)

And to continue what Docey said, if php did not destroy all resources when the script stopped it would be a huge memory leak and everyone would be crying out for it to be fixed right away.

I have been using this function during a script that was breaking an image made of many little icons into the little parts, which could mean 400+ images in the one script, which was using a lot of memory so I needed to destroy them.

Docey (20-Oct-2006 06:59)

When the script stop PHP will automatic destory ANY
resources, this goes for aswell for images, thus in the
case the use clicks the stop button, php will automatic
clear the resource.

thus imagedestroy is used to clear the memory BEFORE
the script ends. this is usefull to keep memory usage
DURING the script to an acceptable level.

hope this clear somethings up.

roland at more-pc dot nl (25-Sep-2005 02:31)

As Andrew states below, it's IMPERATIVE that you use the imagedestroy() function. But your script may abort before it reaches the call to imagedestroy() (for example the user may click the browsers 'stop' button). In this case the default PHP behaviour is to abort the script, never reaching your call to imagedestroy().
One way of doing it (the one which works very well for me) is with register_shutdown_function():
<?
function shutdown_func() {
    global $img;
    if($img)
        imagedestroy($img);
}
register_shutdown_function("shutdown_func");
$img = imagecreate...(...);
... // manipulate image
// normally I'd need the following line, but now it's handled by shutdown_func()
// imagedestroy($img);
?>

Andrew Hoffmann - ahoffmann at wisc dot edu (23-Jun-2005 03:25)

When working with a lot of high-resolution images, it's IMPERATIVE that you use the imagedestroy() function.

In my scenario, I was taking two high resolution desktop wallpapers and shrinking them down into successively smaller ones (preventing the user from having to upload a dozen files).

At first, my script would run, then just stop.  I realized later that I had not destroyed the source image and the newly resized image in memory after I had finished writing the file out to disk.  Thus, I quickly reached the memory limit that my hosting provider placed in their php.ini file.

Reusing an image variable does NOT clear the old data out of memory!  You must use imagedestroy() to clear the data out.  (I don't know if unset() works as well).

Also note that the image data in memory is raw, so don't base how much memory you are using based on the original filesize of the compressed image (such as jpeg or png).

dan at mlodecki dot net (05-Mar-2004 12:42)

I have noticed that gd drawing functions can behave oddly if there is a previous image which has not been imagedestroy()'ed.  You should always imagedestroy when you are done with an image object.