Bizty Posted August 11, 2009 Share Posted August 11, 2009 I have a script that uploads and rezises and image aswell as make a thumb of it. But there's an issue, the script is for some reason using over 24mb memory which causes an error (the max script memory allowed is fixed), I cant seem to find the error my self so hopefully you'd be able to. Code: <? function AddImages() { $i = 1; while($i <= 10) { $image_name = "image_" . $i; if (isset ($_FILES[$image_name])){ $imagename = $_FILES[$image_name]['name']; if($imagename == "") { $i++; } else { $source = $_FILES[$image_name]['tmp_name']; $target = "images/gallery/".$imagename; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "images/gallery/" . $imagepath; //This is the new file $file = "images/gallery/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 840; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $imgwidth = $modwidth; $imgheight = $modheight; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $save = "images/gallery/sml_" . $imagepath; //This is the new file $file = "images/gallery/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 80; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; /*MYSQL INSERTION*/ $imgpath = $imagepath; $thumbpath = "sml_". $imagepath; $text_caption = "text_".$i; $caption = $_POST[$text_caption]; if(empty($caption)) { $caption = 'No info'; } mysql_query("INSERT INTO images (imgpath, thumbpath, caption, imgwidth, imgheight) VALUES ('$imgpath', '$thumbpath', '$caption', '$imgwidth', '$imgheight')") or die(mysql_error()); $i++; } } else { print "Error: No image<br />"; $i++; } } header("location: admin.php?p=upload&succes=Alle Billeder blev uploaded."); } ?> Alternativly: http://paste-it.net/public/l3023f6/ Link to comment https://forums.phpfreaks.com/topic/169771-add-images-leak/ Share on other sites More sharing options...
premiso Posted August 11, 2009 Share Posted August 11, 2009 The main issue I see is that you are in a while loop...10 times. This will load 10 images which at even just 2.4MB's a peice you are out of memory. The code could be cleaned up a bit better, such as "clearing" the images after the loop runs each time (setting to null). But all in all you should just up your memory limit. I would Suggest a 32MB or 64MB limit for multiple image uploading. EDIT: AFter re-looking at the code I made a few comments: <?php function AddImages() { $i = 1; while($i <= 10) { $image_name = "image_" . $i; if (isset ($_FILES[$image_name])){ $imagename = $_FILES[$image_name]['name']; if($imagename == "") { $i++; }else { $source = $_FILES[$image_name]['tmp_name']; $target = "images/gallery/".$imagename; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "images/gallery/" . $imagepath; //This is the new file $file = "images/gallery/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 840; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $imgwidth = $modwidth; $imgheight = $modheight; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; // $save is not defined till later....why have this here. imagejpeg($tn, $save, 100) ; $save = "images/gallery/sml_" . $imagepath; //This is the new file $file = "images/gallery/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 80; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; // Why does this seem to be happening twice ? $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; /*MYSQL INSERTION*/ $imgpath = $imagepath; $thumbpath = "sml_". $imagepath; $text_caption = "text_".$i; $caption = $_POST[$text_caption]; if(empty($caption)) { $caption = 'No info'; } mysql_query("INSERT INTO images (imgpath, thumbpath, caption, imgwidth, imgheight) VALUES ('$imgpath', '$thumbpath', '$caption', '$imgwidth', '$imgheight')") or die(mysql_error()); $i++; // clear up the resources $tn = null; $image = null; } } else { print "Error: No image<br />"; $i++; } } header("location: admin.php?p=upload&succes=Alle Billeder blev uploaded."); } Primarily it seems that you have duplicate code. I am not sure if this is correct or if it is indeed duplicate and you need to clean it up and remove the duplicates. Link to comment https://forums.phpfreaks.com/topic/169771-add-images-leak/#findComment-895648 Share on other sites More sharing options...
Bizty Posted August 14, 2009 Author Share Posted August 14, 2009 I'm not strong in the image scripts so in all honesty I didnt make all of this, so there may be duplicate code. I tried cleaning it my self and got a few errors. With regards to upping my memory, I would, but the host it's at has limited to 24mb :/ Link to comment https://forums.phpfreaks.com/topic/169771-add-images-leak/#findComment-898404 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.