| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\Image\Gd; |
| 4 |
|
| 5 |
use Intervention\Image\Exception\NotReadableException; |
| 6 |
use Intervention\Image\Exception\NotSupportedException; |
| 7 |
use Intervention\Image\Image; |
| 8 |
|
| 9 |
class Decoder extends \Intervention\Image\AbstractDecoder |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Initiates new image from path in filesystem |
| 13 |
* |
| 14 |
* @param string $path |
| 15 |
* @return \Intervention\Image\Image |
| 16 |
*/ |
| 17 |
public function initFromPath($path) |
| 18 |
{ |
| 19 |
if ( ! file_exists($path)) { |
| 20 |
throw new NotReadableException( |
| 21 |
"Unable to find file ({$path})." |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
// get mime type of file |
| 26 |
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path); |
| 27 |
|
| 28 |
// define core |
| 29 |
switch (strtolower($mime)) { |
| 30 |
case 'image/png': |
| 31 |
case 'image/x-png': |
| 32 |
$core = @imagecreatefrompng($path); |
| 33 |
break; |
| 34 |
|
| 35 |
case 'image/jpg': |
| 36 |
case 'image/jpeg': |
| 37 |
case 'image/pjpeg': |
| 38 |
$core = @imagecreatefromjpeg($path); |
| 39 |
if (!$core) { |
| 40 |
$core= @imagecreatefromstring(file_get_contents($path)); |
| 41 |
} |
| 42 |
break; |
| 43 |
|
| 44 |
case 'image/gif': |
| 45 |
$core = @imagecreatefromgif($path); |
| 46 |
break; |
| 47 |
|
| 48 |
case 'image/webp': |
| 49 |
case 'image/x-webp': |
| 50 |
if ( ! function_exists('imagecreatefromwebp')) { |
| 51 |
throw new NotReadableException( |
| 52 |
"Unsupported image type. GD/PHP installation does not support WebP format." |
| 53 |
); |
| 54 |
} |
| 55 |
$core = @imagecreatefromwebp($path); |
| 56 |
break; |
| 57 |
|
| 58 |
case 'image/bmp': |
| 59 |
case 'image/ms-bmp': |
| 60 |
case 'image/x-bitmap': |
| 61 |
case 'image/x-bmp': |
| 62 |
case 'image/x-ms-bmp': |
| 63 |
case 'image/x-win-bitmap': |
| 64 |
case 'image/x-windows-bmp': |
| 65 |
case 'image/x-xbitmap': |
| 66 |
if (! function_exists('imagecreatefrombmp')) { |
| 67 |
throw new NotReadableException( |
| 68 |
"Unsupported image type. GD/PHP installation does not support BMP format." |
| 69 |
); |
| 70 |
} |
| 71 |
$core = @imagecreatefrombmp($path); |
| 72 |
break; |
| 73 |
|
| 74 |
default: |
| 75 |
throw new NotReadableException( |
| 76 |
sprintf("Unsupported image type %s. GD driver is only able to decode JPG, PNG, GIF, BMP or WebP files.", strtolower($mime)) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
if (empty($core)) { |
| 81 |
throw new NotReadableException( |
| 82 |
"Unable to decode image from file ({$path})." |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
$this->gdResourceToTruecolor($core); |
| 87 |
|
| 88 |
// build image |
| 89 |
$image = $this->initFromGdResource($core); |
| 90 |
$image->mime = $mime; |
| 91 |
$image->setFileInfoFromPath($path); |
| 92 |
|
| 93 |
return $image; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Initiates new image from GD resource |
| 98 |
* |
| 99 |
* @param Resource $resource |
| 100 |
* @return \Intervention\Image\Image |
| 101 |
*/ |
| 102 |
public function initFromGdResource($resource) |
| 103 |
{ |
| 104 |
return new Image(new Driver, $resource); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Initiates new image from Imagick object |
| 109 |
* |
| 110 |
* @param Imagick $object |
| 111 |
* @return \Intervention\Image\Image |
| 112 |
*/ |
| 113 |
public function initFromImagick(\Imagick $object) |
| 114 |
{ |
| 115 |
throw new NotSupportedException( |
| 116 |
"Gd driver is unable to init from Imagick object." |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Initiates new image from binary data |
| 122 |
* |
| 123 |
* @param string $data |
| 124 |
* @return \Intervention\Image\Image |
| 125 |
*/ |
| 126 |
public function initFromBinary($binary) |
| 127 |
{ |
| 128 |
$resource = @imagecreatefromstring($binary); |
| 129 |
|
| 130 |
if ($resource === false) { |
| 131 |
throw new NotReadableException( |
| 132 |
"Unable to init from given binary data." |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
$image = $this->initFromGdResource($resource); |
| 137 |
$image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary); |
| 138 |
|
| 139 |
return $image; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Transform GD resource into Truecolor version |
| 144 |
* |
| 145 |
* @param resource $resource |
| 146 |
* @return bool |
| 147 |
*/ |
| 148 |
public function gdResourceToTruecolor(&$resource) |
| 149 |
{ |
| 150 |
$width = imagesx($resource); |
| 151 |
$height = imagesy($resource); |
| 152 |
|
| 153 |
// new canvas |
| 154 |
$canvas = imagecreatetruecolor($width, $height); |
| 155 |
|
| 156 |
// fill with transparent color |
| 157 |
imagealphablending($canvas, false); |
| 158 |
$transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127); |
| 159 |
imagefilledrectangle($canvas, 0, 0, $width, $height, $transparent); |
| 160 |
imagecolortransparent($canvas, $transparent); |
| 161 |
imagealphablending($canvas, true); |
| 162 |
|
| 163 |
// copy original |
| 164 |
imagecopy($canvas, $resource, 0, 0, 0, 0, $width, $height); |
| 165 |
imagedestroy($resource); |
| 166 |
|
| 167 |
$resource = $canvas; |
| 168 |
|
| 169 |
return true; |
| 170 |
} |
| 171 |
} |
| 172 |
|