Imagecopyresized() question

Hi there,

I have successfully created a script that uploads images and copies them via gd.

    list($width, $height) = getimagesize($file_tmp);
           //calculate the image ratio
           $imgratio=$width/$height;
           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           $img_name = $rand_name.$file_ext;
           //the resizing is going on here!
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
 

I would like to make the script check to see if the image width is greater than 700px. If it is, I would like to save the image to new dimensions with 700px the max width; otherwise, just save the image as is.

Can you help me structure my if/else to properly accomplish this?

My thoughts are that I would say something like this:


if ($newwidth > 700) {
$newwidth = 700;
$newheight = ($newwidth * $imgratio)
} else {
//continue as planned

Any thoughts are greatly appreciated. Thank you.

Set $thumbwidth to 700, and you’ll never get an image wider than 700.

If imagewidth > imageheight;
Newwidth = thumbwidth = 700
If imagewidth < imageheight
Newwidth = thumbwidth * ratio. Ratio (imagewidth/imageheight) must be less than 1, so thumbwidth * ratio must be less than thumbwidth (700);

Sorry to be obtuse, but do you mean like this:

           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
              //=================
            
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           
		   if ($newwidth > $newheight) {
			$newwidth = $ThumbWidth = 700;
		   }
		   
		   if ($newwidth < $newheight) {
			$newwidth = $ThumbWidth * $imgratio;
		   }
           
           //function for resize image.
           $resized_img = imagecreatetruecolor($newwidth,$newheight);

If so, at what point do I make the new height for to match the 700 px width?

I had to do this myself just the other day:


function resizeImage($source, $destination, $target_width){
	$success = false;
	$dims = getimagesize($source);
	$width = $dims[0];
	$height = $dims[1];
	$ratio = $width/$height;
	
	$target_height = $target_width*$ratio;
	$thumb = imagecreatetruecolor($target_width, $target_height);
	$source = imagecreatefromjpeg($source);
	imagecopyresampled($thumb, $source, 0, 0, 0, 0, $target_width, $target_height, $width, $height);
	
	if(imagejpeg($thumb, $destination)) $success = true;
	
	return $success;
}

$max_image_width = 700; //set image width here
$dims = getimagesize($filename);
if($dims[1] > $max_image_width){
	if(!resizeImage($upload_filename, $destination_filename, $max_image_width)){
		echo 'There was an error copying image';
		exit();
	}
}


Any good?

No, I mean the script you have already does this - think of it this way. If $ThumbWidth has a value of 700 at the start of your script; what are the possible values of $newwidth at the end?

Thanks for the responses. I feel very close to a solution. I have figured out how to resize width, but not height. If the image width is greater than 700, I set the width to 700. But I haven’t been able to change the height proportionally! The image is stretched as a result. Any ideas?

      
				if ($width>700) {
					$newwidth = 700;
					$newheight = $newwidth * $imgratio;
					
					if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
						   $new_img = imagecreatefromjpeg($file_tmp);
					   }elseif($file_type == "image/pjpg" || $file_type == "image/jpg"){
						   $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);
					   }
					
					
           			$file_tmp = imagecreatetruecolor($newwidth,$newheight);
					imagecopyresized($file_tmp, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           			//finally, save the image
           			ImageJpeg ($file_tmp, $bigpath);
           			ImageDestroy ($file_tmp);
           			ImageDestroy ($new_img);

Thanks for the advice.

Turn your * into a / and see what happens.
(nw/nh = w/h)
(nw/nh = r)
(nw = r * nh)
(nw/r = nh)

OT: Go go Algebra skills…whoever said you’d never use them.

Thanks StarLion.

My math teacher would definitely be a little disappointed with my algebra. Thanks for helping me understand that a bit better.

I now have figured out how to resize the image properly and save it to my database, etc. For some reason, however, I have not figured out how to make a thumbnail for the new re-sized 700 px image. I am properly thumbnailing for regular images perfectly, however. If anyone has a moment I could use an extra pair of eyes to help me see my mistake:



list($width, $height) = getimagesize($file_tmp);
	   //calculate the image ratio
	   $imgratio=$width/$height;
				
			if ($width>700) {
					$newwidth = 700;
					$newheight = $newwidth / $imgratio;
					
					if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
						   $new_img = imagecreatefromjpeg($file_tmp);
					   }elseif($file_type == "image/pjpg" || $file_type == "image/jpg"){
						   $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);
					   }
					
					
           			$file_tmp = imagecreatetruecolor($newwidth,$newheight);
				       imagecopyresized($file_tmp, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $wi                                       dth, $height);
           			//finally, save the image
           			ImageJpeg ($file_tmp, $bigpath);
           			ImageDestroy ($file_tmp);
        				
        				         if ($imgratio>1){
									  $new2width = $ThumbWidth;
									  $new2height = $ThumbWidth/$imgratio;
									  //=================
									
								 }else{
										 $new2height = $ThumbWidth;
										 $new2width = $ThumbWidth*$imgratio;
								   
								              //function for resize image.
									   $resized_img = imagecreatetruecolor($new2width,$n                                                                    ew2height);
									   //the resizing is going on here!
									   imagecopyresized($resized_img, $new_img, 0, 0, 0,                                                                      0, $new2width, $new2height, $width, $height);
									   //finally, save the image
									   ImageJpeg ($resized_img, $thumbpath);
									   ImageDestroy ($resized_img);
									   ImageDestroy ($new_img);
									   
								 }

Thanks everyone for the support in trying to figure out this issue. I really appreciate it.

what do you do if $width is not > 700? Cause… you’ve got a very large dangling if there.

Thanks for your reply StarLion. This is the “else” to that “if”:

} else
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/pjpg" || $file_type == "image/jpg"){
               $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 the width and height and keep the height ratio.
           list($width, $height) = getimagesize($file_tmp);
           //calculate the image ratio
           $imgratio=$width/$height;
           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
              //=================
            
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           //the resizing is going on here!
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //finally, save the image
           ImageJpeg ($resized_img, $thumbpath);
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);         
        }
        //ok copy the finished file to the thumbnail directory
		move_uploaded_file ($file_tmp, $bigpath);

This is the piece of code that I based the other re-sizing on, also. Everything works as intended except creating the thumbnails as described in my other post.

Thanks for helping me out with this.

Take a look at where the code in post 10 has the end of the inner Else. Then look at your code in post 8. Spot the difference?