Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.