PHP code example of superbig / craft3-imgix

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

    

superbig / craft3-imgix example snippets



return [
    // Map your Craft volume handles to Imgix domains
    'imgixDomains' => [
        'myVolumeHandle' => 'my-site.imgix.net',
    ],
];



use craft\helpers\App;

return [
    // imgix API key (/api-keys/new
    // Ensure it has 'Purge' permissions
    'apiKey' => App::env('IMGIX_API_KEY'),

    // Map Craft volume handles to Imgix domains (     'heroImages' => 'my-project.imgix.net/heroes',
        'products' => 'my-shop.imgix.net',
    ],

    // imgix signed URL token (optional, but recommended for security)
    // Generate from: https://dashboard.imgix.com/sources
    // Prevents URL tampering and unauthorized image transformations
    'imgixSignedToken' => App::env('IMGIX_SIGNED_TOKEN'),

    // Lazy load attribute prefix (optional, defaults to 'data-')
    // Used when lazyLoad is enabled to prefix src attributes
    // Set to empty string if using native loading="lazy"
    'lazyLoadPrefix' => 'data-',

    // Prevent upscaling images beyond their original size (optional, defaults to false)
    // When enabled, automatically applies fit=max to prevent upscaling
    // Images smaller than requested dimensions won't be enlarged
    'preventUpscaling' => false,
];



use craft\helpers\App;

return [
    'apiKey' => App::env('IMGIX_API_KEY'),
    
    'imgixDomains' => [
        'uploads' => [
            'domain' => 'my-uploads.imgix.net',
            'signingToken' => App::env('IMGIX_UPLOADS_TOKEN'),  // Optional: secure token for this domain
            'path' => 'assets',                                   // Optional: path prefix (no leading/trailing slashes)
        ],
        'products' => [
            'domain' => 'my-products.imgix.net',
            'signingToken' => App::env('IMGIX_PRODUCTS_TOKEN'),
        ],
        // You can mix string and array formats
        'publicImages' => 'public.imgix.net',
    ],
    
    'lazyLoadPrefix' => 'data-',
];



use craft\helpers\App;

return [
    'apiKey' => App::env('IMGIX_API_KEY'),
    'imgixSignedToken' => App::env('IMGIX_SIGNED_TOKEN'),
    'imgixDomains' => [
        'uploads' => App::env('IMGIX_DOMAIN'),
    ],
];

return [
    'lazyLoadPrefix' => 'lazy-',  # Will use lazy-src, lazy-srcset
    // ...
];



use craft\elements\Entry;
use superbig\imgix\Imgix;

return [
    'endpoints' => [
        'news.json' => [
            'elementType' => Entry::class,
            'criteria' => ['section' => 'news'],
            'transformer' => function(Entry $entry) {
                $asset = $entry->featuredImage->one();
                $featuredImage = Imgix::$plugin->imgixService->transformImage($asset, [
                    'width' => 400,
                    'height' => 350
                ]);
                
                return [
                    'title' => $entry->title,
                    'url' => $entry->url,
                    'featuredImage' => [
                        'url' => $featuredImage->getUrl(),
                        'width' => 400,
                        'height' => 350,
                    ],
                ];
            },
        ],
    ]
];

return [
    'preventUpscaling' => true,
];