CodeSleeve / stapler

ORM-based file upload package for php.
http://codesleeve.com
Other
538 stars 144 forks source link

resize only if original is bigger than requested style #161

Closed jbajou closed 8 years ago

jbajou commented 8 years ago

Hello,

I would like to know if there is an easy way to resize an image only if it is bigger than the size defined in the requested style. If the image is smaller, we just do not touch it.

Thank you for your help Regards,

jbajou commented 8 years ago

any idea ? We're still stuck on this...

Thanks

tabennett commented 8 years ago

I can't think of a way to do this (off the top of my head) using a plain style. I think that you could probably achieve something very similar to this by using a closure for your style and then inside the closure comparing it against the dimensions your using so that you can resize accordingly. What's your use case for this?

jbajou commented 8 years ago

We use userside uploaded pictures for display in a quiz. The image need to be smaller than 600x400 for example. If it is bigger, we resize and crop. If it is smaller, we would like to leave it as it is. (as of now, it is resized to 600x400 making small images unreadable)

jbajou commented 8 years ago

@tabennett Do you have by any chance any example of such a closure ?

Thanks in advance for your help

tabennett commented 8 years ago

Bottom of the page. You could alter the watermark example to get just grab the size of the image and then resize it to whatever you want if the dimensions are larger than 600x400.

The UploadedFile object and the ImagineImage instance will be injected in for you automatically.

flyingL123 commented 8 years ago

@jbajou did you ever find a solution for this? I have the same requirement. I'm creating a "zoom" style for user uploaded images. The zoom style takes the original image and resizes it proportionally to a width of 1280px. However, if the original image is less than 1280px wide, I'd like to just leave the image as is, essentially making the zoom style size the same as the original.

Currently, it looks like Stapler will stretch the original image to the width of the zoom style, which is not desirable. It would be great if there was some way to build a flag into a style such as enlarge: false, or something like that, which tells the image processing that the original file should never be enlarged. It should either be left as is, or shrunk to a style.

jbajou commented 8 years ago

@flyingL123 didn't get the time to work on that unfortunately. It's in the pipe for this month though. I'll post the snippet here as soon as it's done. In the meantime, if you are more quick than me on this, well, do the same! :)

It actually does feel kind of strange that Stapler doesn't offer such an option.

flyingL123 commented 8 years ago

It looks like this logic is supported by ImageMagick, so I wonder if there is a way to use that. From the docs, I think it would be as simple as:

'300>'

I know I used this method with Paperclip in a Rails project, which uses ImageMagick by default. If Stapler could be set to use ImageMagick, which I think it can, and the style option will be passed directly through, then it seems like it should work.

flyingL123 commented 8 years ago

I've adjusted app/config/packages/codesleeve/laravel-stapler/stapler.php to use Imagine\Imagick\Imagine as the image_processing_library. I then changed my style dimensions on the model to 'dimensions' => '1280x1280>', but it doesn't seem to be working. The image is still being enlarged.

flyingL123 commented 8 years ago

@jbajou - Here's what's working for me. In my case I am only concerned with the width of the original image. If the original is greater than 1280px wide, I resize it proportionally to have a width of 1280. If not, I leave the size unchanged.

'zoom' => function($file, $imagine) {
    $image = $imagine->open($file->getRealPath());

    // Auto rotate the image
    $filter = new \Imagine\Filter\Basic\Autorotate;
    $filter->apply($image);

    // Get the current size
    $size = $image->getSize();

    // Scale down to zoom size only if
    // image is wide enough.
    if ($size->getWidth() > 1280) {
        $newSize = $size->widen(1280);
        $image->resize($newSize);
    }

   // Must use strip() method before returning image.  // I struggled with this for a while. strip() is needed because
   // you need to remove the exif information from the photo after
   // Autorotate'ing it. Otherwise browsers will still think it needs to      // be rotated and will display it incorrectly.
    return $image->strip();
}
jbajou commented 8 years ago

Hi @flyingL123, I adapted your code to my needs (changed size, etc) and it works like a charm! Thanks a lot, and sorry for my late feedback.