source: extensions/external_ImageMagick/trunk/imagick.class.php @ 9852

Last change on this file since 9852 was 9852, checked in by patdenice, 13 years ago

Add external ImageMagick plugin.

File size: 3.8 KB
Line 
1<?php
2
3class Imagick
4{
5  private var $source_path = '';
6  private var $tmp_path = '';
7  private var $image_data = array();
8  private var $imagemagickdir = '';
9  private var $commands = array();
10
11  const INTERLACE_NO = 'none';
12  const INTERLACE_LINE = 'line';
13  const INTERLACE_PLANE = 'plane';
14  const INTERLACE_PARTITION = 'partition';
15  const INTERLACE_GIF = 'GIF';
16  const INTERLACE_JPEG = 'JPEG';
17  const INTERLACE_PNG = 'PNG';
18
19  const FILTER_POINT = 'Point';
20  const FILTER_BOX = 'Box';
21  const FILTER_TRIANGLE = 'Triangle';
22  const FILTER_HERMITE = 'Hermite';
23  const FILTER_HANNING = 'Hanning';
24  const FILTER_HAMMING = 'Hamming';
25  const FILTER_BLACKMAN = 'Blackman';
26  const FILTER_GAUSSIAN = 'Gaussian';
27  const FILTER_QUADRATIC = 'Quadratic';
28  const FILTER_CUBIC = 'Cubic';
29  const FILTER_CATROM = 'Catrom';
30  const FILTER_MITCHELL = 'Mitchell';
31  const FILTER_LANCZOS = 'Lanczos';
32  const FILTER_BESSEL = 'Bessel';
33  const FILTER_SINC = 'Sinc';
34
35  const ORIENTATION_TOPLEFT = 'top-left';
36  const ORIENTATION_TOPRIGHT = 'top-right';
37  const ORIENTATION_BOTTOMRIGHT = 'bottom-right';
38  const ORIENTATION_BOTTOMLEFT = 'bottom-left';
39  const ORIENTATION_LEFTTOP = 'left-top';
40  const ORIENTATION_RIGHTTOP = 'right-top';
41  const ORIENTATION_RIGHTBOTTOM = 'right-bottom';
42  const ORIENTATION_LEFTBOTTOM = 'left-bottom';
43
44  function __construct($source)
45  {
46    global $conf;
47
48    $this->source_path = $source;
49
50    if (isset($conf['imagick_dir']))
51      $this->imagemagickdir = rtrim($conf['imagick_dir'], ' /\\').(PHP_OS == 'WINNT' ? '\\' : '/');
52
53    $this->tmp_path = @tempnam($conf['local_data_dir'], get_extension($this->source_path));
54
55    $command = $this->imagemagickdir."identify -verbose ".realpath($this->source_path);
56    @exec($command, $returnarray, $returnvalue);
57    if($returnvalue)
58    {
59      die("ImageMagick: Corrupt image");
60    }
61    else
62    {
63      foreach($returnarray as $value)
64      {
65        $arr = explode(':', $value, 2);
66        if (count($arr) == 2)
67        {
68          $this->image_data[trim($arr[0])] = trim($arr[1]);
69        }
70      }
71    }
72  }
73
74  function add_command($command, $params=null)
75  {
76    $this->commands[$command] = $params;
77  }
78
79  function getImageWidth()
80  {
81    preg_match('#^(\d+)x#', $this->image_data['Geometry'], $match);
82    return isset($match[1]) ? $match[1] : false;
83  }
84
85  function getImageHeight()
86  {
87    preg_match('#^\d+x(\d+)(?:\+|$)#', $this->image_data['Geometry'], $match);
88    return isset($match[1]) ? $match[1] : false;
89  }
90
91  function setImageCompressionQuality($quality)
92  {
93    $this->add_command('quality', $quality);
94  }
95
96  function setInterlaceScheme($interlace)
97  {
98    $this->add_command('interlace', $interlace);
99  }
100
101  function resizeImage($width, $height, $filter, $blur)
102  {
103    $this->add_command('resize', $width.'x'.$height.'!');
104    $this->add_command('filter', $filter);
105    //$this->add_command('blur', $blur);
106  }
107
108  function stripImage()
109  {
110    $this->add_command('strip');
111  }
112
113  function rotateImage($background, $rotate)
114  {
115    $this->add_command('rotate', $rotate);
116  }
117
118  function setImageOrientation($orientation)
119  {
120    $this->add_command('orient', $orientation);
121  }
122
123  function cropImage($width, $height, $x, $y)
124  {
125    $this->add_command('crop', $width.'x'.$height.'+'.$x.'+'.$y);
126  }
127
128  function writeImage($destination_filepath)
129  {
130    global $conf;
131
132    $exec = $this->imagemagickdir.'convert';
133
134    foreach ($this->commands as $command => $params)
135    {
136      $exec .= ' -'.$command;
137      if (!empty($params))
138      {
139        $exec .= ' '.$params;
140      }
141    }
142
143    $exec .= ' '.realpath($this->source_path);
144    $exec .= ' '.realpath($this->tmp_path);
145    @exec($exec, $returnarray, $returnvalue);
146    @rename($this->tmp_path, $destination_filepath);
147  }
148
149  function destroy()
150  {
151    @unlink($this->tmp_path);
152    return true;
153  }
154}
155
156class ImagickPixel { }
157
158?>
Note: See TracBrowser for help on using the repository browser.