Ticket #560 (assigned Bug)
Extending & correcting logic on Image library
| Reported by: | jonathangordon | Owned by: | Shadowhand |
|---|---|---|---|
| Priority: | minor | Milestone: | 2.2 |
| Component: | Libraries:Image | Version: | SVN HEAD |
| Keywords: | image, crop, resize, aspect ratio, logic | Cc: |
Description
Okay, let's assume someone wants to cut a full size image down to fit into a cropped box size of 300x100 pixels:
$image->resize(300, 100)->crop(300, 100)
This won't work.
Well, it could, but only if the aspect ratio is respective of the desired crop aspect ratio. Instead, what will (almost always) happen is the image will fall inside of the box size required, which in this case is 300x100, and the resulting crop will be smaller on one side, in this case; the width, meaning our final cropped image size will end up being 133x100 for a standard 4:3 image, and even worse for tall images. This just won't do.
To get the desired result; a box size not breaking into the boundaries set for any given input and output aspect ratio, rather than not breaking out of the boundaries set, you need to compare the starting and resulting aspect ratios, and choose a master dimension based on that.
Making a new scale-side option (I don't know, 'CANVAS' maybe, since you're requesting a resulting minimum resulting canvas side to crop from) for this would require the following logic:
// image dimensions passed to resize
// equivalent value: x:1
$ar = $width/$height;
// the current working image size
$dimensions = getimagesize($file->image_path);
/*
* Determine which side should be used during resize
* as the master dimension, so we don't break into our
* given box size.
*
*/
if ($dimensions[0] >= $dimensions[1]) { // wide
// wide enough
if (($dimensions[0]/$ar) >= $dimensions[1])
$master = IMAGE::HEIGHT;
// not wide enough for our ar; it would break into the box
else
$master = IMAGE::WIDTH;
}
else { // tall
// tall enough
if ($dimensions[0] <= ($dimensions[1]/$ar))
$master = IMAGE::HEIGHT;
// not tall enough for our ar; it would break into the box
else
$master = IMAGE::WIDTH;
}
I'm not totally sure this would work for tall box-fitting—I didn't actually test that. If not, then you would want to check to see if $ar >= 1 for wide, and have another if block. Nothing too bad.
