2017 © Pedro Peláez
 

library cog-imageresize

Dynamic URI-based image resize functionality for Cog

image

mothership-ec/cog-imageresize

Dynamic URI-based image resize functionality for Cog

  • Tuesday, September 29, 2015
  • by thomasjthomasj
  • Repository
  • 4 Watchers
  • 1 Stars
  • 2,378 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 2 Forks
  • 8 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Image Resize

Resize Class and Controller

The Resizer can be accessed using the service image.resize:, (*1)

$this->get('image.resize')->resize($url);

The Resize-class has two important methods:, (*2)

  • generateUrl($url, $width, $height) - Generates a url consisting of the file-name, width, height and a hash which will be the later location of the file. The $url is the publically accessible path to the image we want to resize. Urls generated by this method could look something like this: resize_directory/test_AUTOx400-de46b9.jpg
  • resize($url) - Does the actual resizing, returns the resized file and saves it in the location defined by $url. The $url passed in is a the url generated by generateUrl().

This means, when we have an image called test.jpg, which is saved in cog:://public/files, this is how we could resize it:, (*3)

$resizer = $this->get('image.resize');
$url = $resizer->generateUrl('/files/test.jpg', null, 400); // you can leave out either height or width!
$resizedImg = $resizer->resize($url);

We go the way of generating the url because that allows us to controll the behaviour with a controller: When we want to render a resized image in a view, we can pass in the generated $url as src-attribute. What will happen is, the first time the image is requested, the router will call the image resize controller, which will resize the image, save it to the new destination and return the image. After that the image already exists and can requested as any other image., (*4)

This is handy especially when used together with the twig extension., (*5)

Twig Extension

There are two methods which allow us to access the generateUrl()-method the ImageResize\Resize-class provides:, (*6)

  • getResizedUrl(ResizableInterface $file, $width, $height) - Returns the result of Resizer::generateUrl using the getUrl()-method the ResizableInterface defines:, (*7)

    <img src="{{ getResizedUrl(file, 500, 200) }}" width="500" height="200" alt="file.altText">
  • getResizedImage(ResizableInterface $file, $width, $height, $attributes = array()) - Returns the rendered <img>-tag including src, width, height, alt(using ResizableInterface's getAltText()-method) and additional attributes using the $attributes-array:, (*8)

    {# file->getUrl() = '/test.jpg' #}
    {# file->getAltText() = 'test' #}
    
    {{ getResizedImage(file, 500, 200, {'class': 'file'}) }}

    Which will render, (*9)

    <img src="/test_500x200-88953b.jpg" width="500" height="200" alt="test" class="file">

    If the $attributes-array provides an alt-attribute, instead of using file's getAltText() to determine the alt-text, $attributes['alt'] will be shown:, (*10)

    {{ getResizedImage(file, 500, 200, {'alt': 'abcabc'}) }}

    This will return this:, (*11)

    <img src="/test_500x200-88953b.jpg" width="500" height="200" alt="abcabc">

Default Image

If the $url passed to the resize-method is not the path to an existing file in the system, a default-image is resized instead., (*12)

This default image can be configured in config/app.yml:, (*13)

image-resize:
    default-image-path: cogules/MyApp:SetUp/images/default.png

The generateUrl method looks up the file and if it doesn't exist it returns the url to the default image. That way only one image for each size is generated and the correct resized image can be generated as soon as a file is uploaded to the server., (*14)

The Versions

29/09 2015

dev-develop

dev-develop http://mothership.ec

Dynamic URI-based image resize functionality for Cog

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

image resize thumbnail

29/09 2015

dev-master

9999999-dev http://mothership.ec

Dynamic URI-based image resize functionality for Cog

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

image resize thumbnail

29/09 2015

2.0.1

2.0.1.0 http://mothership.ec

Dynamic URI-based image resize functionality for Cog

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

image resize thumbnail

24/02 2015

2.0.0

2.0.0.0 http://mothership.ec

Dynamic URI-based image resize functionality for Cog

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

image resize thumbnail

18/03 2014

1.1.0

1.1.0.0 http://message.co.uk

Dynamic URI-based image resize functionality for Cog

  Sources   Download

proprietary

The Requires

 

by The Message Development Team

image resize thumbnail

30/01 2014

dev-feature/4-uri-end

dev-feature/4-uri-end http://message.co.uk

Dynamic URI-based image resize functionality for Cog

  Sources   Download

proprietary

The Requires

 

by The Message Development Team

image resize thumbnail

30/01 2014

1.0.2

1.0.2.0 http://message.co.uk

Dynamic URI-based image resize functionality for Cog

  Sources   Download

proprietary

The Requires

 

by The Message Development Team

image resize thumbnail

10/01 2014

1.0.1

1.0.1.0 http://message.co.uk

Dynamic URI-based image resize functionality for Cog

  Sources   Download

proprietary

The Requires

 

by The Message Development Team

image resize thumbnail

10/01 2014

1.0.0

1.0.0.0 http://message.co.uk

Dynamic URI-based image resize functionality for Cog

  Sources   Download

proprietary

The Requires

 

by The Message Development Team

image resize thumbnail