PHP Crop Image to Fix Width and Height Without Losing Dimension Ratio

PHP crop image to fix width and height without losing dimension ratio

This is done by only using a part of the image as the thumbnail which has a 1:1 aspect ratio (mostly the center of the image). If you look closely you can see it in the flickr thumbnail.

Because you have "crop" in your question, I'm not sure if you didn't already know this, but what do you want to know then?

To use cropping, here is an example:

//Your Image
$imgSrc = "image.jpg";

//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);

//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);

// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}

// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);

How do I Resize images to fixed width & height while maintaing aspect ratio in PHP?

    $newWidth = 250;
$newHeight = 250;

// download and create gd image
$image = ImageCreateFromString(file_get_contents($url));
$width = ImageSX($image);
$height = ImageSY($image);

$coefficient = $newHeight / $height;
if ($newHeight / $width > $coefficient) {
$coefficient = $newHeight / $width;
}

// create image
$output = ImageCreateTrueColor($newWidth, $newHeight);

ImageCopyResampled($output, $image, 0, 0, 0, 0, $width * $coefficient, $height * $coefficient, $width, $height);

// save image
ImageJPEG($output, $destdir, 100);

PHP Resize Uploaded Image To Specific Dimensions Without Cropping

Ended up correcting this. The issue was how I was calculating and compensating for ratios. This was the final code that corrected the issue, with variables to make the code more versatile for other situations

             $targetWidth = 600;
$targetHeight = 360;
$dstY = 0;
$dstX = 0;
$ratioFactor = 0;

$bgImage = imagecreatefromjpeg("../img/bgWhite.jpg");
$dimensions = getimagesize($files["tmp_name"][$i]);
$sourceWidth = $dimensions[0];
$sourceHeight = $dimensions[1];
$ratio = $sourceWidth / $sourceHeight; // width/height

//final image should be 600x360
if ($ratio > 1) {
$ratioFactor = $sourceWidth / $targetWidth;
$targetHeight = $sourceHeight / $ratioFactor;
$dstY = (360 - $targetHeight) / 2;
} else {
$ratioFactor = $sourceHeight / $targetHeight;
$targetWidth = $sourceWidth / $ratioFactor;
$dstX = (600 - $targetWidth) / 2;
}

$source = imagecreatefromstring(file_get_contents($files["tmp_name"][$i]));
$target = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($target, $source, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);

imagecopymerge($bgImage, $target, $dstX, $dstY, 0, 0, imagesx($target), imagesy($target), 100);
imagepng($bgImage, $dir . $filename);

Image Resize without cropping and maintaing aspect ratio

Documentation of imagemagick:

http://www.imagemagick.org/Usage/resize/#thumbnail


Or, if you are using it in code:

$width = 100; 
$height = 300;

$ratio = $width / $height; // get ratio by dividing width by height

$newWidth = 200; // a random new width
$newHeight = $newWidth / $ratio; // devide by ratio to get new width (600 in this case)
// OR, if you have a new height:
$newHeight = 200;
$newWidth = $newHeight * $ratio; // multiply instead of devide

Dont forget to use round() in the end if you want to make actual pixels of it (which I asume you will)


Coming back here I thought of another solution (which actually is easier):

$width = 100; 
$height = 300;

$newWidth = 200;
$newHeight = ($newWidth/$width) * $height;

The $newWidth/$width part calculates how much the width has changed (in my example it will result in 2), so the height getschanged in the same ratio. Same works of you turn it arround for if you want $newWidth based on height.

Crop image to fit in container as close to desired size as possible without losing ratio

You can try

$imageHeight = 500;
$imageWidth = 600;

$maxHeight = 315;
$maxWidth = 210;

$max = ($maxWidth > $maxHeight) ? $maxWidth : $maxHeight;
$ratio = max($imageWidth, $imageHeight) / $max;

$ratio = max($ratio, 1.0);

$newHeight = ceil($maxHeight / $ratio);
$newWidth = ceil($imageWidth / $ratio);

var_dump("Height From: $imageHeight -> $newHeight", "Width From : $imageWidth -> $newWidth " );

Output

string 'Height From: 500 -> 166' (length=18)
string 'Width From: 600 -> 315' (length=20)

Resize image without distortion keeping aspect ratio then crop excess using WideImage

You might try:

$img->resize(200, 150, 'outside')->crop('center', 'middle', 200, 150);

Some users post their versions of calculations... Here's also my version:

$sourceWidth = 1000;
$sourceHeight = 250;

$targetWidth = 200;
$targetHeight = 150;

$sourceRatio = $sourceWidth / $sourceHeight;
$targetRatio = $targetWidth / $targetHeight;

if ( $sourceRatio < $targetRatio ) {
$scale = $sourceWidth / $targetWidth;
} else {
$scale = $sourceHeight / $targetHeight;
}

$resizeWidth = (int)($sourceWidth / $scale);
$resizeHeight = (int)($sourceHeight / $scale);

$cropLeft = (int)(($resizeWidth - $targetWidth) / 2);
$cropTop = (int)(($resizeHeight - $targetHeight) / 2);

var_dump($resizeWidth, $resizeHeight, $cropLeft, $cropTop);

PHP Convert image to specific height and width without stretching

After digging up some long lost mathematics knowledge, I found a working solution:

    $thumb_width = 225;
$thumb_height = 150;

if( ($owidth/$thumb_width) > ($oheight/$thumb_height) ) {
$y = 0;
$x = $owidth - ( ($oheight * $thumb_width) / $thumb_height);
} else {
$x = 0;
$y = $oheight - ( ($owidth * $thumb_height) / $thumb_width);
}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
imagecopyresampled( $thumb, $image, 0, 0, $x/2, $y/2, $thumb_width, $thumb_height, $owidth - $x, $oheight - $y);


Related Topics



Leave a reply



Submit