PHP code example of io-developer / php-thumbnails

1. Go to this page and download the library: Download io-developer/php-thumbnails 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/ */

    

io-developer / php-thumbnails example snippets




class MyImageType
{
    const ORIGINAL = "";
    const FULLSIZE = "fullsize";
    const WIDE = "wide";
    const SQUARE = "square";
}




use iodev\Lib\Thumbnails\ThumbnailBuilder;
use iodev\Lib\Thumbnails\Thumbnailer;
use iodev\Lib\Thumbnails\ThumbnailerBuilder;

class MyImageFactory
{
    /**
     * @return Thumbnailer
     */
    public static function createThumbnailer()
    {
        return ThumbnailerBuilder::create()
            ->setCommonResolver($_SERVER["DOCUMENT_ROOT"], "/files/content-by-id")
            ->setPrimaryThumbnailName(MyImageType::ORIGINAL)
            ->addThumbnail(
                ThumbnailBuilder::create()
                    ->name(MyImageType::ORIGINAL)
                    ->size(4096, 4096)
                    ->modeContain()
                    ->formatSource()
                    ->toThumbnail()
            )
            ->addThumbnail(
                ThumbnailBuilder::create()
                    ->name(MyImageType::FULLSIZE)
                    ->size(4096, 4096)
                    ->modeContain()
                    ->formatSource()
                    ->watermarkAdaptive("watermark.png")
                    ->toThumbnail()
            )
            ->addThumbnail(
                ThumbnailBuilder::create()
                    ->name(MyImageType::WIDE)
                    ->size(1280, 720)
                    ->modeCover()
                    ->formatJpeg(95)
                    ->watermarkAdaptive("watermark.png")
                    ->toThumbnail()
            )
            ->addThumbnail(
                ThumbnailBuilder::create()
                    ->name(MyImageType::SQUARE)
                    ->size(150, 150)
                    ->modeCover()
                    ->formatPng()
                    ->toThumbnail()
            )
            ->toThumbnailer();
    }
}



use iodev\Lib\Thumbnails\Thumbnailer;

class MyImage
{
    /** @var Thumbnailer */
    private static $_thumbnailer = null;
    
    /**
     * @return Thumbnailer
     */
    public static function getThumbnailer()
    {
        if (self::$_thumbnailer == null) {
            self::$_thumbnailer = MyImageFactory::createThumbnailer();
        }
        return self::$_thumbnailer;
    }
    
    
    /**
     * @param string $path
     */
    public function __construct( $path )
    {
        $m = new MyImage();
        $m->path = $path;
        return $m;
    }
    
    /** @var string */
    public $path = "";
    
    /**
     * @return string
     */
    public function toUri( $imageType="" )
    {
        return self::getThumbnailer()
            ->resolveLocationByName($this->path, $imageType);
    }
    
    /**
     * @return string
     */
    public function toFullsizeUri()
    {
        return $this->toUri(MyImageType::FULLSIZE);
    }
    
    /**
     * @return string
     */
    public function toWideUri()
    {
        return $this->toUri(MyImageType::WIDE);
    }
    
    /**
     * @return string
     */
    public function toSquareUri()
    {
        return $this->toUri(MyImageType::SQUARE);
    }
}



// For example: you need to thumbnail main image of some article. So content ID it's article ID :)
$contentId = 123;
$inputName = "image-file-form-input-name";
$areaSet = null;
$overlayWatermarks = true;

$thumbnailer = MyImage::getThumbnailer();
$result = $thumbnailer->thumbnailFormInput($contentId, $inputName, $areaSet, $overlayWatermarks);

// thumbnailed image path to store to somewhere
$path = $result->getPrimaryPath();

$myThumbnailedImage = new MyImage($path);




$contentId = 123;
$oldPath = "previous-generated-thumbnail-path.jpg";
$areaSet = null;
$overlayWatermarks = false;

$thumbnailer = MyImage::getThumbnailer();
$result = $thumbnailer->reThumbnailPath($contentId, $oldPath, $areaSet, $overlayWatermarks);

// thumbnailed image path to save
$newPath = $result->getPrimaryPath();

$myThumbnailedImage = new MyImage($newPath);




$image = new MyImage("saved-thumbnailed-path.jpg");

// original
echo $image->toUri();

// fullsized watermarked
echo $image->toFullsizeUri();

// wide watermarked
echo $image->toWideUri();

// square
echo $image->toSquareUri();