Slytherin
Slytherin

Reputation: 474

PHP JPEG resampling

i have this script to take original picture , resample it twice , for thumbnail and preview.This script works fine , even though You may find some weaknesses of syntactic fashion , im sure.The script as it is , is not subject of my question. I am wondering whether i am supposed to somehow clear memory afterwards.Am i flooding my server with data ? or is this fine and clears itself afterwards.Iam asking because this script will handle my gallery , and it is expected to handle multiple files at once.

script is written like this :

 $filename = $DumpHere.$Processed;

// Get new dimensions
 list($width, $height) = getimagesize($filename);
// Resample thumbnail
 $image_p = imagecreatetruecolor(70, 70);
 $image = imagecreatefromjpeg($filename);
 imagecopyresampled($image_p, $image, 0, 0, 0, 0, 70, 70, $width, $height);
// Output Thumbnail
 imagejpeg($image_p, $ThumbsFolder.'thumb_'.$Processed, 100);
// Resample preview
 $image_p = imagecreatetruecolor(500, 300);
 $image = imagecreatefromjpeg($filename);
 imagecopyresampled($image_p, $image, 0, 0, 0, 0, 500, 300, $width, $height);
// Output Preview
 imagejpeg($image_p, $PreviewFolder.'preview_'.$Processed, 100);

just to be clear

$DumpHere

is path to a folder containing original files before processing.Thanks for any help.

Upvotes: 1

Views: 756

Answers (1)

Landon
Landon

Reputation: 4108

You want to use imagedestroy() on your resources, so just add:

imagedestroy($image_p);
imagedestroy($image);

At the end and that will free up the memory. PHP is pretty good about getting rid of memory on it's own. For example, once your script ends, all the memory is freed up. But this is the method to explicitly return those resources to the system.

Upvotes: 1

Related Questions