EGOCMS  18.0
EGOTEC Content-Managament-System
Ego_Image.php
gehe zur Dokumentation dieser Datei
1 <?php
13 class Ego_Image {
19  const EXIF_ALL = 0;
20  const EXIF_COPYRIGHT = 1;
21 
27  public $image = '';
28 
34  private $imagick = null;
35 
41  public function __construct($file = '') {
42  if ($file) {
43  $this->load($file);
44  }
45  }
46 
50  public function __destruct() {
51  $this->free();
52  }
53 
60  public function load($file) {
61  $this->free();
62  $this->imagick = new Imagick($file);
63  $this->image = $this->imagick->getImageFilename();
64 
65  $this->imagick->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
66  }
67 
76  public function save($file, $type = '', $quality = 100) {
77  if ($type == 'jpg' || $type == 'jpeg') {
78  $this->imagick->setImageCompressionQuality($quality);
79  }
80  return $this->imagick->writeImages($file, true);
81  }
82 
88  public function free() {
89  if ($this->imagick) {
90  $this->imagick->clear();
91  }
92  }
93 
102  private function modify($action, $params = array()) {
110  $resize = function(Imagick &$image) use ($params) {
111  $image->scaleImage($params['width'], $params['height'], $params['width'] && $params['height']);
112  };
113 
120  $crop = function(Imagick &$image) use ($params) {
121  $image->cropImage($params['width'], $params['height'], $params['x'], $params['y']);
122  $image->setImagePage($params['width'], $params['height'], 0, 0);
123  };
124 
131  $rotate = function(Imagick &$image) use ($params) {
132  $image->rotateImage(new ImagickPixel('#00000000'), $params['deg']);
133  };
134 
141  $mirror = function(Imagick &$image) use ($params) {
142  if ($params['horizontal']) {
143  $image->flopImage();
144  }
145  if ($params['vertical']) {
146  $image->flipImage();
147  }
148  };
149 
150  if (isset($$action)) {
151  if ($this->imagick->getNumberImages() > 1) {
152  // Bei einer Animation muss die Aktion für jedes Frame ausgeführt werden
153  $image = $this->imagick->coalesceImages();
154  foreach ($image as $frame) {
155  $$action($frame);
156  }
157  $this->imagick = $image->optimizeImageLayers();
158  } else {
159  $$action($this->imagick);
160  }
161  } else {
162  throw new Exception('Invalid modify action.');
163  }
164  }
165 
176  public function crop($x1, $y1, $x2, $y2) {
177  $this->modify('crop', array(
178  'width' => $x2 - $x1,
179  'height' => $y2 - $y1,
180  'x' => $x1,
181  'y' => $y1
182  ));
183  }
184 
192  public function rotate($deg) {
193  $this->modify('rotate', array(
194  'deg' => $deg
195  ));
196  }
197 
206  public function mirror($vertical, $horizontal) {
207  $this->modify('mirror', array(
208  'vertical' => $vertical,
209  'horizontal' => $horizontal
210  ));
211  }
212 
221  public function resize($width, $height) {
222  $this->modify('resize', array(
223  'width' => $width,
224  'height' => $height
225  ));
226  }
227 
235  public function scaleByX($width) {
236  $this->resize($width, 0);
237  }
238 
246  public function scaleByY($height) {
247  $this->resize(0, $height);
248  }
249 
257  public function watermark($file) {
258  $watermark = new Imagick($file);
259 
260  // Bei Bedarf das Wasserzeichen skalieren
261  $image_w = $this->imagick->getImageWidth();
262  $image_h = $this->imagick->getImageHeight();
263  $watermark_w = $watermark->getImageWidth();
264  $watermark_h = $watermark->getImageHeight();
265 
266  if ($image_w < $watermark_w || $image_h < $watermark_h) {
267  $watermark->scaleImage($image_w, 0);
268  $watermark_w = $watermark->getImageWidth();
269  $watermark_h = $watermark->getImageHeight();
270  if ($image_h < $watermark_h) {
271  $watermark->scaleImage(0, $image_h);
272  $watermark_w = $watermark->getImageWidth();
273  $watermark_h = $watermark->getImageHeight();
274  }
275  }
276 
277  $x = floor(($image_w - $watermark_w) / 2);
278  $y = floor(($image_h - $watermark_h) / 2);
279 
280  $this->imagick->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
281  }
282 
291  public function thumbnail($width, $height, $params = []) {
292  $type = $params['type'] ?? 'png';
293 
294  $this->imagick->setImageFormat($type);
295  $this->imagick->scaleImage($width, $height ? $height : $width, true);
296  $this->imagick->setImageAlphaChannel(Imagick::VIRTUALPIXELMETHOD_WHITE);
297 
298  $file = tempnam($GLOBALS['egotec_conf']['bin_dir'] . 'tmp', 'thumbnail');
299  if ($this->save($file, $type, $params['quality'] ?? $GLOBALS['egotec_conf']['image']['quality'] ?? 100)) {
300  return $file;
301  }
302  return null;
303  }
304 
314  public function getDiffImage($file) {
315  try {
316  $result = $this->imagick->compareImages(new Imagick($file), Imagick::METRIC_MEANSQUAREERROR);
317  return 'data:' . $result[0]->getImageMimeType() . ';base64,' . base64_encode($result[0]->getImageBlob());
318  } catch (Exception $e) {
319  return null;
320  }
321  }
322 
328  public function getImageWidth() {
329  return $this->imagick->getImageWidth();
330  }
331 
337  public function getImageHeight() {
338  return $this->imagick->getImageHeight();
339  }
340 
346  public function getImageType() {
347  $data = @getimagesize($this->image);
348  if (is_array($data)){
349  switch($data[2]) {
350  case 1:
351  return 'gif';
352  case 2:
353  return 'jpeg';
354  case 3:
355  return 'png';
356  case 4:
357  return 'swf';
358  case 5:
359  return 'psd';
360  case 6:
361  return 'bmp';
362  case 7:
363  case 8:
364  return 'tiff';
365  }
366  }
367  return '';
368  }
369 
375  public function getMimeType() {
376  return $this->imagick->getImageMimeType();
377  }
378 
385  public function getExif($entry = self::EXIF_ALL) {
386  if (
387  function_exists('exif_read_data')
388  && ($data = @exif_read_data($this->imagick->getImageFilename())) !== false
389  ) {
390  switch ($entry) {
391  case self::EXIF_ALL:
392  return $data;
393  case self::EXIF_COPYRIGHT:
394  $value = trim($data['Copyright'] ?? $data['COMPUTED']['Copyright'] ?? $data['Artist']);
395  if (empty($value) && isset($data['COMMENT'])) {
396  if (is_array($data['COMMENT'])) {
397  $value = implode($data['COMMENT'], ', ');
398  } else {
399  $value = $data['COMMENT'];
400  }
401  }
402  return trim(str_replace('©', '', $value));
403  }
404  }
405  return null;
406  }
407 }
408 ?>
scaleByX($width)
Definition: Ego_Image.php:235
getImageHeight()
Definition: Ego_Image.php:337
crop($x1, $y1, $x2, $y2)
Definition: Ego_Image.php:176
__destruct()
Definition: Ego_Image.php:50
watermark($file)
Definition: Ego_Image.php:257
getImageWidth()
Definition: Ego_Image.php:328
save($file, $type='', $quality=100)
Definition: Ego_Image.php:76
getImageType()
Definition: Ego_Image.php:346
thumbnail($width, $height, $params=[])
Definition: Ego_Image.php:291
__construct($file='')
Definition: Ego_Image.php:41
rotate($deg)
Definition: Ego_Image.php:192
getExif($entry=self::EXIF_ALL)
Definition: Ego_Image.php:385
const EXIF_ALL
Definition: Ego_Image.php:19
mirror($vertical, $horizontal)
Definition: Ego_Image.php:206
const EXIF_COPYRIGHT
Definition: Ego_Image.php:20
load($file)
Definition: Ego_Image.php:60
resize($width, $height)
Definition: Ego_Image.php:221
getDiffImage($file)
Definition: Ego_Image.php:314
scaleByY($height)
Definition: Ego_Image.php:246