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