Jump to content

weird problem with uploading big images


garydt

Recommended Posts

OK Good News and bad News

 

Good News: I am at home and ran some tests, and it works :)

 

Bad News: I didn't change much..

 

So i have PM'ed you a link to my test site it has a link to phpinfo() and the source code (little changes)

 

files uploaded for you to test are:~

 

1.1Mb File 2000x1303 (main file)

3.5Mb File 4000x2606 (doubled)

10.2mb File 8000x5212 (doubled) again

 

Link to comment
Share on other sites

  • Replies 92
  • Created
  • Last Reply

Top Posters In This Topic

MadTechie is right on. Honestly, you wouldn't need more than about 60M or so for at 3500 pixel image, even if PHP creates a bitmap copy during the image resampling process. 80M should be enough, and 160M is probably overkill.

 

I take it the memory_get_usage() line never executed or showed you any additional error message?

Link to comment
Share on other sites

No, the memory_get_usage() line never executed or showed any additional error message.

 

Thanks very much, MadTechie. I can see it should b working, i can't understand why  my script doesn't always work

 

I added the lines -  ini_set('memory_limit', '500M');

        set_time_limit ( 0 );

and i still get server error 500 or it says its  not a jpeg when i try and upload/resize a large image.

here's most of my script in case you spot something i've got wrong.

Upload form-

<form name="form1" id="Upload" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data" method="POST"> 
        <p> 
            <input type="hidden" name="MAX_FILE_SIZE" value="2000000000"> 
        </p> 
        <h3> 
          <input name="file" type="file" id="file" onchange="MM_showHideLayers('Layer1','','hide')" />
          <span class="style1">for your photo</span> </h3>
        <p>
          <label>
          <input name="sent" type="hidden" id="sent" value="1" />
          </label>
        </p>
	<h3 class="style7">
          <input type="text" name="textfield" />
          <span class="style2 style1">Add caption for the photo</span>          </h3>
            <input name="submit" type="submit" id="submit" onclick="MM_showHideLayers('Layer1','','hide','Layer3','','show')" value="Upload your photo"> 
        </p> 
     
        <input type="hidden" name="MM_insert" value="form1">
</form>

check if its a jpeg and resize image to 140pixels.

