Image Destroy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I know this will be really simple, but I'm struggling to get my head round
the use of imagedestroy()
I have some code which uploads an image, resizes to create a smaller image
and thumbnail then deletes the source image.
My question is which images need to be destroyed?
I've read the even reusing a variable name still keeps the old one in
memory.

Here is the code...


$image1 = basename($_FILES['image1']['name']);
$image2 = basename($_FILES['image2']['name']);
$image3 = basename($_FILES['image3']['name']);

$uploaddir = '/home/public_html/images/upload/';
$uploadfile1 = $uploaddir . basename($_FILES['image1']['name']);
$uploadfile2 = $uploaddir . basename($_FILES['image2']['name']);
$uploadfile3 = $uploaddir . basename($_FILES['image3']['name']);


// echo '<pre>';
if (move_uploaded_file($_FILES['image1']['tmp_name'], $uploadfile1)) {
  echo "Image1 is valid, and was successfully uploaded.\n";
} else {
  echo "Possible file upload attack!\n";
  echo "<BR>";
}
// echo '<pre>';
if (move_uploaded_file($_FILES['image2']['tmp_name'], $uploadfile2)) {
  echo "Image2 is valid, and was successfully uploaded.\n";
} else {
  echo "Possible file upload attack!\n";
  echo "<BR>";
}
// echo '<pre>';
if (move_uploaded_file($_FILES['image3']['tmp_name'], $uploadfile3)) {
  echo "Image3 is valid, and was successfully uploaded.\n";
} else {
  echo "Possible file upload attack!\n";
  echo "<BR>";
}
echo "Car inserted into database";

// Now Resize Images


/* resizeToFile resizes a picture and writes it to the harddisk
*
* $sourcefile = the filename of the picture that is going to be resized
* $dest_x   = X-Size of the target picture in pixels
* $dest_y   = Y-Size of the target picture in pixels
* $targetfile = The name under which the resized picture will be stored
* $jpegqual   = The Compression-Rate that is to be used
*/
function resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile,
$jpegqual)
{

/* Get the dimensions of the source picture */
$picsize=getimagesize("$sourcefile");
$source_x  = $picsize[0];
$source_y  = $picsize[1];
$source_id = imageCreateFromJPEG("$sourcefile");
/* Create a new image object (not neccessarily true colour) */

$target_id=imagecreatetruecolor($dest_x, $dest_y);
/* Resize the original picture and copy it into the just created image
 object. Because of the lack of space I had to wrap the parameters to
 several lines. I recommend putting them in one line in order keep your
 code clean and readable */

$target_pic=imagecopyresampled($target_id,$source_id,
                             0,0,0,0,
                             $dest_x,$dest_y,
                             $source_x,$source_y);
/* Create a jpeg with the quality of "$jpegqual" out of the
 image object "$target_pic".
 This will be saved as $targetfile */

imagejpeg ($target_id,"$targetfile",$jpegqual);
return true;
}

//Set Quality to Max
$jpegqual = '100';

//Resize Main Image
$sourcefile = $uploadfile1;
$targetfile = $uploaddir . 'main_' . basename($_FILES['image1']['name']);
$dest_x = '570';
$dest_y = '428';
resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual);
$sourcefile = $uploadfile2;
$targetfile = $uploaddir . 'main_' . basename($_FILES['image2']['name']);
$dest_x = '570';
$dest_y = '428';
resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual);
$sourcefile = $uploadfile3;
$targetfile = $uploaddir . 'main_' . basename($_FILES['image3']['name']);
$dest_x = '570';
$dest_y = '428';
resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual);

//Create Thumbnails
$sourcefile = $uploadfile1;
$targetfile = $uploaddir . 'thumb_' . basename($_FILES['image1']['name']);
$dest_x = '120';
$dest_y = '90';
resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual);
$sourcefile = $uploadfile2;
$targetfile = $uploaddir . 'thumb_' . basename($_FILES['image2']['name']);
$dest_x = '120';
$dest_y = '90';
resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual);
$sourcefile = $uploadfile3;
$targetfile = $uploaddir . 'thumb_' . basename($_FILES['image3']['name']);
$dest_x = '120';
$dest_y = '90';
resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual);

//Delete Uploaded Source Files as no longer required
if(file_exists($uploadfile1))
unlink($uploadfile1);
if(file_exists($uploadfile2))
unlink($uploadfile2);
if(file_exists($uploadfile3))
unlink($uploadfile3);



Should I be using imagedestroy($sourcefile) each time I create an image or
imagedestroy($targetfile) or both?

Many thanks in advance.


Tom

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux