Gregwar / Image

A PHP library to handle images
MIT License
1k stars 191 forks source link

Return original width after resize #123

Open ssitdikov opened 8 years ago

ssitdikov commented 8 years ago

Hi.

For example, for code {{ image = image(photo).cropResize(120, 120) }} {{ image.width }} will be returned original width, but must be 120 or I am not right?

Reason is the using watermark images for photo-previews, so i need return correct new width/height data, but can't.

hundblue commented 7 years ago

I have encountered the same problem:

use Gregwar\Image\Image;

$in  = __DIR__.'/image-w800.jpg';
$out = __DIR__.'/image-w120.jpg';

$widths = [];

$image = Image::open($in);
$widths['original'] = $image->width();

$image->cropResize(120);
$widths['after-resize'] = $image->width();

$image->save($out);
$widths['after-save'] = $image->width();

print_r ($widths);
// Array ( [original] => 800 [after-resize] => 800 [after-save] => 120 )
cord commented 7 years ago

Can confirm this issue in the latest release

Gregwar commented 7 years ago

This is because width() now is just an helper and doesn't apply the queued operations

Gregwar commented 7 years ago

This could be fixed by applying the operations when calling width(), however this would violate the interest of the cache system in this case Can you describe me your use case so I can think a solution for this?

cord commented 7 years ago

Ok, with a meta-data api call I want to get the dimensions of the generated images by cacheId. I tried to apply images( resource $image) , imagesy( resource $image) but failed to retrieve resource $image after creating the image with $img->png(). Any advice?

Gregwar commented 7 years ago

When you use cache, if there is already the file in cache absolutely no files are opened currently I think in the current release width() will work the first time because the image is generated but will still be the original width the second time I can fix that, however note that calling width() will then cost extra performances since this will open the cached file

cord commented 7 years ago

Should be optional.

Scenario: generating and outputting responsive images with 2x resolution, so the html() method should generate something like this:

<img src="{cacheUrl}" width="{imgWidth/2}" height="{imgHeight/2}">

Gregwar commented 7 years ago

I did a change so that calling width() and height() after generating cache should work now

Gregwar commented 7 years ago

Example:

$img = Image::open('test.jpg')->cropResize(50, 50);
$img->width(); // Width pre-resize
$img->png();
$img->width(); // Width post-resize
cord commented 7 years ago

thanks a lot, works perfectly!

Karmalakas commented 3 years ago

Is this a viable solution? Why issue is still open? Some drawbacks?