forked from php-imagine/Imagine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Imagine.php
195 lines (154 loc) · 5.46 KB
/
Imagine.php
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Gd;
use Imagine\Image\AbstractImagine;
use Imagine\Image\Metadata\MetadataBag;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Palette\PaletteInterface;
use Imagine\Image\BoxInterface;
use Imagine\Image\Palette\Color\RGB as RGBColor;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Exception\RuntimeException;
/**
* Imagine implementation using the GD library
*/
final class Imagine extends AbstractImagine
{
/**
* @var array
*/
private $info;
/**
* @throws RuntimeException
*/
public function __construct()
{
$this->loadGdInfo();
$this->requireGdVersion('2.0.1');
}
/**
* {@inheritdoc}
*/
public function create(BoxInterface $size, ColorInterface $color = null)
{
$width = $size->getWidth();
$height = $size->getHeight();
$resource = imagecreatetruecolor($width, $height);
if (false === $resource) {
throw new RuntimeException('Create operation failed');
}
$palette = null !== $color ? $color->getPalette() : new RGB();
$color = $color ? $color : $palette->color('fff');
if (!$color instanceof RGBColor) {
throw new InvalidArgumentException('GD driver only supports RGB colors');
}
$index = imagecolorallocatealpha($resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round(127 * $color->getAlpha() / 100));
if (false === $index) {
throw new RuntimeException('Unable to allocate color');
}
if (false === imagefill($resource, 0, 0, $index)) {
throw new RuntimeException('Could not set background color fill');
}
if ($color->getAlpha() >= 95) {
imagecolortransparent($resource, $index);
}
return $this->wrap($resource, $palette, new MetadataBag());
}
/**
* {@inheritdoc}
*/
public function open($path)
{
$path = $this->checkPath($path);
$data = @file_get_contents($path);
if (false === $data) {
throw new RuntimeException(sprintf('Failed to open file %s', $path));
}
$resource = @imagecreatefromstring($data);
if (!is_resource($resource)) {
throw new RuntimeException(sprintf('Unable to open image %s', $path));
}
return $this->wrap($resource, new RGB(), $this->getMetadataReader()->readFile($path));
}
/**
* {@inheritdoc}
*/
public function load($string)
{
return $this->doLoad($string, $this->getMetadataReader()->readData($string));
}
/**
* {@inheritdoc}
*/
public function read($resource)
{
if (!is_resource($resource)) {
throw new InvalidArgumentException('Variable does not contain a stream resource');
}
$content = stream_get_contents($resource);
if (false === $content) {
throw new InvalidArgumentException('Cannot read resource content');
}
return $this->doLoad($content, $this->getMetadataReader()->readStream($resource));
}
/**
* {@inheritdoc}
*/
public function font($file, $size, ColorInterface $color)
{
if (!$this->info['FreeType Support']) {
throw new RuntimeException('GD is not compiled with FreeType support');
}
return new Font($file, $size, $color);
}
private function wrap($resource, PaletteInterface $palette, MetadataBag $metadata)
{
if (!imageistruecolor($resource)) {
list($width, $height) = array(imagesx($resource), imagesy($resource));
// create transparent truecolor canvas
$truecolor = imagecreatetruecolor($width, $height);
$transparent = imagecolorallocatealpha($truecolor, 255, 255, 255, 127);
imagefill($truecolor, 0, 0, $transparent);
imagecolortransparent($truecolor, $transparent);
imagecopymerge($truecolor, $resource, 0, 0, 0, 0, $width, $height, 100);
imagedestroy($resource);
$resource = $truecolor;
}
if (false === imagealphablending($resource, false) || false === imagesavealpha($resource, true)) {
throw new RuntimeException('Could not set alphablending, savealpha and antialias values');
}
if (function_exists('imageantialias')) {
imageantialias($resource, true);
}
return new Image($resource, $palette, $metadata);
}
private function loadGdInfo()
{
if (!function_exists('gd_info')) {
throw new RuntimeException('Gd not installed');
}
$this->info = gd_info();
}
private function requireGdVersion($version)
{
if (version_compare(GD_VERSION, $version, '<')) {
throw new RuntimeException(sprintf('GD2 version %s or higher is required', $version));
}
}
private function doLoad($string, MetadataBag $metadata)
{
$resource = @imagecreatefromstring($string);
if (!is_resource($resource)) {
throw new InvalidArgumentException('An image could not be created from the given input');
}
return $this->wrap($resource, new RGB(), $metadata);
}
}