PHP code example of mehr-it / levi-images

1. Go to this page and download the library: Download mehr-it/levi-images 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/ */

    

mehr-it / levi-images example snippets


LeviImages::raster()

// open a file 
$im = LeviImages::raster()->open('path/to/file.png');

// load a string 
$im = LeviImages::raster()->load(/* image data as string */);

// read from a resource
$im = LeviImages::raster()->load(fopen('path/to/file.png', 'r'));    

// levi-images.php
return [
     'raster' => [
         /*
          * Configures the raster image driver to use. Available options are "vips", "imagick", "gmagick" and "gd". If
          * "auto" is set, the first available driver is chosen.
          */
         'driver' => 'vips',
     ],
];    
   

$size    = new Box(100, 100);
$bgColor = (new RGB())->color("ff0000", 1); // red

$im = LeviImages::raster()->create($size, $bgColor);  

$rasterImage = LeviImages::raster()->open('path/to/file.png');
$vectorImage = LeviImages::vector()->open('path/to/file.svg');

$imported = LeviImages::raster()->import($vectorImage); 

$rasterImage->paste($imported, new Point(0, 0)) 

LeviImages::vector()

// open a file 
$im = LeviImages::raster()->open('path/to/file.svg');

// load a string 
$im = LeviImages::raster()->load(/* image data as string */);

// read from a resource
$im = LeviImages::raster()->load(fopen('path/to/file.svg', 'r'));    

LeviImages::optimizer()

// optimize file in place
LeviImages::optimizer()
    ->optimizeFile('path/to/file.png');

// create a new optimized image file
LeviImages::optimizer()
    ->optimizeFile('path/to/file.png', 'path/to/output.png');

$res = LeviImages::optimizer()
    ->optimizeResource(fopen('path/to/file.png', 'r'));

$res = LeviImages::optimizer()
    ->optimizeImage($img, Optimizer::FORMAT_PNG);

LeviImages::optimizer()
    ->setOptimizersResolver(function() {
    
        return [
            new Jpegoptim([
                '--max=85',
                '--strip-all',
                '--all-progressive',
            ]),
            return new Cwebp([
                '-m 6',
                '-pass 10',
                '-mt',
                '-q 80',
            ]); 
        ];    
    });

LeviImages::optimizer()
    ->useOptimizers([
        new Jpegoptim([
                '--max=90',
            ]),
       ]
   );
bash
php ./artisan levi-images:test
bash
   sudo apt-get install php-pear