PHP Classes

Lawondyss Imager: Create image thumbnails using ImageMagick

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 185 All time: 8,657 This week: 455Up
Version License PHP version Categories
lawondyss-imager 1.8MIT/X Consortium ...5.4PHP 5, Graphics
Description 

Author

This package can create image thumbnails using ImageMagick command line program.

It can take a given image and resize it to a given size up to 200%.

The package can also process uploaded files copying the original images to a repository directory and creating thumbnail images in another directory.

It can also work as a plugin for the Nette framework.

Picture of Ladislav Vondracek
Name: Ladislav Vondracek <contact>
Classes: 7 packages by
Country: Czech Republic Czech Republic

 

Documentation

Imager

Imager is manager for images and thumbnails.

Installation

Installation performed by [Composer]. Write to command line composer require lawondyss/imager.

CAUTION

Library used [ImageMagick] over command line. Is necessary have ImageMagick installed in system.

Generating image cannot be greater than 200 % of original image.

Examples

Create thumbnails

// Image accepts argument of image as instance of class Imager\ImageInfo (extends SplFileInfo)
$imageInfo = new Imager\ImageInfo('path/to/image.jpg');
$image = new Imager\Image($imageInfo);

// create over factory
// ImageFactory::create() accepts image name in string or instance of ImageInfo
$factory = new Imager\ImageFactory;
$image = $factory->create('path/to/image.jpg');

/Thumbnails/
// resize by width
$thumb = $image->resize(100); // instance of Imager\ImageInfo with temporary image
var_dump($thumb->getPathname()); // path to thumbnail

// resize by height
$thumb = $image->resize(null, 100);

// resize with crop, cropped image is centered
$thumb = $image->resize(100, 100);

// origin dimensions
$thumb = $image->resize(0, 0);

/Quality/
// set in third parameter, lower is worse, 0 is default quality
$thumb = $image->resize(0, 0, 25);

/Send image to output/
header('Content-Type: ' . $thumb->getMime());
header('Content-Length: ' . $thumb->getSize());
echo $thumb->getContent();

Repository for sources images and thumbnails

$factory = new Imager\ImageFactory;

// first argument is required; is directory with sources
// second argument is optional; is directory for thumbnails; if not set, then is same as directory for sources; autocreated 
$repository = new Imager\Repository('path/to/sources', 'path/to/thumbnails');

// create ImageInfo of source image
$uploadImageInfo = $factory->createInfo('path/to/uploaded/image.jpg');

// source image has not source, therefore save to sources directory
// second optional argument defined new name for saved image
$sourceImageInfo = $repository->save($uploadImageInfo, 'image.jpg'); // instance of Imager\ImageInfo with saved source image

// fetch source image only by name
$imageInfo = $repository->fetch('image.jpg'); // instance of Imager\ImageInfo with source image

// created thumbnail
$thumb = $factory->create($imageInfo)->resize(100); // instance of Imager\ImageInfo with temporary thumbnail of image

// thumbnail has source, therefore save to thumbnails directory
$thumbImageInfo = $repository->save($thumb); // instance of Imager\ImageInfo with saved thumbnail

Nette extension

For registration Imager as Nette extension is required add this configuration.

extensions:
    imager: Imager\DI\Extension

Extension has this configuration:

imager:
    sourcesDir: %appDir%/../cdn/assets # required
    thumbsDir: %wwwDir%/images/thumbs
    baseUrl: http://cdn.example.com # if is your images in another URL 
    basePath: images/thumbs/ # required; adds this path to URL
    errorImage: on # default on; displays error image if when generating an error occurred
    debugger: on # default as debugMode; display information in debug bar; WARNING! For every image send new HEAD request!

Example with extension

Presenter for upload and show images

class ImagerPresenter extends BasePresenter
{
    / @var \Imager\Repository @inject */
    public $repository;

    / @var \Imager\ImageFactory @inject */
    public $factory;

    public function renderDefault($id)
    {
        if (isset($id)) {
            $this->template->imageFromString = $id;
            $this->template->imageFromRepository = $this->repository->fetch($id);
        }
    }


    protected function createComponentUploadForm()
    {
        $control = new Nette\Application\UI\Form;

        $control->addUpload('photo')
            ->setRequired();
        $control->addSubmit('load', 'load image');
        $control->onSuccess[] = $this->uploadFormSucceed;

        return $control;
    }

    public function uploadFormSucceed(Nette\Application\UI\Form $form, $values)
    {
        $upload = $this->factory->createInfo($values->photo->getTemporaryFile());
        $source = $this->repository->save($upload);

        $this->redirect('default', $source->getFilename());
    }
}

Latte template

{block content}
{control uploadForm}
{ifset $image}
    <img n:src="$imageFromRepository, 200, 0"> {set width, origin height}
    <img n:src="$imageFromRepository, 200, 300"> {set width and height}
    <img n:src="$imageFromRepository, null, 300"> {resize by height}
    <img n:src="$imageFromRepository, 200"> {resize by width}
    <img n:src="$imageFromRepository"> {origin width and height}

    {set quality}
    <img n:src="$imageFromRepository, null, null, 25"> {origin width and height, but lowest quality}

    {same parameters as for $imageFromRepository}
    <img n:src="$imageFromString">

{/ifset}

Link to image

If you required create link to image (example to e-mail), then is here method ImageFactory::createLink(). Create a link is only possible when integrating into the Nette.

class EmailPresenter extends BasePresenter
{
    / @var \Imager\ImageFactory @inject */
    public $factory;

    public function actionSendEmail($email)
    {
        // ... defines $images as array with Imager\ImageInfo objects or names of images

        $linksHtml = [];
        foreach ($images as $image) {
          $linksHtml = $this->createImg($this, $image);
        }

        // ... functionality for send e-mail
    }


    private function createImg($image, $width = null, $height = null)
    {
        $link = $this->factory->createLink($this, $image, $width, $height);
        
        $img = Nette\Utils\Html::el('img', ['src' => $link]);

        return $img->render();
    }
}

[Composer]:https://getcomposer.org/ [ImageMagick]:http://www.imagemagick.org/


  Files folder image Files (26)  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagetests (6 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 15%
Total:185
This week:0
All time:8,657
This week:455Up