note 106385 added to function.imagedestroy

From: Date: Thu, 03 Nov 2011 14:11:26 +0000
Subject: note 106385 added to function.imagedestroy
Groups: php.notes 
Request: Send a blank email to php-notes+get-184281@lists.php.net to get a copy of this message
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); ---- Server IP: 69.147.83.197 Probable Submitter: 41.151.71.70 ---- Manual Page -- http://www.php.net/manual/en/function.imagedestroy.php Edit -- https://master.php.net/note/edit/106385 Del: integrated -- https://master.php.net/note/delete/106385/integrated Del: useless -- https://master.php.net/note/delete/106385/useless Del: bad code -- https://master.php.net/note/delete/106385/bad+code Del: spam -- https://master.php.net/note/delete/106385/spam Del: non-english -- https://master.php.net/note/delete/106385/non-english Del: in docs -- https://master.php.net/note/delete/106385/in+docs Del: other reasons-- https://master.php.net/note/delete/106385 Reject -- https://master.php.net/note/reject/106385 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184281) next »