Help with Image Function

Discussion in 'PHP' started by adamjblakey, Feb 28, 2008.

  1. #1
    Hi,

    I want to add a bit to the function where the image is resized to 2 sizes and not just one.

    <?php
    function uploadimage($value){
    	$file_type = $value['type'];
    	$file_name = $value['name'];
    	$file_size = $value['size'];
    	$file_tmp = $value['tmp_name'];
    
    	//check file extension
    	$ext = strrchr($file_name,'.');
    	$ext = strtolower($ext);
    
    	//get the file extension.
    	$getExt = explode ('.', $file_name);
    	$file_ext = $getExt[count($getExt)-1];
    
    	//create a random file name
    	$rand_name = md5(time());
    	$rand_name= rand(0,999999999);
    	//get the new width variable.
    	$ThumbWidth = 150;
    
    	//keep image type
    	if($file_size){
    		if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
    			$new_img = imagecreatefromjpeg($file_tmp);
    		}elseif($file_type == "image/x-png" || $file_type == "image/png"){
    			$new_img = imagecreatefrompng($file_tmp);
    		}elseif($file_type == "image/gif"){
    			$new_img = imagecreatefromgif($file_tmp);
    		}
    		//list width and height and keep height ratio.
    		list($width, $height) = getimagesize($file_tmp);
    		$imgratio=$width/$height;
    		if ($imgratio>1){
    			$newwidth = $ThumbWidth;
    			$newheight = $ThumbWidth/$imgratio;
    		}else{
    			$newheight = $ThumbWidth;
    			$newwidth = $ThumbWidth*$imgratio;
    		}
    		$resized_img = imagecreatetruecolor($newwidth, $newheight);
    		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    		//save image
    		move_uploaded_file($resized_img, "uploads/thumbs/$rand_name.$file_ext");
    		imagejpeg ($resized_img,"uploads/thumbs/$rand_name.$file_ext");
    		imagedestroy($resized_img);
    		imagedestroy($new_img);
    
    		//upload the big image
    		move_uploaded_file ($file_tmp, "uploads/big/$rand_name.$file_ext");
    	}	
    	$value = "$rand_name.$file_ext";
    	return $value;
    } ?>
    PHP:
    Cheers,
    Adam
     
    adamjblakey, Feb 28, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You have to make a second call to imagecreatetruecolor(), imagecopyresized(), and imagejpeg().

    imagecreatetruecolor = To create the new image with the new size.
    imagecopyresized = To copy the image contents from the original image to the new one we've created.
    imagejpeg = To create and save the new image.

    www.php.net/imagecreatetruecolor
    www.php.net/imagecopyresized
    www.php.net/imagejpeg

    No offense, but you've been coding for quite some time now. It's about time you learn to work a little bit more independently. :) Give it a try before asking.
     
    nico_swd, Feb 28, 2008 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Your right nico.. so here is my attempt which does not seem to be working. Any Ideas?

    <?php
    function uploadimage($value){
    	$file_type = $value['type'];
    	$file_name = $value['name'];
    	$file_size = $value['size'];
    	$file_tmp = $value['tmp_name'];
    
    	//check file extension
    	$ext = strrchr($file_name,'.');
    	$ext = strtolower($ext);
    
    	//get the file extension.
    	$getExt = explode ('.', $file_name);
    	$file_ext = $getExt[count($getExt)-1];
    
    	//create a random file name
    	$rand_name = md5(time());
    	$rand_name= rand(0,999999999);
    	//get the new width variable.
    	$ThumbWidth = 150;
    	$ImgWidth = 450;
    
    	//keep image type
    	if($file_size){
    		if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
    			$new_img = imagecreatefromjpeg($file_tmp);
    		}elseif($file_type == "image/x-png" || $file_type == "image/png"){
    			$new_img = imagecreatefrompng($file_tmp);
    		}elseif($file_type == "image/gif"){
    			$new_img = imagecreatefromgif($file_tmp);
    		}
    		//list width and height and keep height ratio.
    		list($width, $height) = getimagesize($file_tmp);
    		$imgratio=$width/$height;
    		if ($imgratio>1){
    			$newwidth = $ThumbWidth;
    			$newheight = $ThumbWidth/$imgratio;
    			$newwidth2 = $ImgWidth;
    			$newheight2 = $ImgWidth/$imgratio;
    			
    		}else{
    			$newheight = $ThumbWidth;
    			$newwidth = $ThumbWidth*$imgratio;
    			
    			$newheight2 = $ImgWidth;
    			$newwidth2 = $ImgWidth*$imgratio;
    		}
    		
    		
    		$resized_img = imagecreatetruecolor($newwidth, $newheight);
    		$resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
    		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    		imagecopyresized($resized_img2, $new_img2, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
    		
    		//save image
    		move_uploaded_file($resized_img, "uploads/thumbs/$rand_name.$file_ext");
    		move_uploaded_file($resized_img2, "uploads/big/$rand_name.$file_ext");
    		imagejpeg ($resized_img,"uploads/thumbs/$rand_name.$file_ext");
    		imagejpeg ($resized_img2,"uploads/thumbs/$rand_name.$file_ext");
    		imagedestroy($resized_img);
    		imagedestroy($resized_img2);
    		imagedestroy($new_img);
    		imagedestroy($new_img2);
    
    
    	}	
    	$value = "$rand_name.$file_ext";
    	return $value;
    } ?>
    PHP:
     
    adamjblakey, Feb 28, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    
    imagejpeg ($resized_img,"uploads/thumbs/$rand_name.$file_ext");
    imagejpeg ($resized_img2,"uploads/thumbs/$rand_name.$file_ext");
    
    PHP:
    The second parameter is the path, where the image will be saved. If you're saving both images in the same path, with the same name, one gets overwritten.

    Also, when you say "which does not seem to be working"... please be more specific, and say what it not does, and include all errors you get (if any).
     
    nico_swd, Feb 28, 2008 IP
  5. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Take out the two "move_uploaded_file" calls. They won't work and aren't needed.
    Change your 2nd imagejpeg to: imagejpeg ($resized_img2,"uploads/big/$rand_name.$file_ext");
    Remove the last line - you don't define a $new_img2 anywhere.
     
    jnestor, Feb 28, 2008 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Hi,

    I have changed this now and seems to work. The only problem is that the second uploaded image come out black and not the image.

    $resized_img = imagecreatetruecolor($newwidth, $newheight);
    		$resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
    		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    		imagecopyresized($resized_img2, $new_img2, 0, 0, 0, 0, $newwidth2, $newheight2, $width2, $height2);
    		
    		//save image
    		move_uploaded_file($resized_img, "uploads/thumbs/$rand_name.$file_ext");
    		move_uploaded_file($resized_img2, "uploads/big/$rand_name.$file_ext");
    		imagejpeg ($resized_img,"uploads/thumbs/$rand_name.$file_ext");
    		imagejpeg ($resized_img2,"uploads/big/$rand_name.$file_ext");
    		imagedestroy($resized_img);
    		imagedestroy($resized_img2);
    		imagedestroy($new_img);
    		imagedestroy($new_img2);
    PHP:
    Cheers,
    Adam
     
    adamjblakey, Feb 29, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Replace $new_img2, with $new_img. We're gonna use the same resource for both images.
     
    nico_swd, Feb 29, 2008 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    I am now getting the following error:

    Warning: imagecopyresized() [function.imagecopyresized]: Invalid image dimensions in /home/wish/public_html/test/imageupload.php on line 78
    PHP:
    And also i have changed the line to:
    
    imagecopyresized($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width2, $height2);
    PHP:
    But this has not solved the problem and the image is still black.
     
    adamjblakey, Feb 29, 2008 IP
  9. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #9
    Any ideas anyone?
     
    adamjblakey, Feb 29, 2008 IP