File indexing completed on 2025-01-26 05:29:15

0001 <?php
0002 
0003 namespace Intervention\Image\Imagick;
0004 
0005 use Intervention\Image\Image;
0006 
0007 class Decoder extends \Intervention\Image\AbstractDecoder
0008 {
0009     /**
0010      * Initiates new image from path in filesystem
0011      *
0012      * @param  string $path
0013      * @return \Intervention\Image\Image
0014      */
0015     public function initFromPath($path)
0016     {
0017         $core = new \Imagick;
0018 
0019         try {
0020 
0021             $core->setBackgroundColor(new \ImagickPixel('transparent'));
0022             $core->readImage($path);
0023             $core->setImageType(defined('\Imagick::IMGTYPE_TRUECOLORALPHA') ? \Imagick::IMGTYPE_TRUECOLORALPHA : \Imagick::IMGTYPE_TRUECOLORMATTE);
0024 
0025         } catch (\ImagickException $e) {
0026             throw new \Intervention\Image\Exception\NotReadableException(
0027                 "Unable to read image from path ({$path}).",
0028                 0,
0029                 $e
0030             );
0031         }
0032 
0033         // build image
0034         $image = $this->initFromImagick($core);
0035         $image->setFileInfoFromPath($path);
0036 
0037         return $image;
0038     }
0039 
0040     /**
0041      * Initiates new image from GD resource
0042      *
0043      * @param  Resource $resource
0044      * @return \Intervention\Image\Image
0045      */
0046     public function initFromGdResource($resource)
0047     {
0048         throw new \Intervention\Image\Exception\NotSupportedException(
0049             'Imagick driver is unable to init from GD resource.'
0050         );
0051     }
0052 
0053     /**
0054      * Initiates new image from Imagick object
0055      *
0056      * @param  Imagick $object
0057      * @return \Intervention\Image\Image
0058      */
0059     public function initFromImagick(\Imagick $object)
0060     {
0061         // currently animations are not supported
0062         // so all images are turned into static
0063         $object = $this->removeAnimation($object);
0064 
0065         // reset image orientation
0066         $object->setImageOrientation(\Imagick::ORIENTATION_UNDEFINED);
0067 
0068         return new Image(new Driver, $object);
0069     }
0070 
0071     /**
0072      * Initiates new image from binary data
0073      *
0074      * @param  string $data
0075      * @return \Intervention\Image\Image
0076      */
0077     public function initFromBinary($binary)
0078     {
0079         $core = new \Imagick;
0080 
0081         try {
0082             $core->setBackgroundColor(new \ImagickPixel('transparent'));
0083 
0084             $core->readImageBlob($binary);
0085 
0086         } catch (\ImagickException $e) {
0087             throw new \Intervention\Image\Exception\NotReadableException(
0088                 "Unable to read image from binary data.",
0089                 0,
0090                 $e
0091             );
0092         }
0093 
0094         // build image
0095         $image = $this->initFromImagick($core);
0096         $image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary);
0097 
0098         return $image;
0099     }
0100 
0101     /**
0102      * Turns object into one frame Imagick object
0103      * by removing all frames except first
0104      *
0105      * @param  Imagick $object
0106      * @return Imagick
0107      */
0108     private function removeAnimation(\Imagick $object)
0109     {
0110         $imagick = new \Imagick;
0111 
0112         foreach ($object as $frame) {
0113             $imagick->addImage($frame->getImage());
0114             break;
0115         }
0116 
0117         $object->destroy();
0118 
0119         return $imagick;
0120     }
0121 }