if(isset($_POST['sent'])){
unset($submit);
if (isset($_POST['sent'])) $submit = true;
else die('Error: No file got sent');
if($_FILES['file']['name'])
   {
      $arr  = getimagesize($_FILES['file']['tmp_name']); 

      list($width, $height, $type, $attr) = getimagesize($_FILES['file']['tmp_name']); 
       if($type == FALSE)
      {
         header("Location: uploadform2.php");
         exit();
      }
               elseif($type != 2)
                {

      header("Location: uploadform2.php");
           exit();
      }   
   }

$fieldname = $_FILES['file'];
function resizeimage($name){
$filename = './uploads/'.$name;
	if(file_exists($filename)) 
	{
	ini_set('memory_limit', '500M'); 
        set_time_limit ( 0 ); 
		$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($filename); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 140;
		}else {
			$im_divice	= $im_height / 140;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$name, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}

After this it resizes the image to 300pixels and saves it to another folder.

My phi.ini -

upload_max_filesize: 20M

max_execution_time: 300

memory_limit: 300M

post_max_size: 55M

SMTP: localhost

extension=php_gd2.dll

 

On the server it has  GD 1.6.2 (FreeType, TTF library, GIF Read/Create, JPG, PNG, WBMP)

is this ok?

 

Link to comment
Share on other sites

Page is timeing out..

 

on your code add

if (ob_get_level() == 0) ob_start();

 

then

after the upload add

echo "upload done";
ob_flush();
flush();
set_time_limit ( 0 );

 

this will reset the timeout..

 

basically you are uploading and resizing in one timeout slot

 

by adding the above the timeout will start again the echo can be "" but for testing have some text in their

see if that helps.. i don't have access to my php.ini so i'm more limited

Link to comment
Share on other sites

at start of the script

if (ob_get_level() == 0) ob_start();

//only need once

 

after the upload

echo ""; //send something to page to restart timeout
ob_flush();
flush();
set_time_limit ( 0 ); //reset timeout timer

 

before the reading of the JPG (i think thats the 135 line)

ini_set('memory_limit', '500M'); //overkill memory

Link to comment
Share on other sites

resize and upload script

if(isset($_POST['sent'])){
unset($submit);
if (isset($_POST['sent'])) $submit = true;
else die('Error: No file got sent');
if($_FILES['file']['name'])
  {
     $arr  = getimagesize($_FILES['file']['tmp_name']); 

     list($width, $height, $type, $attr) = getimagesize($_FILES['file']['tmp_name']); 
      if($type == FALSE)
     {
   if ($num==1) {
        header("Location: uplodsmalpic3.php?picnum=1");
        exit();
     }
  if ($num==2) {
        header("Location: uplodsmalpic3.php?picnum=2");
        exit();
     }
  if ($num==3) {
        header("Location: uplodsmalpic3.php?picnum=3");
        exit();
     }
  if ($num==4) {
        header("Location: uplodsmalpic3.php?picnum=4");
        exit();
     }
  
  }
              elseif($type != 2)
               {
if ($num==1) {
        header("Location: uplodsmalpic3.php?picnum=1");
        exit();
     }
  if ($num==2) {
        header("Location: uplodsmalpic3.php?picnum=2");
        exit();
     }
  if ($num==3) {
        header("Location: uplodsmalpic3.php?picnum=3");
        exit();
     }
  if ($num==4) {
        header("Location: uplodsmalpic3.php?picnum=4");
        exit();
     }
     
  }
  }
$fieldname = $_FILES['file'];
function resizeimage($name){
$filename = './uploads/'.$name;
	if(file_exists($filename)) {
	ini_set('memory_limit', '500M'); 
		$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($filename); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 140px; max width: 140px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 140;
		}else {
			$im_divice	= $im_height / 140;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$name, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}

function resizelarger($name){
$filename = './uploads/'.$name;
	if(file_exists($filename)) {
		$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($filename); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 300px; max width: 300px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 300;
		}else {
			$im_divice	= $im_height / 300;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "smallpics/".$name, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
      }

// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = './uploads/'; 

//Upload image
$uploadFilename = $uploadsDirectory.$fieldname['name'];

// now let's move the file to its final location and allocate the new filename to it 
move_uploaded_file($fieldname['tmp_name'], $uploadFilename);


$uploadsDirectory = './uploads/'; 

$uploadedphoto = $uploadsDirectory.$fieldname['name'];

$user = $_SESSION['MM_Username'];
if (ob_get_level() == 0) ob_start();
ob_flush();
flush();
set_time_limit ( 0 );
$a = rand(1,99);
resizeimage($fieldname['name']);
rename("thumbnails/".$fieldname['name'], "thumbnails/".$user.$a.$fieldname['name']);
$smallimage = './thumbnails/'.$user.$a.$fieldname['name'];

resizelarger($fieldname['name']);
rename("smallpics/".$fieldname['name'], "smallpics/".$user.$a.$fieldname['name']);
$smallpics = './smallpics/'.$user.$a.$fieldname['name'];
unlink($uploadedphoto);

Link to comment
Share on other sites

updated resize and upload script

if(isset($_POST['sent'])){
unset($submit);
if (isset($_POST['sent'])) $submit = true;
else die('Error: No file got sent');
if($_FILES['file']['name'])
   {
      $arr  = getimagesize($_FILES['file']['tmp_name']); 

      list($width, $height, $type, $attr) = getimagesize($_FILES['file']['tmp_name']); 
       if($type == FALSE)
      {
   if ($num==1) {
         header("Location: uplodsmalpic3.php?picnum=1");
         exit();
      }
  if ($num==2) {
         header("Location: uplodsmalpic3.php?picnum=2");
         exit();
      }
  if ($num==3) {
         header("Location: uplodsmalpic3.php?picnum=3");
         exit();
      }
  if ($num==4) {
         header("Location: uplodsmalpic3.php?picnum=4");
         exit();
      }
  
  }
               elseif($type != 2)
                {
if ($num==1) {
         header("Location: uplodsmalpic3.php?picnum=1");
         exit();
      }
  if ($num==2) {
         header("Location: uplodsmalpic3.php?picnum=2");
         exit();
      }
  if ($num==3) {
         header("Location: uplodsmalpic3.php?picnum=3");
         exit();
      }
  if ($num==4) {
         header("Location: uplodsmalpic3.php?picnum=4");
         exit();
      }
     
   }
   }
$fieldname = $_FILES['file'];
function resizeimage($name){
$filename = './uploads/'.$name;
	if(file_exists($filename)) {
	ini_set('memory_limit', '500M'); 
		$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($filename); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 140px; max width: 140px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 140;
		}else {
			$im_divice	= $im_height / 140;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$name, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}

function resizelarger($name){
$filename = './uploads/'.$name;
	if(file_exists($filename)) {
		$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($filename); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 300px; max width: 300px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 300;
		}else {
			$im_divice	= $im_height / 300;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "smallpics/".$name, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
       }

// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = './uploads/'; 

//Upload image
$uploadFilename = $uploadsDirectory.$fieldname['name'];

// now let's move the file to its final location and allocate the new filename to it 
move_uploaded_file($fieldname['tmp_name'], $uploadFilename);


$uploadsDirectory = './uploads/'; 

$uploadedphoto = $uploadsDirectory.$fieldname['name'];

$user = $_SESSION['MM_Username'];
echo "upload done";
ob_flush();
flush();
set_time_limit ( 0 );
$a = rand(1,99);
resizeimage($fieldname['name']);
rename("thumbnails/".$fieldname['name'], "thumbnails/".$user.$a.$fieldname['name']);
$smallimage = './thumbnails/'.$user.$a.$fieldname['name'];

resizelarger($fieldname['name']);
rename("smallpics/".$fieldname['name'], "smallpics/".$user.$a.$fieldname['name']);
$smallpics = './smallpics/'.$user.$a.$fieldname['name'];
unlink($uploadedphoto);

Link to comment
Share on other sites

revised (few changes)

 

i added some CheckPoints it should display something ie

1

1.1

2

etc

 

let me know what it gets to..

 

<?php
if(isset($_POST['sent']))
{
unset($submit);
if (isset($_POST['sent']))
{
	$submit = true;
}else{
	die('Error: No file got sent');
}

if($_FILES['file']['name'])
{
	$arr  = getimagesize($_FILES['file']['tmp_name']); 

	list($width, $height, $type, $attr) = getimagesize($_FILES['file']['tmp_name']); 
	if($type == FALSE)
	{
		header("Location: uplodsmalpic3.php?picnum=$num");
		exit();
	}elseif($type != 2){
		header("Location: uplodsmalpic3.php?picnum=$num");
		exit();	     
	}
}
}
$fieldname = $_FILES['file'];

// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = './uploads/'; 

//Upload image
$uploadFilename = $uploadsDirectory.$fieldname['name'];

// now let's move the file to its final location and allocate the new filename to it 
move_uploaded_file($fieldname['tmp_name'], $uploadFilename);

$uploadedphoto = $uploadsDirectory.$fieldname['name'];

$user = $_SESSION['MM_Username'];

CP("1");
$a = rand(1,99);
resizeimage($fieldname['name']);
CP("2");
rename("thumbnails/".$fieldname['name'], "thumbnails/".$user.$a.$fieldname['name']);
$smallimage = './thumbnails/'.$user.$a.$fieldname['name'];
CP("3");
resizelarger($fieldname['name']);
rename("smallpics/".$fieldname['name'], "smallpics/".$user.$a.$fieldname['name']);
$smallpics = './smallpics/'.$user.$a.$fieldname['name'];
unlink($uploadedphoto);

function CP($Num)
{
echo "CP: $Num<br />";
ob_flush();
flush();
set_time_limit ( 0 );
}


function resizeimage($name)
{
$filename = './uploads/'.$name;
if(file_exists($filename))
{
	ini_set('memory_limit', '500M'); 
	$image		= imagecreatefromjpeg($filename); //unresized image
	$im_info	= getimagesize($filename); //unresized image
	$im_width	= $im_info[0];
	$im_height	= $im_info[1];
	$im_flag	= $im_info[2];

	//max height: 140px; max width: 140px
	if($im_width >= $im_height) {
		$im_divice	= $im_width / 140;
	}else {
		$im_divice	= $im_height / 140;
	}
	$thumb_width	= $im_width / $im_divice;
	$thumb_height	= $im_height / $im_divice;

	//create empty image
	ini_set('memory_limit', '500M'); 
	CP("1.1");
	$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
	//resize image
	imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

	if(imagejpeg($thumb, "thumbnails/".$name, 80)) { //image-resource, filename, quality
		return 1;
	}

	imagedestroy($thumb); //destroy temporary image-resource
	imagedestroy($image); //destroy temporary image-resource
}
}

function resizelarger($name)
{
$filename = './uploads/'.$name;
if(file_exists($filename))
{
	$image		= imagecreatefromjpeg($filename); //unresized image
	$im_info	= getimagesize($filename); //unresized image
	$im_width	= $im_info[0];
	$im_height	= $im_info[1];
	$im_flag	= $im_info[2];

	//max height: 300px; max width: 300px
	if($im_width >= $im_height) {
		$im_divice	= $im_width / 300;
	}else {
		$im_divice	= $im_height / 300;
	}
	$thumb_width	= $im_width / $im_divice;
	$thumb_height	= $im_height / $im_divice;

	//create empty image
	$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
	//resize image
	ini_set('memory_limit', '500M'); 
	CP("2.1");
	imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

	if(imagejpeg($thumb, "smallpics/".$name, 80)) { //image-resource, filename, quality
		return 1;
	}

	imagedestroy($thumb); //destroy temporary image-resource
	imagedestroy($image); //destroy temporary image-resource
}
}


?>

Link to comment
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.