Jump to content

Auto adjust width image creation but set height


Russia

Recommended Posts

Hey guys I have a small issue, i have an upload that resizes the image into thumbnail by max width and ratios the width based on that.

Here is my code

$width = 100; //*** Set a Fix Width & Height (Auto caculate) ***//
$size = GetimageSize($images);
$height = round($width * $size[1] / $size[0]);
$images_orig = imagecreatefromjpeg($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY);
ImageJPEG($images_fin, $new_images);



What I am wanting to do is instead upload the image with a max height and ratio the width proportionally. What variables do I have to reverse?

Link to comment
Share on other sites

With this you can set a max width or height and will be a pretty close scale

<?php
$max_size = 100;
$size     = getimagesize($images);
$ratio    = $size[0] / $size[1];
if ($ratio >= 1) {
    $width  = $max_size;
    $height = round($max_size / $ratio);
} else {
    $width  = round($max_size * $ratio);
    $height = $max_size;
}
$images_orig = imagecreatefromjpeg($images);
$photoX     = ImagesX($images_orig);
$photoY     = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width, $height, $photoX, $photoY);
ImageJPEG($images_fin, $new_images);
imagedestroy($images_fin); //you should destroy at the end because will reside in memory
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.