Jump to content

[SOLVED] imagecreatefromjpeg() Memory issue


darknessmdk

Recommended Posts

I have built a multiple file upload for an image gallery, one of the requirements is to resize any images that exceed a width of 500px or height of 400px, My script works great as long as the files are under 1MB after that I get a half displayed page with this error "Allowed memory size of 20971520 bytes exhausted (tried to allocate 12288 bytes)" and it references the line that imagecreatefromjpeg() is on.

I have had my host (hostmysite) raise the memory_limit setting to 20MB, they will go no higher.

 

Any idea why this is an issue with a 1MB jpeg?

 

 

foreach ($_FILES["photofile"]["error"] as $key => $error) {

			if($error == UPLOAD_ERR_OK){

			$logo = $_FILES['photofile']['name'][$key];;

				$logo_ext = strtolower(substr($logo,strrpos($logo,".")));	

			$result = mysql_query("INSERT into gallery(image, caption, section, subsection)VALUES('$image', '$caption', '$section', '$subsection')");

			$id=mysql_insert_id();

			$tmp_name = $_FILES["photofile"]["tmp_name"][$key];

       			$name = $_FILES["photofile"]["name"][$key];
			$random = rand(1, 6);

			//:::File Resize:::\\

			$uploadedfile = $tmp_name;
			$src = imagecreatefromjpeg($uploadedfile);

			// get size of image
			list($width,$height)=getimagesize($uploadedfile);

			if ($width > $height)
			{
			$target_width=500;
			$ratio = $target_width/$width;
			$newwidth = $width * $ratio;
			$newheight = $height * $ratio;
			 }
			else
			{
			$target_height = 400;
			$ratio =  $target_height/$height;
			$newwidth = $width * $ratio;
			$newheight = $height * $ratio;
			}
			$tmp=imagecreatetruecolor($newwidth,$newheight);

			// resize 
			imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

			// save resized
			//$fname = explode('.', $_FILES["photofile"]["name"]);
			//$filename = $fname[0] . "resized" . ".jpg";
			imagejpeg($tmp,$uploadedfile,100); 

			imagedestroy($src);
			imagedestroy($tmp);

			//:::End File Resize:::\\
      			
			move_uploaded_file($uploadedfile, "../gallery/photo".$id."".$random."".$logo_ext."");
			// Sets permissions 644
			chmod("../gallery/photo".$id."".$random."".$logo_ext."", 0644);

			//$image = "propimg/";

			$query2= mysql_query("UPDATE gallery SET image='photo".$id."".$random."". $logo_ext ."' WHERE id = '$id'");

			//$checked = $_POST['type2'];

			//foreach ($checked as $type) {

				//	}
				}
			}

			if($result == TRUE) {

			echo "<span style='message'>Your photos have been uploaded!</span>";

			} else {

			echo "<span style='error'>There was a problem uploading, Try again</span>";

			}

			}else{ 
				echo "";
				}
			?>

Link to comment
https://forums.phpfreaks.com/topic/109352-solved-imagecreatefromjpeg-memory-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.