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

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

Do not use temp file

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