PHP code example of lawondyss / imager

1. Go to this page and download the library: Download lawondyss/imager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lawondyss / imager example snippets


// 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();

$factory = new Imager\ImageFactory;

// first argument is ls; 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

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());
    }
}

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();
    }
}