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

0001 <?php
0002 
0003 namespace Intervention\Image\Gd;
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         if ( ! file_exists($path)) {
0018             throw new \Intervention\Image\Exception\NotReadableException(
0019                 "Unable to find file ({$path})."
0020             );
0021         }
0022 
0023         // get mime type of file
0024         $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
0025 
0026         // define core
0027         switch (strtolower($mime)) {
0028             case 'image/png':
0029             case 'image/x-png':
0030                 $core = @imagecreatefrompng($path);
0031                 break;
0032 
0033             case 'image/jpg':
0034             case 'image/jpeg':
0035             case 'image/pjpeg':
0036                 $core = @imagecreatefromjpeg($path);
0037                 if (!$core) {
0038                     $core= @imagecreatefromstring(file_get_contents($path));
0039                 }
0040                 break;
0041 
0042             case 'image/gif':
0043                 $core = @imagecreatefromgif($path);
0044                 break;
0045 
0046             case 'image/webp':
0047             case 'image/x-webp':
0048                 if ( ! function_exists('imagecreatefromwebp')) {
0049                     throw new \Intervention\Image\Exception\NotReadableException(
0050                         "Unsupported image type. GD/PHP installation does not support WebP format."
0051                     );
0052                 }
0053                 $core = @imagecreatefromwebp($path);
0054                 break;
0055 
0056             default:
0057                 throw new \Intervention\Image\Exception\NotReadableException(
0058                     "Unsupported image type. GD driver is only able to decode JPG, PNG, GIF or WebP files."
0059                 );
0060         }
0061 
0062         if (empty($core)) {
0063             throw new \Intervention\Image\Exception\NotReadableException(
0064                 "Unable to decode image from file ({$path})."
0065             );
0066         }
0067 
0068         $this->gdResourceToTruecolor($core);
0069 
0070         // build image
0071         $image = $this->initFromGdResource($core);
0072         $image->mime = $mime;
0073         $image->setFileInfoFromPath($path);
0074 
0075         return $image;
0076     }
0077 
0078     /**
0079      * Initiates new image from GD resource
0080      *
0081      * @param  Resource $resource
0082      * @return \Intervention\Image\Image
0083      */
0084     public function initFromGdResource($resource)
0085     {
0086         return new Image(new Driver, $resource);
0087     }
0088 
0089     /**
0090      * Initiates new image from Imagick object
0091      *
0092      * @param  Imagick $object
0093      * @return \Intervention\Image\Image
0094      */
0095     public function initFromImagick(\Imagick $object)
0096     {
0097         throw new \Intervention\Image\Exception\NotSupportedException(
0098             "Gd driver is unable to init from Imagick object."
0099         );
0100     }
0101 
0102     /**
0103      * Initiates new image from binary data
0104      *
0105      * @param  string $data
0106      * @return \Intervention\Image\Image
0107      */
0108     public function initFromBinary($binary)
0109     {
0110         $resource = @imagecreatefromstring($binary);
0111 
0112         if ($resource === false) {
0113              throw new \Intervention\Image\Exception\NotReadableException(
0114                 "Unable to init from given binary data."
0115             );
0116         }
0117 
0118         $image = $this->initFromGdResource($resource);
0119         $image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary);
0120 
0121         return $image;
0122     }
0123 
0124     /**
0125      * Transform GD resource into Truecolor version
0126      *
0127      * @param  resource $resource
0128      * @return bool
0129      */
0130     public function gdResourceToTruecolor(&$resource)
0131     {
0132         $width = imagesx($resource);
0133         $height = imagesy($resource);
0134 
0135         // new canvas
0136         $canvas = imagecreatetruecolor($width, $height);
0137 
0138         // fill with transparent color
0139         imagealphablending($canvas, false);
0140         $transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
0141         imagefilledrectangle($canvas, 0, 0, $width, $height, $transparent);
0142         imagecolortransparent($canvas, $transparent);
0143         imagealphablending($canvas, true);
0144 
0145         // copy original
0146         imagecopy($canvas, $resource, 0, 0, 0, 0, $width, $height);
0147         imagedestroy($resource);
0148 
0149         $resource = $canvas;
0150 
0151         return true;
0152     }
0153 }