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
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);
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?
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:
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.