mambax7/extgallery

View on GitHub
class/pear/Image/Transform/Driver/Imagick.php

Summary

Maintainability
A
25 mins
Test Coverage
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4: */

/**
 * Image Transformation interface using old ImageMagick extension
 *
 * PHP versions 4 and 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @category   Image
 * @package    Image_Transform
 * @author     Peter Bowyer <peter@mapledesign.co.uk>
 * @copyright  2002-2005 The PHP Group
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 * @deprecated
 * @link       http://pear.php.net/package/Image_Transform
 */

/**
 * Include of base class
 */
require_once XOOPS_ROOT_PATH . '/modules/extgallery/class/pear/Image/Image/Transform.php';

/**
 * Image Transformation interface using old ImageMagick extension
 *
 * DEPRECATED: current CVS/release imagick extension should use
 * the Imagick2 driver
 *
 * @deprecated
 */
class Image_Transform_Driver_Imagick extends Image_Transform
{
    /**
     * Handler of the imagick image ressource
     * @var array
     */
    public $imageHandle;
    /**
     * Handler of the image ressource before
     * the last transformation
     * @var array
     */
    public $oldImage;

    public function __construct()
    {
        if (!PEAR::loadExtension('imagick')) {
            return PEAR::raiseError('The imagick extension can not be found.', true);
        }
        require_once __DIR__ . '/Image/Transform/Driver/Imagick/ImageTypes.php';
        //return true;
    }

    // End Image_IM

    /**
     * Load image
     *
     * @param mixed $image
     *
     * @return mixed none or a PEAR error object on error
     * @see PEAR::isError()
     */
    public function load($image)
    {
        $this->imageHandle = imagick_create();
        if (!is_resource($this->imageHandle)) {
            return PEAR::raiseError('Cannot initialize imagick image.', true);
        }

        if (!imagick_read($this->imageHandle, $image)) {
            return PEAR::raiseError('The image file ' . $image . ' does\'t exist', true);
        }
        $this->image = $image;
        $result      = $this->_get_image_details($image);
        if (PEAR::isError($result)) {
            return $result;
        }
    }

    // End load

    /**
     * Resize Action
     *
     * @param mixed $new_x
     * @param mixed $new_y
     *
     * @return none
     * @see PEAR::isError()
     */
    public function _resize($new_x, $new_y)
    {
        $img2 = imagick_copy_resize($this->imageHandle, $new_x, $new_y, IMAGICK_FILTER_CUBIC, 1);
        if ($img2) {
            $this->oldImage    = $this->imageHandle;
            $this->imageHandle = $img2;
            $this->new_x       = $new_x;
            $this->new_y       = $new_y;
        } else {
            return PEAR::raiseError('Cannot create a new imagick imagick image for the resize.', true);
        }
    }

    // End resize

    /**
     * rotate
     * Note: color mask are currently not supported
     *
     * @param float      $angle
     * @param null|mixed $options
     *
     * @return none
     * @see PEAR::isError()
     */
    public function rotate($angle, $options = null)
    {
        $img2 = imagick_copy_rotate($this->imageHandle, $angle);
        if ($img2) {
            $this->oldImage    = $this->imageHandle;
            $this->imageHandle = $img2;
            $this->new_x       = imagick_get_attribute($img2, 'width');
            $this->new_y       = imagick_get_attribute($img2, 'height');
        } else {
            return PEAR::raiseError('Cannot create a new imagick imagick image for the resize.', true);
        }
    }

    // End rotate

    /**
     * addText
     *
     * @param mixed $params
     *
     * @see PEAR::isError()
     */
    public function addText($params)
    {
        $default_params = [
            'text'         => 'This is a Text',
            'x'            => 10,
            'y'            => 20,
            'size'         => 12,
            'color'        => 'red',
            'font'         => 'Arial.ttf',
            'resize_first' => false, // Carry out the scaling of the image before annotation?
        ];
        $params         = array_merge($default_params, $params);
        extract($params);

        $color = is_array($color) ? $this->colorarray2colorhex($color) : mb_strtolower($color);

        imagick_annotate(
            $this->imageHandle,
            [
                'primitive' => "text $x,$y " . $text,
                'pointsize' => $size,
                'antialias' => 0,
                'fill'      => $color,
                'font'      => $font,
            ]
        );
    }

    // End addText

    /**
     * Save the image file
     *
     * @param string $filename the name of the file to write to
     *
     * @param string $type
     * @param int    $quality
     */
    public function save($filename, $type = '', $quality = 75)
    {
        if ('' == $type) {
            $type = mb_strtoupper($type);
            imagick_write($this->imageHandle, $filename, $type);
        } else {
            imagick_write($this->imageHandle, $filename);
        }
        imagick_free($handle);
    }

    // End save

    /**
     * Display image without saving and lose changes
     *
     * @param mixed $type
     * @param mixed $quality
     */
    public function display($type = '', $quality = 75)
    {
        if ('' == $type) {
            header('Content-type: image/' . $this->type);
            if (!imagick_dump($this->imageHandle)) {
            }
        } else {
            header('Content-type: image/' . $type);
            if (!imagick_dump($this->imageHandle, $this->type)) {
            }
        }
        $this->free();
    }

    /**
     * Destroy image handle
     *
     * @return bool
     */
    public function free()
    {
        if (is_resource($this->imageHandle)) {
            imagick_free($this->imageHandle);
        }
        if (is_resource($this->oldImage)) {
            imagick_free($this->oldImage);
        }

        return true;
    }
} // End class ImageIM