Jump to content

Crop images by width


bytesize

Recommended Posts

I need to set the width and let height adjust to the proportions of width. Can someone please help me with this?

I understand that $canvas_height should be 160, but then the image will crop to whichever side is greatest not by the width only.

<?php
$img = "image.jpg";
$canvas_width  = 160;
$canvas_height = 0;

list($img_width, $img_height) = getimagesize($img);

$ratio_orig = $img_width / $img_height;

if($canvas_width / $canvas_height > $ratio_orig)
{
$canvas_width = $canvas_height * $ratio_orig;	
}
else
{
$canvas_height = $canvas_width / $ratio_orig;
}

$original = imagecreatefromjpeg($img);

$canvas = imagecreatetruecolor($canvas_width, $canvas_height);

imagecopyresampled($canvas, $original, 0, 0, 0, 0, $canvas_width, $canvas_height, $img_width, $img_height);

$dest = "medium/image.jpg";
imagejpeg($canvas, $dest, 100);
?>

Link to comment
https://forums.phpfreaks.com/topic/222668-crop-images-by-width/
Share on other sites

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.