Image upload freezes with high resolution images

Discussion in 'PHP' started by TOM1, Oct 1, 2006.

  1. #1
    I'm having this weird problem with my image uploading script. If I upload image with 2272x1704 resoltion and 978kb filesize my script freezes. When I scale that same image down to 1400x1050 resolution everything goes well.

    Comments and some variables are in Finnish, sorry about that...
    
    <?php
    
    // Lisätyn tiedoston tyyppi
    $image_type = $_FILES['userfile']['type'];
    
    // Sallitut tiedostotyypit
    $array_allowed_types = array("image/png","image/gif","image/jpg","image/jpeg","image/pjpeg");
    
    // Tarkistetaan lisätty tiedosto							
    if(array_search($image_type,$array_allowed_types) === FALSE)
    {
    	error("Virheellinen tiedostotyyppi");
    }
    
    // Tarkistetaan onko ko. numerolla jo kansio
    $dirname = "cars/".$_POST['kansionumero'];
    if (!is_dir($dirname)) 
    {
    	mkdir ($dirname,0777);
    }
    
    if(($image_type == "image/jpeg") || ($image_type == "image/pjpeg"))
    {
    	$image_type = "image/jpg";
    	$tmp_image = imagecreatefromjpeg($_FILES['userfile']['tmp_name']);
    }
    elseif($image_type == "image/gif")
    {
    	$image_type = "image/jpg";
    	$tmp_image = imagecreatefromgif($_FILES['userfile']['tmp_name']);
    }
    else
    {
      	$tmp_image = imagecreatefromjpeg($_FILES['userfile']['tmp_name']);
    }
    
    $tmp_time = time();
    $image_name = $tmp_time;
    	
    $max_width = 400;
    $max_height = 400;
    
    $width = imagesx($tmp_image);
    $height = imagesy($tmp_image);
    						
    if(($width > $max_width) || ($height > $max_height))
    {
    	if($width > $height)
    	{
    		$new_width = $max_width;
    		$new_height = floor($new_width * $height / $width);
    						
    		$thumb_width = 100;
    		$thumb_height = floor($thumb_width * $height / $width);
    	}
    	else
    	{
    		$ratio = $height / $width;
    		$new_height = $max_height;
    		$new_width = $new_height / $ratio;
    		
    		$thumb_height = 120;
    		$thumb_width = $thumb_height / $ratio;
    	}
    }
    else
    {
    	$new_width = $width;
    	$new_height = $height;
    	
    	if($width > $height)
    	{
    		$ratio = $width / $height;				
    		$thumb_width = 100;
    		$thumb_height = $thumb_width / $ratio;
    	}
    	else
    	{
    		$ratio = $height / $width;				
    		$thumb_height = 120;
    		$thumb_width = $thumb_height / $ratio;
    	}
    	
    }
    				
    $new_image = imagecreatetruecolor($new_width,$new_height);
    imagecopyresampled($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
    
    $thumb_image = imagecreatetruecolor($thumb_width,$thumb_height);
    imagecopyresampled($thumb_image, $tmp_image,0,0,0,0, $thumb_width, $thumb_height, $width, $height);
    
    $watermark = imagecreatefrompng("images/vesileima.png");  				  
    $watermark_width = imagesx($watermark);  
    $watermark_height = imagesy($watermark);  	 
    $dest_x = $new_width - $watermark_width;  
    $dest_y = $new_height - $watermark_height;  
    				
    ImageAlphaBlending($new_image, true);
    imagecopy($new_image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
    		
    ImageJPEG($new_image,$dirname."/r_".$image_name.".".substr($image_type,6),82);
    ImageJPEG($thumb_image,$dirname."/t_".$image_name.".".substr($image_type,6),90);
    		
    ImageDestroy($new_image);	
    ImageDestroy($thumb_image);
    
    $tyyppi = substr($image_type,6);
    
    mysql_query("INSERT INTO table (car_id,name,default_image,added) VALUES('".$_SESSION['id_car']."','".$image_name.".".$tyyppi."','0','".$date."')");
    
    function error($message)
    {
    	exit($message);	
    }
    
    ?>
    Code (markup):

     
    TOM1, Oct 1, 2006 IP
  2. digdogger

    digdogger Well-Known Member

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #2
    The script is probably going over the memory limit when converting the image.

    If you just copy the image and do the processing later, you might not crash.

    You could also check the MAX_POST_SIZE and MAX_UPLOAD_SIZE.

    (actual post size will be bigger than image)
     
    digdogger, Oct 2, 2006 IP
  3. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use ini_set() to increase the memory limit if needed.
     
    penagate, Oct 2, 2006 IP
  4. TOM1

    TOM1 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the tips, I'll give a try.
     
    TOM1, Oct 5, 2006 IP
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    I think generally the memory limit is set to 2MB but this can vary from server to server. If you do what TOM1 says and increase the memory limit in PHP.INI you'll have success!
     
    Weirfire, Oct 5, 2006 IP