Skip to content

Add ability to set image quality - #8

Closed
bonzai wants to merge 2 commits into
CodeSleeve:developmentfrom
bonzai:image-quality-option
Closed

bonzai wants to merge 2 commits into
CodeSleeve:developmentfrom
bonzai:image-quality-option

Conversation

@bonzai

@bonzai bonzai commented Sep 27, 2013

Copy link
Copy Markdown
Contributor

The image quality of generated images isn't good as it should be so I decided to add a simple option, which allows you to set the quality of every single style with a simple @percent postfix:

$this->hasAttachedFile('image', array(
    'styles' => array(
        'thumbnail' => '70x70@95',
    ),
));

@tabennett

Copy link
Copy Markdown
Contributor

I really like this idea, it's actually something I've been considering for awhile now but just haven't gotten around to implementing. I think there's a better way to implement this though. We can provide a nested array syntax for styles that allow for more detailed settings (file format, etc). We can then use something like paperclip's convert_options setting to provide more detailed image processing:

'styles' => [
    'large' => 
         [
             'geometry' => "280x280", 
             'format' => "png", 
             'convert_options' => "-gravity center -extent 280x280"
        ], 
    'small' => [
        'geometry' => "50x50",
        'format' => "png", 
        'convert_options' => "-gravity center -extent 50x50 -quality 92"
    ]
]

So basically, this would give users an way to supply more advanced style options without having to go so far as providing a callable type as the style.

@bonzai

bonzai commented Oct 1, 2013

Copy link
Copy Markdown
Contributor Author

I was thinking about it too, but personally I don't like the syntax of convert_options (which will work only with ImageMagick).

'styles' => array(
    'foobar' => array(
        //
        // https://imagine.readthedocs.org/en/latest/usage/effects.html
        //
        'effects' => array(
            'gamma' => 0.7,
            'grayscale',
            'negative',
        ),

        //
        // https://imagine.readthedocs.org/en/latest/usage/filters.html
        //
        'filters' => array(
            //
            // We can use class that implements Imagine\Filter\FilterInterface
            //
            'Vendor\Some\HelpfulFilter',

            //
            // We can also use some of built-in filters
            //
            'watermark' => array(
                'image' => function()
                {
                    return (new Imagine\Gd\Imagine())
                        ->open('/path/to/large_image.jpg');
                },
                'x' => '50%',
                'y' => '50%',
            ),

            //
            // Or just define a closure
            //
            function($imagine)
            {
                return $imagine->thumbnail(new Imagine\Image\Box(40, 40), Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND);
            },
        ),
    ),
),

Please take a look at AvalancheImagineBundle. We could borrow some ideas.

@tabennett

Copy link
Copy Markdown
Contributor

I really like this. I think transformations would be great to have. Let me take a look at avalanche and see what we can leverage from there too.

@tortuetorche

Copy link
Copy Markdown

Maybe Illuminage is also a good candidate to be a Stapler dependency, isn't it ?

@jgonera

jgonera commented Feb 12, 2014

Copy link
Copy Markdown

Is there any progress on this? I'd like to use Stapler but the quality of resized images is really bad.

@tabennett

Copy link
Copy Markdown
Contributor

I'm working on this one now. I should have a solution for this sometime in the next few days.

@jgonera jgonera mentioned this pull request Feb 12, 2014
@jgonera

jgonera commented Feb 12, 2014

Copy link
Copy Markdown

Awesome! If you were going to reuse some of @bonzai's code, I rebased it in #33.

@tabennett

Copy link
Copy Markdown
Contributor

I've implemented this feature on development branch in 7c7006c. I'll be merging this into master soon. It uses the following syntax:

$this->hasAttachedFile('avatar',
 [
            'styles' => [
                'thumbnail' => '100x100#',
            ],
            'convert_options' => [
                'thumbnail' => ['quality' => 100]
            ]
   ]);

You can also set the DPI:

$this->hasAttachedFile('avatar', 
[
      'styles' => [
            'thumbnail' => '100x100#',
       ],
       'convert_options' => [
            'thumbnail' => [
                    'quality' => 100,
                    'resolution-units' => ImageInterface::RESOLUTION_PIXELSPERINCH,
                    'resolution-x' => 150,
                    'resolution-y' => 120,
             ]
       ]
 ]);

@tabennett tabennett closed this Feb 14, 2014
@jgonera

jgonera commented Feb 16, 2014

Copy link
Copy Markdown

Thanks, works great! One small issue though: readme says "Please see the Imagine\Image documentation for more details." I could not find it and if it wasn't for your comment in this pull request, I would have not known how to use this feature.

@tabennett

Copy link
Copy Markdown
Contributor

@jgonera

jgonera commented Feb 16, 2014

Copy link
Copy Markdown

Yeah, I found the docs, but I didn't know what to look for. I tried searching for "convert_options" which is not present on the page. What I mean is that it's not obvious that I should be looking at the second argument to the save() method (I only figured it out now).

A link to http://imagine.readthedocs.org/en/latest/usage/introduction.html#save-images and saying that it's the same as the second argument to save() would help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants