| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfImageTransformGDAdapter extends sfImageTransformAdapterAbstract |
|---|
| 22 |
{ |
|---|
| 23 |
|
|---|
| 24 |
* The image resource. |
|---|
| 25 |
* @access protected |
|---|
| 26 |
* @var resource |
|---|
| 27 |
*/ |
|---|
| 28 |
protected $holder; |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
* Supported MIME types for the sfImageGDAdapter |
|---|
| 32 |
* and their associated file extensions |
|---|
| 33 |
* @var array |
|---|
| 34 |
*/ |
|---|
| 35 |
protected $types = array( |
|---|
| 36 |
'image/jpeg' => array('jpeg','jpg'), |
|---|
| 37 |
'image/gif' => array('gif'), |
|---|
| 38 |
'image/png' => array('png') |
|---|
| 39 |
); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
* List of GD functions used to load specific image types |
|---|
| 43 |
* @var array |
|---|
| 44 |
*/ |
|---|
| 45 |
protected $loaders = array( |
|---|
| 46 |
'image/jpeg' => 'imagecreatefromjpeg', |
|---|
| 47 |
'image/jpg' => 'imagecreatefromjpeg', |
|---|
| 48 |
'image/gif' => 'imagecreatefromgif', |
|---|
| 49 |
'image/png' => 'imagecreatefrompng' |
|---|
| 50 |
); |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
* List of GD functions used to create specific image types |
|---|
| 54 |
* @var array |
|---|
| 55 |
*/ |
|---|
| 56 |
protected $creators = array( |
|---|
| 57 |
'image/jpeg' => 'imagejpeg', |
|---|
| 58 |
'image/jpg' => 'imagejpeg', |
|---|
| 59 |
'image/gif' => 'imagegif', |
|---|
| 60 |
'image/png' => 'imagepng' |
|---|
| 61 |
); |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
* Initialize the object. Check for GD extension. A exception is thrown if not installed |
|---|
| 65 |
* |
|---|
| 66 |
* @throws sfImageTransformException |
|---|
| 67 |
*/ |
|---|
| 68 |
public function __construct() |
|---|
| 69 |
{ |
|---|
| 70 |
|
|---|
| 71 |
if (!extension_loaded('gd')) |
|---|
| 72 |
{ |
|---|
| 73 |
throw new sfImageTransformException('The image processing library GD is not enabled. See PHP Manual for installation instructions.'); |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
* Tidy up the image resources |
|---|
| 79 |
*/ |
|---|
| 80 |
public function __destruct() |
|---|
| 81 |
{ |
|---|
| 82 |
if ($this->hasHolder()) |
|---|
| 83 |
{ |
|---|
| 84 |
imagedestroy($this->getHolder()); |
|---|
| 85 |
} |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
* Create a new empty (1 x 1 px) gd true colour image |
|---|
| 90 |
* @param integer Width |
|---|
| 91 |
* @param integer Height |
|---|
| 92 |
*/ |
|---|
| 93 |
public function create($x=1, $y=1) |
|---|
| 94 |
{ |
|---|
| 95 |
$this->setHolder(imagecreatetruecolor($x,$y)); |
|---|
| 96 |
imagefill($this->holder,0,0,imagecolorallocate($this->getHolder(), 255, 255, 255)); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
* Load and sets the resource from a existing file |
|---|
| 101 |
* @param string |
|---|
| 102 |
* @return boolean |
|---|
| 103 |
* |
|---|
| 104 |
* @throws sfImageTransformException |
|---|
| 105 |
*/ |
|---|
| 106 |
public function load($filename, $mime) |
|---|
| 107 |
{ |
|---|
| 108 |
if (array_key_exists($mime,$this->loaders)) |
|---|
| 109 |
{ |
|---|
| 110 |
$this->holder = $this->loaders[$mime]($filename); |
|---|
| 111 |
$this->mime_type = $mime; |
|---|
| 112 |
$this->setFilename($filename); |
|---|
| 113 |
|
|---|
| 114 |
return true; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
else |
|---|
| 118 |
{ |
|---|
| 119 |
throw new sfImageTransformException(sprintf('Cannot load file %s as %s is an unsupported file type.', $filename, $mime)); |
|---|
| 120 |
} |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
* Loads an image from a string |
|---|
| 125 |
* @param string String image |
|---|
| 126 |
* @return boolean |
|---|
| 127 |
*/ |
|---|
| 128 |
public function loadString($string) |
|---|
| 129 |
{ |
|---|
| 130 |
$resource = imagecreatefromstring($string); |
|---|
| 131 |
|
|---|
| 132 |
if (is_resource($resource) && 'gd' === get_resource_type($resource)) |
|---|
| 133 |
{ |
|---|
| 134 |
$this->setHolder($resource); |
|---|
| 135 |
|
|---|
| 136 |
return true; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
return false; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
* Get the image as string |
|---|
| 144 |
* |
|---|
| 145 |
* @return string |
|---|
| 146 |
*/ |
|---|
| 147 |
public function __toString() |
|---|
| 148 |
{ |
|---|
| 149 |
ob_start(); |
|---|
| 150 |
$this->__output(false); |
|---|
| 151 |
|
|---|
| 152 |
return ob_get_clean(); |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
* Save the image to disk |
|---|
| 157 |
* |
|---|
| 158 |
* @param string $mime |
|---|
| 159 |
* @return boolean |
|---|
| 160 |
*/ |
|---|
| 161 |
public function save($mime='') |
|---|
| 162 |
{ |
|---|
| 163 |
if ('' !== $mime) |
|---|
| 164 |
{ |
|---|
| 165 |
$this->setMimeType($mime); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
return $this->__output(true); |
|---|
| 169 |
|
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
* Save the image to disk |
|---|
| 174 |
* |
|---|
| 175 |
* @param string $filename |
|---|
| 176 |
* @param string $mime |
|---|
| 177 |
* @return boolean |
|---|
| 178 |
*/ |
|---|
| 179 |
public function saveAs($filename, $mime='') |
|---|
| 180 |
{ |
|---|
| 181 |
if ('' !== $mime) |
|---|
| 182 |
{ |
|---|
| 183 |
if (!$this->setMimeType($mime)) |
|---|
| 184 |
{ |
|---|
| 185 |
throw new sfImageTransformException(sprintf('Cannot convert as %s is an unsupported type' ,$mime)); |
|---|
| 186 |
} |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
$this->setFilename($filename); |
|---|
| 190 |
|
|---|
| 191 |
return $this->__output(true, $filename); |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
* Returns a copy of the adapter object |
|---|
| 196 |
* |
|---|
| 197 |
* @return sfImage |
|---|
| 198 |
*/ |
|---|
| 199 |
public function copy() |
|---|
| 200 |
{ |
|---|
| 201 |
$copyObj = clone $this; |
|---|
| 202 |
|
|---|
| 203 |
$copy = imagecreatetruecolor($this->getWidth(), $this->getHeight()); |
|---|
| 204 |
imagecopy($copy, $this->getHolder(), 0, 0, 0, 0, $this->getWidth(), $this->getHeight()); |
|---|
| 205 |
|
|---|
| 206 |
$copyObj->setHolder($copy); |
|---|
| 207 |
|
|---|
| 208 |
return $copyObj; |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
* Gets the pixel width of the image |
|---|
| 213 |
* |
|---|
| 214 |
* @return integer |
|---|
| 215 |
*/ |
|---|
| 216 |
public function getWidth() |
|---|
| 217 |
{ |
|---|
| 218 |
if ($this->hasHolder()) |
|---|
| 219 |
{ |
|---|
| 220 |
return imagesx($this->getHolder()); |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
return 0; |
|---|
| 224 |
} |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
* Gets the pixel height of the image |
|---|
| 228 |
* |
|---|
| 229 |
* @return integer |
|---|
| 230 |
*/ |
|---|
| 231 |
public function getHeight() |
|---|
| 232 |
{ |
|---|
| 233 |
if ($this->hasHolder()) |
|---|
| 234 |
{ |
|---|
| 235 |
return imagesy($this->getHolder()); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
return 0; |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
* Sets the image resource holder |
|---|
| 243 |
* @param resource |
|---|
| 244 |
* @return boolean |
|---|
| 245 |
* |
|---|
| 246 |
*/ |
|---|
| 247 |
public function setHolder($resource) |
|---|
| 248 |
{ |
|---|
| 249 |
|
|---|
| 250 |
if (is_resource($resource) && 'gd' === get_resource_type($resource)) |
|---|
| 251 |
{ |
|---|
| 252 |
|
|---|
| 253 |
$this->holder = $resource; |
|---|
| 254 |
return true; |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
return false; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
* Returns the image resource |
|---|
| 262 |
* @return resource |
|---|
| 263 |
* |
|---|
| 264 |
*/ |
|---|
| 265 |
public function getHolder() |
|---|
| 266 |
{ |
|---|
| 267 |
if ($this->hasHolder()) |
|---|
| 268 |
{ |
|---|
| 269 |
return $this->holder; |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
return false; |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
* Returns whether there is a valid GD image resource |
|---|
| 277 |
* @return boolean |
|---|
| 278 |
* |
|---|
| 279 |
*/ |
|---|
| 280 |
public function hasHolder() |
|---|
| 281 |
{ |
|---|
| 282 |
if (is_resource($this->holder) && 'gd' === get_resource_type($this->holder)) |
|---|
| 283 |
{ |
|---|
| 284 |
return true; |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
return false; |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
* Returns image MIME type |
|---|
| 292 |
* @return boolean |
|---|
| 293 |
* |
|---|
| 294 |
*/ |
|---|
| 295 |
public function getMIMEType() |
|---|
| 296 |
{ |
|---|
| 297 |
return $this->mime_type; |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
public function setMIMEType($mime) |
|---|
| 301 |
{ |
|---|
| 302 |
if (array_key_exists($mime,$this->loaders)) |
|---|
| 303 |
{ |
|---|
| 304 |
$this->mime_type = $mime; |
|---|
| 305 |
return true; |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
return false; |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
* Returns image MIME type |
|---|
| 313 |
* @return boolean |
|---|
| 314 |
* |
|---|
| 315 |
*/ |
|---|
| 316 |
public function getMIMETypeFromFilename($filename) |
|---|
| 317 |
{ |
|---|
| 318 |
|
|---|
| 319 |
$path = pathinfo($filename); |
|---|
| 320 |
|
|---|
| 321 |
foreach($this->types as $type => $extensions) |
|---|
| 322 |
{ |
|---|
| 323 |
if (in_array($path['extension'], $extensions)) |
|---|
| 324 |
{ |
|---|
| 325 |
return $type; |
|---|
| 326 |
} |
|---|
| 327 |
|
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
return false; |
|---|
| 331 |
} |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
* Returns the name of the adapter |
|---|
| 335 |
* @return string |
|---|
| 336 |
* |
|---|
| 337 |
*/ |
|---|
| 338 |
public function getAdapterName() |
|---|
| 339 |
{ |
|---|
| 340 |
return 'GD'; |
|---|
| 341 |
} |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
* Returns the image color for a hex value (format #XXXXXX). |
|---|
| 345 |
* |
|---|
| 346 |
* @param resource image resource |
|---|
| 347 |
* @param string full hex value of the color or GD constant |
|---|
| 348 |
* @return integer |
|---|
| 349 |
*/ |
|---|
| 350 |
public function getColorByHex($image, $color) |
|---|
| 351 |
{ |
|---|
| 352 |
|
|---|
| 353 |
if (preg_match('/#[\d\w]{6}/',$color)) |
|---|
| 354 |
{ |
|---|
| 355 |
$rgb = sscanf($color, '#%2x%2x%2x'); |
|---|
| 356 |
$color = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]); |
|---|
| 357 |
|
|---|
| 358 |
return $color; |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
return $color; |
|---|
| 362 |
} |
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
* Returns image in current format and optionally writes image to disk |
|---|
| 366 |
* @return resource |
|---|
| 367 |
* |
|---|
| 368 |
* @throws sfImageTransformException |
|---|
| 369 |
*/ |
|---|
| 370 |
protected function __output($to_file=false, $filename='') |
|---|
| 371 |
{ |
|---|
| 372 |
$file = null; |
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
if ($to_file) |
|---|
| 376 |
{ |
|---|
| 377 |
$file = $filename; |
|---|
| 378 |
if ('' === $file) |
|---|
| 379 |
{ |
|---|
| 380 |
$file = $this->getFilename(); |
|---|
| 381 |
} |
|---|
| 382 |
} |
|---|
| 383 |
|
|---|
| 384 |
$mime = $this->getMimeType(); |
|---|
| 385 |
|
|---|
| 386 |
if (array_key_exists($mime,$this->creators)) |
|---|
| 387 |
{ |
|---|
| 388 |
|
|---|
| 389 |
switch ($mime) |
|---|
| 390 |
{ |
|---|
| 391 |
|
|---|
| 392 |
case 'image/jpeg': |
|---|
| 393 |
case 'image/jpg': |
|---|
| 394 |
if (is_null($this->quality)) |
|---|
| 395 |
{ |
|---|
| 396 |
$this->quality = 75; |
|---|
| 397 |
} |
|---|
| 398 |
$output = $this->creators[$mime]($this->holder,$file,$this->getImageSpecificQuality($this->quality, $mime)); |
|---|
| 399 |
break; |
|---|
| 400 |
|
|---|
| 401 |
case 'image/png': |
|---|
| 402 |
imagesavealpha($this->holder, true); |
|---|
| 403 |
$output = $this->creators[$mime]($this->holder,$file,$this->getImageSpecificQuality($this->quality, $mime), null); |
|---|
| 404 |
break; |
|---|
| 405 |
|
|---|
| 406 |
case 'image/gif': |
|---|
| 407 |
|
|---|
| 408 |
if (!is_null($file)) |
|---|
| 409 |
{ |
|---|
| 410 |
$output = $this->creators[$mime]($this->holder,$file); |
|---|
| 411 |
} |
|---|
| 412 |
else |
|---|
| 413 |
{ |
|---|
| 414 |
$output = $this->creators[$mime]($this->holder); |
|---|
| 415 |
} |
|---|
| 416 |
break; |
|---|
| 417 |
|
|---|
| 418 |
default: |
|---|
| 419 |
throw new sfImageTransformException(sprintf('Cannot convert as %s is an unsupported type' ,$mime)); |
|---|
| 420 |
} |
|---|
| 421 |
} |
|---|
| 422 |
else |
|---|
| 423 |
{ |
|---|
| 424 |
throw new sfImageTransformException(sprintf('Cannot convert as %s is an unsupported type' ,$mime)); |
|---|
| 425 |
} |
|---|
| 426 |
|
|---|
| 427 |
return $output; |
|---|
| 428 |
} |
|---|
| 429 |
|
|---|
| 430 |
protected function getImageSpecificQuality($quality, $mime) |
|---|
| 431 |
{ |
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
if ('image/png' === $mime) |
|---|
| 435 |
{ |
|---|
| 436 |
|
|---|
| 437 |
return 9 - round($this->quality * (9/100)); |
|---|
| 438 |
} |
|---|
| 439 |
|
|---|
| 440 |
return $this->quality; |
|---|
| 441 |
} |
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
* Helper method. Returns a transparent image resource of the specified size |
|---|
| 445 |
* @param integer width |
|---|
| 446 |
* @param integer height |
|---|
| 447 |
* @return resource image |
|---|
| 448 |
* |
|---|
| 449 |
* @throws sfImageTransformException |
|---|
| 450 |
*/ |
|---|
| 451 |
public function getTransparentImage($w, $h) |
|---|
| 452 |
{ |
|---|
| 453 |
|
|---|
| 454 |
$resource = $this->getHolder(); |
|---|
| 455 |
|
|---|
| 456 |
$dest_resource = imagecreatetruecolor((int)$w, (int)$h); |
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
if (in_array($this->getMIMEType(), array('image/gif','image/png'))) |
|---|
| 460 |
{ |
|---|
| 461 |
$index = imagecolortransparent($resource); |
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
if ($index >= 0) |
|---|
| 465 |
{ |
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
$index_color = imagecolorsforindex($resource, $index); |
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
$index = imagecolorallocate($dest_resource, $index_color['red'], $index_color['green'], $index_color['blue']); |
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
imagefill($dest_resource, 0, 0, $index); |
|---|
| 475 |
|
|---|
| 476 |
|
|---|
| 477 |
imagecolortransparent($dest_resource, $index); |
|---|
| 478 |
|
|---|
| 479 |
} |
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
elseif ($this->getMIMEType() == 'image/png') |
|---|
| 483 |
{ |
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
imagealphablending($dest_resource, false); |
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
$color = imagecolorallocatealpha($dest_resource, 0, 0, 0, 127); |
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
imagefill($dest_resource, 0, 0, $color); |
|---|
| 493 |
|
|---|
| 494 |
|
|---|
| 495 |
imagesavealpha($dest_resource, true); |
|---|
| 496 |
} |
|---|
| 497 |
} |
|---|
| 498 |
|
|---|
| 499 |
return $dest_resource; |
|---|
| 500 |
|
|---|
| 501 |
} |
|---|
| 502 |
|
|---|
| 503 |
} |
|---|
| 504 |
|
|---|