Hi!
I try to write my thumbnail creator script, no trouble with image load, resize cut, watermark, etc, but memory handling!
File is submitted by simple file form.
function makeLog($logthis) // this is only for tracking mem usage
{
$handler=fopen("log.txt","a");
fwrite($handler,$logthis . " \r\n");
fclose($handler);
}
function handleImgUpload()
{
$fTName=$_FILES["Filedata"]["tmp_name"];
$fName=$_FILES["Filedata"]["name"];
$fSize=$_FILES["Filedata"]["size"];
$fType=$_FILES["Filedata"]["type"];
if(!is_uploaded_file($fTName)) return 1;
makelog("\r\n\r\n\r\nstart:" . memory_get_usage(true)/1024);
$img1=imagecreatefromjpeg($fTName);
makelog("mem usage after image creation:" . memory_get_usage(true)/1024);
imagedestroy($img1);
makelog("mem usage after destory img1:" . memory_get_usage(true)/1024);
}
Content of log.txt after uploading an 1600 x 1200 24 bpp image:
start:768
mem usage after image creation:9984
mem usage after destory img1:768
Seems ok, all used memory freed!
Modify the code:
$img1=imagecreatefromjpeg($fTName);
makelog("mem usage after image creation:" . memory_get_usage(true)/1024);
// new part - create thumbnail
$rImage = imagecreatetruecolor(200,200);
makelog("mem usage after creating rImage" . memory_get_usage(true)/1024);
imagecopyresampled($rImage,$img1,0,0,0,0,200,200,200,200);
makelog("mem usage after imagecopyresampled" . memory_get_usage(true)/1024);
imagedestroy($rImage);
makelog("mem usage after destory rImage:" . memory_get_usage(true)/1024);
// end of new part - create tumbnail
imagedestroy($img1);
makelog("mem usage after destory img1:" . memory_get_usage(true)/1024);
And here comes the interesting part:
Content of log.txt after uploading the same image:
start:768
mem usage after image creation:9984
mem usage after creating rImage10240
mem usage after imagecopyresampled10240
mem usage after destory rImage:10240
mem usage after destory img1:5120
WTF? After destroying $rImage no change? 5 megs used memory after all images destroyed?
Is it me, gd or what?
Sadly this script must run on a server, where memory limit for scripts is low, and the final task is multiple upload at the same time, so i cant afford 5 megs for nothing.
(Apache 2.2.11 / PHP 5.2.9-2, Op. system: Win XP-64 bit (X64))
Thanks for any answers, and apologise for my english!