PHP code example of midnight / image

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

    

midnight / image example snippets


$image = \Midnight\Image\Image::open('path/to/image.jpg');

$image->fit(array('width' => 150, 'height' => 200));

$image->getFile();

$options = array('foo' => true, 'bar' => 42);
$filtered_image = $image->filterName($options);

// $image has a size of 1500 by 2000 pixels
$thumbnail = $image->fit(array('width' => 200, 'height' => 200));
// $thumbnail is 150 x 200 pixels 

// $image has a size of 1500 by 2000 pixels
$thumbnail = $image->fill(array('width' => 200, 'height' => 200));
// $thumbnail is 200 x 200 pixels, with cropped areas at the top and bottom

$brightened = $image->brighten(array('amount' => 50));

$desaturated = $image->desaturate(array('amount' => 25));

// Apply a red color cast.
$red = new \Midnight\Color\Color(255, 0, 0);
$colorcasted = $image->colorcast(array('color' => $red, 'amount' => .2));

$noisy = $image->noise(array('amount' => .1));

class MyCustomFilter extends \Midnight\Image\Filter\AbstractGdFilter
{
    public function filter($image)
    {
        parent::filter($image);

        $cache = $this->getCache();
        if ($cache->exists($this)) {
            return Image::open($cache->getPath($this));
        }

        $im = $this->getGdImage();

        // Do stuff to GD image resource $im

        $image = $this->save($im);

        return $image;
    }
}

$plugin_manager = \Midnight\Image\ImagePluginManager::getInstance();
$plugin_manager->setService('myCustomFilter', 'MyCustomFilter');

// Use the filter
$filtered = \Midnight\Image\Image::open('file.jpg')->myCustomFilter();