0

I wrote a script that uploads and resizes an image image, it used to work fine but the last 2 days i get an Internal Server Error. This happens to bigger images "7 mb", the smaller images like an 1 mb will work just fine. I tried to remove the scaling factor so the image won't be resize and it still won't work. I hosting on iPage and the script used to work just fine for all image sizes. Is it an error from the server size or it is an script error ? Any tips for optimising the code.

Thank you all

<?php
$id = $_POST["id"];
$page = $_POST["page"];

$target_dir = "";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadedfile = $_FILES['fileToUpload']['tmp_name'];

$src = imagecreatefromjpeg($uploadedfile);

list($width,$height)=getimagesize($uploadedfile);

$divide = 1010/$width;
$newwidth=$width*$divide;
$newheight=$height*$divide;
$tmp=imagecreatetruecolor($newwidth,$newheight);

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

$filename = $_FILES['fileToUpload']['name'];
imagejpeg($tmp,'../presentation/'.$target_file,100);

imagedestroy($src);
imagedestroy($tmp);
?>

error code :

20150218T075818: mpooutlet.com/scripts/uploadimg.php PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 22464 bytes) in /hermes/bosnaweb04a/b2659/ipg.mpooutletcom/scripts/uploadimg.php on line 13

Shawn
  • 228
  • 1
  • 4
  • 19
  • did you change php.ini configuration of max file upload setting? – coDe murDerer Feb 19 '15 at 11:19
  • no i have not, i cleaned the database, but this version does not save the link in it so it can't be that – Shawn Feb 19 '15 at 11:20
  • ok.. check this http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size – coDe murDerer Feb 19 '15 at 11:22
  • will try it out and report it back. but why would this be changed ? It used to work and now it doesn't. Maybe there was an server wide update. Thank you – Shawn Feb 19 '15 at 11:25
  • tried changing some parameters, still there were fairly good, 20M max upload, 100M max uploadfilesize ,100M memory limit, 100 working time. Still i raised them to 30M,200M,200M and 900 and it still won't work. Maybe the server will take some time to apply the changes. Will try in 5 minutes. – Shawn Feb 19 '15 at 11:47
  • a guide if anyone needs how to change php.ini on ipage http://andrewapeterson.com/2011/02/ipages-secret-php-ini-editor?track=facebucketchallenge – Shawn Feb 19 '15 at 11:48
  • this is the error code it looks like the alocation is to small what variable does change it in the php.ini 20150218T075818: mpooutlet.com/scripts/uploadimg.php PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 22464 bytes) in /hermes/bosnaweb04a/b2659/ipg.mpooutletcom/scripts/uploadimg.php on line 13 – Shawn Feb 19 '15 at 11:59

1 Answers1

1

First, turn on error reporting, so that php tells you what the problem is:

error_reporting(E_ALL);
ini_set("display_errors", 1); 

I assume the upload works. But there is likely a memory limitation. The bigger the image resolution is, the more memory is consumed by imagecreatetruecolor and imagecopyresampled. Try the following:

ini_set( 'memory_limit', '255M');

Notice: 255M is just a wild guess, depending on the size of source and target image the consumed memory can be much higher.

To have a more permanent solution, I use a variation of this function posted on php.net in my projects: http://php.net/manual/en/function.imagecreatefromjpeg.php#64155

Mario A
  • 3,286
  • 1
  • 17
  • 22
  • can i set ini_set( 'memory_limit', '255M'); in the file or is it a server variable ? – Shawn Feb 19 '15 at 12:01
  • `ini_set` is a php function, so paste the code at the top of your php script. – Mario A Feb 19 '15 at 12:02
  • you can also add this to your php.ini: `memory_limit = 255M` – Mario A Feb 19 '15 at 12:04
  • i changed memory limit in php.ini to 100M but it did not work. Now the 255M limit works fine. Both your answers are good and work. Kind of strange thou that a limit of 100M can't upload a 7mb file. Thank you so much – Shawn Feb 19 '15 at 12:09
  • It's neither the upload nor the filesize, but the resolution and the image processing. Imagine that when you scale an image, each pixel of the source and the destination image must be held in memory, and each pixel consumes 3 Bytes (RGB). – Mario A Feb 19 '15 at 12:21