×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Resizing image with same aspect ratio

Resizing image with same aspect ratio

Resizing image with same aspect ratio

(OP)
I have a script that uploads and if necessary resizes an image. It works fine except that if the uploaded image is not the same aspect ratio as the source image it gets distorted. Can anyone help me work out what I need to add to change the aspect ratio please?

CODE --> php

$image = 'img'.date('U').'.'.$extn;
$destfile = '../video-img/'.$image;
$tempfile = '../temp/'.$image;
$tempname = $_FILES['image']['tmp_name'];
$size = getimagesize($tempname);
$width  = 800;
$height = 450;
if ( ($size[0] < $width) || ($size[1] < $height) )
  echo '<p class="error">Image ', $filename, ' is too small but will be resized and uploaded.</p>', "\n";

// if image is the right size, just save it
if ( ($size[0] === $width) || ($size[1] === $height) ) {
  if ( !(is_uploaded_file($tempname) && move_uploaded_file($tempname, $destfile)) )
    echo '<p class="error">Image ', $filename, ' could not be uploaded.</p>', "\n";
}
// if image is not the right size, save it to a temporary are and then resize it
else {
  if ( !(is_uploaded_file($tempname) && move_uploaded_file($tempname, $tempfile)) )
    echo '<p class="error">Image ', $filename, ' could not be uploaded.</p>', "\n";
  $resized = imagecreatetruecolor($width, $height);

  if ( $extn == 'jpg' or $extn == 'jpeg' ) {
    $srcfile = imagecreatefromjpeg($tempfile);
    imagecopyresampled($resized, $srcfile, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
    imagejpeg($resized, $destfile, 80);
  }
  elseif ( $extn == 'png' ) {
    $srcfile = imagecreatefrompng($tempfile);
    imagecopyresampled($resized, $srcfile, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
    imagepng($resized, $destfile, 6);
  }
  imagedestroy($resized);
  imagedestroy($srcfile);
  unlink($tempfile);
} 

I'm not a number, I'm a free man

RE: Resizing image with same aspect ratio

The predefined $width and $height is causing the problem. You need to calculate that instead of defining 800 and 450.

Assuming that you want the image to be 800 pixels wide, calculate what percentage of the original $size[0] would equal 800 pixels ($width). Then apply that same percentage to $size[1] to determine the proper $height.

Don't forget to round the $height to an integer.

RE: Resizing image with same aspect ratio

...and as these may be video thumbnails/previews, you may also consider cropping to the target size of 800/450. There may be fault in the rest of your project if the images are not consistently sized.

https://www.google.com/search?q=php+image+cropping

RE: Resizing image with same aspect ratio

(OP)
Thanks spamjim.

I'm not a number, I'm a free man

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close