ImageMagickPHP

SensioLabsInsight Build Status Coverage Status

An ImageMagick "exec" component for PHP apps.

Installation

Install with Composer, it's the best packages manager you can have :

composer require orbitale/imagemagick-php

Requirements

Settings

There are not many settings, but when you instanciate a new Command object, you may specify ImageMagick's executable directory directly in the constructor, for example :

use Orbitale\Component\ImageMagick\Command;

// Default directory for many Linux distributions:
$command = new Command('/usr/bin');

// Or in Windows, depending of the install directory:
$command = new Command('C:\ImageMagick');

// If it is available in the global scope for the user running the script:
$command = new Command('');

The constructor will automatically search for the convert executable, test it, and throw an exception if it's not available.

/!\ Make sure your ImageMagick executables have the "+x" chmod option, and that the user has the rights to execute it.

Usage

Basic image converter with ImageMagick's basic logo

Read the comments :

require_once 'vendor/autoload.php'; // A classic one, if you know how Composer works.

use Orbitale\Component\ImageMagick\Command;

// Create a new command
$command = new Command();

$response = $command
    // The command will search for the "logo.png" file. If it does not exist, it will throw an exception.
    // If it does, it will create a new command with this source image.
    ->convert('logo.png')

    // The "file()" method will simply add a file name to the command.
    // For this one, it will be used as an output file name,
    //  that's why we set the 2nd argument "checkExistence" to "false",
    //  so the Command will not check if the file exists (because it would throw an exception then)
    ->file('logo.gif', false)

    // At this time, the command shall look like this :
    // $ "{ImageMagickPath}convert" "logo.png" "logo.gif"

    // Then we run the command by using "exec()" to get the CommandResponse
    ->run();

if (!$response->hasFailed()) {
    // If it has not failed, then we simply send it to the buffer
    header('Content-type: image/gif');
    echo file_get_contents('logo.gif');
}

Simply resizing an image

require_once 'vendor/autoload.php';

use Orbitale\Component\ImageMagick\Command;

// Create a new command
$command = new Command();

$response = $command

    // Here we are using "mogrify", so the file must exist as it is overwritten (it's basically the difference between "convert" and "mogrify")
    ->mogrify('background.jpeg')

    // The "resize" method allows you to add a "Geometry" operation.
    // It must fit to the "Geometry" parameters in the ImageMagick official documentation (see links below)
    ->resize('50x50')

    ->run()
;

if (!$response->hasFailed()) {
    header('Content-type: image/jpeg');
    echo file_get_contents('background.jpeg');
}

Useful links