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?

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
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.