imagedestroy() - gd函数(图像处理)
imagedestroy()
(PHP 4, PHP 5, PHP 7)
销毁一图像
说明
imagedestroy(resource $image): boolimagedestroy()释放与$image关联的内存。$image是由图像创建函数返回的图像标识符,例如imagecreatetruecolor()。
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.
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.
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.
In theory creating an image object and calling imagedestroy in your destructor should be a good way of doing it; something like: 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.
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.
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).
Important note: When the imagedestroy is called, the resource is freed but the printed resource output is the same as before calling the function: 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:
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)