1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Intervention\Image\Gd;
use Intervention\Image\Image;
class Decoder extends \Intervention\Image\AbstractDecoder
{
/**
* Initiates new image from path in filesystem
*
* @param string $path
* @return \Intervention\Image\Image
*/
public function initFromPath($path)
{
$info = @getimagesize($path);
if ($info === false) {
throw new \Intervention\Image\Exception\NotReadableException(
"Unable to read image from file ({$path})."
);
}
// define core
switch ($info[2]) {
case IMAGETYPE_PNG:
$core = @imagecreatefrompng($path);
break;
case IMAGETYPE_JPEG:
$core = @imagecreatefromjpeg($path);
break;
case IMAGETYPE_GIF:
$core = @imagecreatefromgif($path);
break;
default:
throw new \Intervention\Image\Exception\NotReadableException(
"Unable to read image type. GD driver is only able to decode JPG, PNG or GIF files."
);
}
if (empty($core)) {
throw new \Intervention\Image\Exception\NotReadableException(
"Unable to read image from file ({$path})."
);
}
$this->gdResourceToTruecolor($core);
// build image
$image = $this->initFromGdResource($core);
$image->mime = $info['mime'];
$image->setFileInfoFromPath($path);
return $image;
}
/**
* Initiates new image from GD resource
*
* @param Resource $resource
* @return \Intervention\Image\Image
*/
public function initFromGdResource($resource)
{
return new Image(new Driver, $resource);
}
/**
* Initiates new image from Imagick object
*
* @param Imagick $object
* @return \Intervention\Image\Image
*/
public function initFromImagick(\Imagick $object)
{
throw new \Intervention\Image\Exception\NotSupportedException(
"Gd driver is unable to init from Imagick object."
);
}
/**
* Initiates new image from binary data
*
* @param string $data
* @return \Intervention\Image\Image
*/
public function initFromBinary($binary)
{
$resource = @imagecreatefromstring($binary);
if ($resource === false) {
throw new \Intervention\Image\Exception\NotReadableException(
"Unable to init from given binary data."
);
}
$image = $this->initFromGdResource($resource);
$image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary);
return $image;
}
/**
* Transform GD resource into Truecolor version
*
* @param resource $resource
* @return bool
*/
public function gdResourceToTruecolor(&$resource)
{
$width = imagesx($resource);
$height = imagesy($resource);
// new canvas
$canvas = imagecreatetruecolor($width, $height);
// fill with transparent color
imagealphablending($canvas, false);
$transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
imagefilledrectangle($canvas, 0, 0, $width, $height, $transparent);
imagecolortransparent($canvas, $transparent);
imagealphablending($canvas, true);
// copy original
imagecopy($canvas, $resource, 0, 0, 0, 0, $width, $height);
imagedestroy($resource);
$resource = $canvas;
return true;
}
}