John Davidson

php - Change image dimensions to 4:3 in imagemagick without cropping the image

0 comments
Message:


I have this image which I get from an API call:



On the client's website, everything is set to use 4:3 format images.


Now I tried many aproaches to change the dimensions of the image to 4:3 but it always crops the image which is not de desired result.


This code:


"convert /original-file.jpg -background white -gravity center -extent 4:3 jpg:new-file.jpg";

generates this image:



But I want this result:




My Answer:
php
$image = new Imagick('input.jpg');

$width = $image->getImageWidth();
$height = $image->getImageHeight();

$newWidth = $width;
$newHeight = round($width * 3 / 4);

$image->resizeImage($newWidth, $newHeight, Imagick::FILTER_LANCZOS, 1);

$image->writeImage('output.jpg');
$image->destroy();


This code will resize the image to have a 4:3 aspect ratio without cropping the image. It calculates the new height based on the new width and the desired aspect ratio of 4:3.

Rate this post

3 of 5 based on 3720 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us