PHP code example of ngfw / webpconverter

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

    

ngfw / webpconverter example snippets



return [

    /*
    * Default Image Processing Driver
    */
    'driver' => env('WEBP_CONVERTER_DRIVER', 'gd'),

    /*
    * Default WebP Quality
    */
    'quality' => env('WEBP_CONVERTER_QUALITY', 80),

    /*
    * Storage Path
    */

    'storage_path' => env('WEBP_CONVERTER_STORAGE_PATH', 'public/webp_images'),
];

use Ngfw\WebpConverter\WebpConverterFacade as WebpConverter;

// Load a local image
// WebpConverter::load('/path/to/image.jpg');

// Load a remote image
$imgUrl = "https://images.unsplash.com/photo-1724169913051-49f6ff76a070?q=80&w=3570&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D";

$image = WebpConverter::load($imgUrl)->convert();
// /storage/webp_images/photo-1724169913051-49f6ff76a070.webp

$thumbnailUrl = WebpConverter::load($imgUrl)
        ->width(200)
        ->quality(70) // Set the quality for optimization
        ->optimize() // Apply optimization
        ->saveAs('optimized_thumbnail')
        ->convert();
// /storage/webp_images/optimized_thumbnail.webp

$converter->quality(80); // Set quality to 80%

$converter->width(300)->height(200); // Resize to 300x200

$converter->optimize();


$webpUrl = $converter->saveAs('optimized_image')->convert();
echo $webpUrl; // Outputs the URL to the converted WebP image


$webpUrl = $converter->load('/path/to/image.jpg')
                     ->quality(90)
                     ->width(500)
                     ->height(300)
                     ->optimize()
                     ->saveAs('final_image')
                     ->convert();
echo $webpUrl; // Outputs the URL to the optimized WebP image

$content = $converter->serve(true); // Serve the image as a response array
bash
php artisan vendor:publish --tag="webp_converter"