Decoder.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Intervention\Image\Imagick;
  3. use Intervention\Image\Image;
  4. class Decoder extends \Intervention\Image\AbstractDecoder
  5. {
  6. /**
  7. * Initiates new image from path in filesystem
  8. *
  9. * @param string $path
  10. * @return \Intervention\Image\Image
  11. */
  12. public function initFromPath($path)
  13. {
  14. $core = new \Imagick;
  15. try {
  16. $core->setBackgroundColor(new \ImagickPixel('transparent'));
  17. $core->readImage($path);
  18. $core->setImageType(defined('\Imagick::IMGTYPE_TRUECOLORMATTE') ? \Imagick::IMGTYPE_TRUECOLORMATTE : \Imagick::IMGTYPE_TRUECOLORALPHA);
  19. } catch (\ImagickException $e) {
  20. throw new \Intervention\Image\Exception\NotReadableException(
  21. "Unable to read image from path ({$path}).",
  22. 0,
  23. $e
  24. );
  25. }
  26. // build image
  27. $image = $this->initFromImagick($core);
  28. $image->setFileInfoFromPath($path);
  29. return $image;
  30. }
  31. /**
  32. * Initiates new image from GD resource
  33. *
  34. * @param Resource $resource
  35. * @return \Intervention\Image\Image
  36. */
  37. public function initFromGdResource($resource)
  38. {
  39. throw new \Intervention\Image\Exception\NotSupportedException(
  40. 'Imagick driver is unable to init from GD resource.'
  41. );
  42. }
  43. /**
  44. * Initiates new image from Imagick object
  45. *
  46. * @param Imagick $object
  47. * @return \Intervention\Image\Image
  48. */
  49. public function initFromImagick(\Imagick $object)
  50. {
  51. // currently animations are not supported
  52. // so all images are turned into static
  53. $object = $this->removeAnimation($object);
  54. // reset image orientation
  55. $object->setImageOrientation(\Imagick::ORIENTATION_UNDEFINED);
  56. return new Image(new Driver, $object);
  57. }
  58. /**
  59. * Initiates new image from binary data
  60. *
  61. * @param string $data
  62. * @return \Intervention\Image\Image
  63. */
  64. public function initFromBinary($binary)
  65. {
  66. $core = new \Imagick;
  67. try {
  68. $core->readImageBlob($binary);
  69. } catch (\ImagickException $e) {
  70. throw new \Intervention\Image\Exception\NotReadableException(
  71. "Unable to read image from binary data.",
  72. 0,
  73. $e
  74. );
  75. }
  76. // build image
  77. $image = $this->initFromImagick($core);
  78. $image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary);
  79. return $image;
  80. }
  81. /**
  82. * Turns object into one frame Imagick object
  83. * by removing all frames except first
  84. *
  85. * @param Imagick $object
  86. * @return Imagick
  87. */
  88. private function removeAnimation(\Imagick $object)
  89. {
  90. $imagick = new \Imagick;
  91. foreach ($object as $frame) {
  92. $imagick->addImage($frame->getImage());
  93. break;
  94. }
  95. $object->destroy();
  96. return $imagick;
  97. }
  98. }