PHP code example of thegrommet / image-resizer

1. Go to this page and download the library: Download thegrommet/image-resizer 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/ */

    

thegrommet / image-resizer example snippets


$resizer = new \Grommet\ImageResizer\Resizer('/path/to/images', '/path/to/save');
$newPath = $resizer->resize('in.jpg', 'out.jpg', ['strategy' => 'fit', 'width' => 100]);
// $newPath = '/path/to/save/out.jpg'

$urlGen = new \Grommet\ImageResizer\UrlGenerator('https://cdn.site.com/media');
$url = $urlGen->imageUrl('i/image.jpg', ['strategy' => 'fit', 'width' => 100]);
// $url = 'https://cdn.site.com/media/fit_w-100/i/image.jpg'

$presets = [
    'small' => [
        'width' => 293,
        'height' => 219
    ],
    'large' => [
        'strategy' = 'crop',
        'width' => 500,
        'height' => 500
    ]
];

/* urls */
$urlGen = new \Grommet\ImageResizer\UrlGenerator('https://cdn.site.com/media', $presets);
$url = $urlGen->imageUrl('i/image.jpg', ['size' => 'small']);
// $url = 'https://cdn.site.com/media/fit_w-293_h-219/i/image.jpg'

/* files */
$resizer = new \Grommet\ImageResizer\PresetResizer(
    '/path/to/images',
    '/path/to/save',
    $presets
);
$newPath = $resizer->resize('image.jpg', 'large');
// $newPath = '/path/to/save/crop_w-500_h-500_m-c/image.jpg'

$urlResizer = new \Grommet\ImageResizer\UrlResizer(
    '/path/to/images',
    '/path/to/save',
    'https://cdn.site.com/media'
);

// incoming request for a resized image
$url = 'https://cdn.site.com/media/fit_w-100/i/image.jpg';
$newPath = $urlResizer->resize($url);
// $newPath = '/path/to/save/fit_w-100/i/image.jpg'

header('Content-Type: image/jpeg');
$resource = imagecreatefromjpeg($newPath);
imagejpeg($resource);  // output to browser
imagedestroy($resource);

$resizer = new \Grommet\ImageResizer\Resizer(
    '/path/to/images',
    '/path/to/save',
    'kraken',
    ['api-key', 'api-secret']
);
$newPath = $resizer->resize('in.jpg', 'out.jpg', ['strategy' => 'fit', 'w' => 100, 'h' => 50]);
// $newPath = '/path/to/save/out.jpg'