PHP code example of tomkirsch / superimage

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

    

tomkirsch / superimage example snippets




namespace Config;

class SuperImage extends \Tomkirsch\SuperImage\SuperImageConfig
{
    public string $sourcePath = WRITEPATH . 'img/'; // where your hires assets live
    public string $cachePath = FCPATH . '_superimage_cache/'; // MUST be in public for direct Apache caching (no PHP overhead)
    public string $servingStrategy = 'htaccess'; // most efficient. falls back to php's readfile()
    public string $publicUrlPrefix = 'img/';
    public string $defaultOutputExt = 'webp';
    public bool $enableCacheBusting = true;
    public string $cacheBustingMethod = 'mtime'; // use mtime so updated images get automatically stale
}

    public static function superImage(?array $options = null, ?SuperImageConfig $config = null): \Tomkirsch\SuperImage\SuperImage
    {
        $config = $config ?? new \Config\SuperImage();
        $instance = new \Tomkirsch\SuperImage\SuperImage($config);
        if (!empty($options)) {
            $instance->load($options);
        }
        return $instance;
    }

    public static function resizer(?SuperImageConfig $config = null): \Tomkirsch\SuperImage\Resizer
    {
        $config = $config ?? new \Config\SuperImage();
        return new \Tomkirsch\SuperImage\Resizer($config);
    }



use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
$routes->get('/', 'Home::index');
$routes->get('img/(.+)', 'Home::serveImage/$1');
// optional - cache management

    /**
     * Serve image request
     */
    public function serveImage(...$path)
    {
        $path = implode("/", $path);
        \Config\Services::resizer()->serve($path);
    }

'src' => 'hero.jpg'               // relative base name with extension
'src' => 'product_1.jpg'          // relative base name with extension
'src' => 'products/photo_1.png'   // relative base name with subdirectory and extension

<?= \Config\Services::superImage()->render([
    'src' => 'hero.jpg',
    'widths' => 'full',
    'loading' => 'eager',
    'fetchPriority' => 'high'
]) 

<?= \Config\Services::superImage()->render([
    'src' => 'product_1.jpg',
    'widths' => 'full',
    'loading' => 'lazy',
    'fetchPriority' => 'low',
]) 

<?= \Config\Services::superImage()->render([
    'src'           => 'hero.jpg',
    'alt'           => 'Hero image',
    'widths'        => 'full',
    'loading'       => 'eager',
    'fetchPriority' => 'high',
]) 

 for ($i = 1; $i <= 3; $i++): 

<?= \Config\Services::superImage()->render([
    'src'     => 'featured.jpg',
    'alt'     => 'Featured article',
    'loading' => 'lazy',
    'widths'  => \Tomkirsch\SuperImage\SuperImageWidths::make()
        ->full()             // 0px+:    100% (mobile)
        ->at(800, 'half')    // 800px+:   50% (tablet)
        ->at(1024, 'third')  // 1024px+:  33% (desktop)
]) 

'widths' => \Tomkirsch\SuperImage\SuperImageWidths::make()
    ->at(0, 1.0)
    ->at(768, 0.6)
    ->at(1200, 0.4),
'gutter' => 24,

 $si = \Config\Services::superImage()->load([
    'widths'  => 'quarter',
    'loading' => 'lazy',
    'gutter'  => 16,
]); 

<?= \Config\Services::superImage()->render([
    'src'     => 'avatar.jpg',
    'alt'     => 'User avatar',
    'static'  => true,
    'widths'  => [100, 200, 300],
    'imgAttr' => ['class' => 'rounded-circle'],
]) 

$url = \Config\Services::superImage()->imgUrl(800, ['src' => 'banner.jpg']);