伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: imagedestroy

来源:网络转载 浏览:49次 时间:2023-07-13
imageellipse » « imagedashedline PHP 手册 函数参考 图像生成和处理 GD GD 和图像处理 函数

imagedestroy

(PHP 4, PHP 5, PHP 7, PHP 8)

imagedestroy — 销毁一图像

说明

imagedestroy(resource $image): bool

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

add a note

User Contributed Notes 7 notes

up down 49 Docey15 years ago 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.
up down 6 devel at kijote dot com dot ar2 years ago Important note: When the imagedestroy is called, the resource is freed but the printed resource output is the same as before calling the function:

<?php

  $img = imagecreate(1, 1);

  print_r([$img, $img ? 'TRUE': 'FALSE', is_resource($img) ? 'TRUE' : 'FALSE', get_resource_type($img) ?: 'FALSE']);

  /*
  Result:
  Array
  (
    [0] => Resource id #1
    [1] => TRUE
    [2] => TRUE
    [3] => gd

  )
  */

  imagedestroy($img);

  print_r([$img, $img ? 'TRUE': 'FALSE', is_resource($img) ? 'TRUE' : 'FALSE', get_resource_type($img) ?: 'FALSE']);

  /*
  Result:
  Array
  (
    [0] => Resource id #1
    [1] => TRUE
    [2] => FALSE
    [3] => Unknown
  )
  */
?>

As you can see in the above example, the first index in array is TRUE in both cases. So, despite the common thinking you can't trust in something like:

<?php

  if ($img)  {  // it's casted as boolean and returns true even after imagedestroy is called
     // do something
  }

?>

If you need to ensure the availability of a certain resource, you must to use is_resource and get_resource_type functions.
up down 14 Claude D.6 years ago Caution should be observed with imagedestroy(); copying your reference variable over to another variable will cause imagedestroy to destroy both at once.

Eg.:

$a = imagecreate(...);
$b = $a;
imagedestroy($a);

While you'd think $b still contains your image, it doesn't. Both $a and $b are destroyed.
up down 7 dan at mlodecki dot net18 years ago 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. up down 2 webmaster at codefisher dot org14 years ago 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.
up down 5 Andrew Hoffmann - ahoffmann at wisc dot edu16 years ago 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).
up down 4 corpus-deus at softhome dot net12 years ago 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.
add a note

官方地址:https://www.php.net/manual/en/function.imagedestroy.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net