PageRenderTime 226ms CodeModel.GetById 83ms app.highlight 18ms RepoModel.GetById 123ms app.codeStats 0ms

/plugins/sfImageTransformPlugin/lib/adapters/sfImageTransformImageMagickAdapter.class.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 310 lines | 140 code | 39 blank | 131 comment | 14 complexity | 549160fca5b49754eae50073099e7587 MD5 | raw file
  1<?php
  2/*
  3 * This file is part of the sfImageTransform package.
  4 * (c) 2007 Stuart Lowes <stuart.lowes@gmail.com>
  5 *
  6 * For the full copyright and license information, please view the LICENSE
  7 * file that was distributed with this source code.
  8 */
  9
 10/**
 11 *
 12 * sfImageTransformImagickAdapter class.
 13 *
 14 * ImageMagick support for sfImageTransform.
 15 *
 16 *
 17 * @package sfImageTransform
 18 * @author Stuart Lowes <stuart.lowes@gmail.com>
 19 * @version SVN: $Id$
 20 */
 21class sfImageTransformImageMagickAdapter extends sfImageTransformAdapterAbstract
 22{
 23  /**
 24   * The image resource.
 25   * @access protected
 26   * @var resource
 27   *
 28   * @throws sfImageTransformException
 29  */
 30  protected $holder;
 31
 32  /*
 33   * Supported MIME types for the sfImageImageMagickAdapter
 34   * and their associated file extensions
 35   * @var array
 36   */
 37  protected $types = array(
 38    'image/jpeg' => array('jpeg','jpg'),
 39    'image/gif' => array('gif'),
 40    'image/png' => array('png')
 41  );
 42
 43  /**
 44   * Initialize the object. Check for imagick extension. An exception is thrown if not installed
 45   *
 46   * @throws sfImageTransformException
 47   */
 48  public function __construct()
 49  {
 50    // Check that the GD extension is installed and configured
 51    if (!extension_loaded('imagick'))
 52    {
 53      throw new sfImageTransformException('The image processing library ImageMagick is not enabled. See PHP Manual for installation instructions.');
 54    }
 55
 56    $this->setHolder(new Imagick());
 57  }
 58
 59  /**
 60   * Tidy up the object
 61  */
 62  public function __destruct()
 63  {
 64    if ($this->hasHolder())
 65    {
 66      $this->getHolder()->destroy();
 67    }
 68  }
 69
 70  /**
 71   * Create a new empty (1 x 1 px) gd true colour image
 72   *
 73   * @param integer Image width
 74   * @param integer Image Height
 75   */
 76  public function create($x=1, $y=1)
 77  {
 78    $image = new Imagick();
 79    $image->newImage($x, $y, new ImagickPixel('white'));
 80    $image->setImageFormat('png');
 81    $this->setHolder($image);
 82  }
 83
 84  /**
 85   * Load and sets the resource from a existing file
 86   *
 87   * @param string
 88   * @return boolean
 89   *
 90   * @throws sfImageTransformException
 91   */
 92  public function load($filename, $mime)
 93  {
 94    if (preg_match('/image\/.+/',$mime))
 95    {
 96      $this->holder = new Imagick($filename);
 97      $this->mime_type = $mime;
 98      $this->setFilename($filename);
 99
100      return true;
101    }
102
103    throw new sfImageTransformException(sprintf('Cannot load file %s as %s is an unsupported file type.', $filename, $mime));
104  }
105
106  /**
107   * Load and sets the resource from a string
108   *
109   * @param string
110   * @return boolean
111   *
112   * @throws sfImageTransformException
113   */
114  
115   public function loadString($string)
116	{
117    return $this->getHolder()->readImageBlob($string);;
118	 }
119 
120  /**
121   * Get the image as string
122   *
123   * @return string
124   */
125  public function __toString()
126  {
127    $this->getHolder()->setImageCompressionQuality($this->getQuality());
128
129    return (string)$this->getHolder();
130  }
131
132  /**
133   * Save the image to disk
134   *
135   * @return boolean
136   */
137  public function save()
138  {
139    $this->getHolder()->setImageCompressionQuality($this->getQuality());
140
141    return $this->getHolder()->writeImage($this->getFilename());
142  }
143
144  /**
145   * Save the image to the specified file
146   *
147   * @param string Filename
148   * @param string MIME type
149   * @return boolean
150   */
151  public function saveAs($filename, $mime='')
152  {
153    if ('' !== $mime)
154    {
155      $this->setMimeType($mime);
156    }
157
158    $this->getHolder()->setImageCompressionQuality($this->getQuality());
159
160    return $this->getHolder()->writeImage($filename);
161  }
162
163  /**
164   * Returns a copy of the adapter object
165   *
166   * @return sfImage
167   */
168  public function copy()
169  {
170    $copyObj = clone $this;
171
172    $copyObj->setHolder($this->getHolder()->clone());
173
174    return $copyObj;
175  }
176
177  /**
178   * Gets the pixel width of the image
179   *
180   * @return integer
181   */
182  public function getWidth()
183  {
184    if ($this->hasHolder())
185    {
186      return $this->getHolder()->getImageWidth();
187    }
188
189    return 0;
190  }
191
192  /**
193   * Gets the pixel height of the image
194   *
195   * @return integer
196   */
197  public function getHeight()
198  {
199    if ($this->hasHolder())
200    {
201      return $this->getHolder()->getImageHeight();
202    }
203
204    return 0;
205  }
206
207  /**
208   * Sets the image resource holder
209   * @param Imagick resource object
210   * @return boolean
211   *
212   */
213  public function setHolder($holder)
214  {
215    if (is_object($holder) && 'Imagick' === get_class($holder))
216    {
217      $this->holder = $holder;
218      return true;
219    }
220
221    return false;
222  }
223
224  /**
225   * Returns the image resource
226   * @return resource
227   *
228   */
229  public function getHolder()
230  {
231    if ($this->hasHolder())
232    {
233      return $this->holder;
234    }
235
236    return false;
237  }
238
239  /**
240   * Returns whether there is a valid GD image resource
241   * @return boolean
242   *
243   */
244  public function hasHolder()
245  {
246    if (is_object($this->holder) && 'Imagick' === get_class($this->holder))
247    {
248      return true;
249    }
250
251    return false;
252  }
253
254  /**
255   * Returns the supported MIME types
256   * @return array
257   *
258   */
259  public function getMimeType()
260  {
261    return $this->mime_type;
262  }
263
264 /**
265   * Returns image MIME type
266   * @param string valid MIME Type
267   * @return boolean
268   *
269   */
270  public function setMimeType($mime)
271  {
272    $this->mime_type = $mime;
273    if ($this->hasHolder() && isset($this->types[$mime]))
274    {
275      $this->getHolder()->setImageFormat($this->types[$mime][0]);
276
277      return true;
278    }
279
280    return false;
281  }
282
283  /**
284   * Returns the name of the adapter
285   * @return string
286   *
287   */
288  public function getAdapterName()
289  {
290    return 'ImageMagick';
291  }
292
293  /**
294   * Sets the image filename
295   * @param integer Quality of the image
296   *
297   * @return boolean
298   */
299  public function setQuality($quality)
300  {
301    if (parent::setQuality($quality))
302    {
303      $this->getHolder()->setImageCompressionQuality($quality);
304
305      return true;
306    }
307
308    return false;
309  }
310}