PHP Classes
elePHPant
Icontem

Lawondyss Imager: Create image thumbnails using ImageMagick

Recommend this page to a friend!
  Info   View files Documentation   View files View files (26)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2016-10-15 (16 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 150 This week: 14All time: 8,159 This week: 64Up
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: 5 packages by
Country: Czech Republic Czech Republic

Details

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  
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

  Files folder image Files  /  src  
File Role Description
Files folder imageImager (6 files, 4 directories)

  Files folder image Files  /  src  /  Imager  
File Role Description
Files folder imageApplication (3 files)
Files folder imageDI (1 file)
Files folder imageLatte (1 file)
Files folder imageTracy (2 files)
  Plain text file exceptions.php Class Class source
  Plain text file Helpers.php Class Class source
  Plain text file Image.php Class Class source
  Plain text file ImageFactory.php Class Class source
  Plain text file ImageInfo.php Class Class source
  Plain text file Repository.php Class Class source

  Files folder image Files  /  src  /  Imager  /  Application  
File Role Description
  Plain text file ImageResponse.php Class Class source
  Plain text file ImageResponse.php Class Class source
  Plain text file Route.php Class Class source

  Files folder image Files  /  src  /  Imager  /  DI  
File Role Description
  Plain text file Extension.php Class Class source

  Files folder image Files  /  src  /  Imager  /  Latte  
File Role Description
  Plain text file Macro.php Class Class source

  Files folder image Files  /  src  /  Imager  /  Tracy  
File Role Description
  Plain text file Panel.php Class Class source
  Plain text file Panel.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageassets (3 files)
Files folder imagetmp (1 file)
  Accessible without login Plain text file bootstrap.php Test Unit test script
  Accessible without login Plain text file Helpers.phpt Data Auxiliary data
  Accessible without login Plain text file Image.phpt Data Auxiliary data
  Accessible without login Plain text file ImageFactory.phpt Data Auxiliary data
  Accessible without login Plain text file ImageInfo.phpt Data Auxiliary data
  Accessible without login Plain text file Repository.phpt Data Auxiliary data

  Files folder image Files  /  tests  /  assets  
File Role Description
  Accessible without login Image file avatar.jpg Data Auxiliary data
  Accessible without login Image file avatar.jpg Data Auxiliary data
  Accessible without login Plain text file non-image.txt Data Auxiliary data

  Files folder image Files  /  tests  /  tmp  
File Role Description
  Accessible without login Plain text file .gitignore Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 15%
Total:150
This week:14
All time:8,159
This week:64Up