Cheetah
Decoder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Intervention\Image\Gd;
4 
5 use \Intervention\Image\Image;
6 use \Intervention\Image\Size;
7 
9 {
16  public function initFromPath($path)
17  {
18  $info = @getimagesize($path);
19 
20  if ($info === false) {
21  throw new \Intervention\Image\Exception\NotReadableException(
22  "Unable to read image from file ({$path})."
23  );
24  }
25 
26  // define core
27  switch ($info[2]) {
28  case IMAGETYPE_PNG:
29  $core = imagecreatefrompng($path);
30  $this->gdResourceToTruecolor($core);
31  break;
32 
33  case IMAGETYPE_JPEG:
34  $core = imagecreatefromjpeg($path);
35  $this->gdResourceToTruecolor($core);
36  break;
37 
38  case IMAGETYPE_GIF:
39  $core = imagecreatefromgif($path);
40  $this->gdResourceToTruecolor($core);
41  break;
42 
43  default:
44  throw new \Intervention\Image\Exception\NotReadableException(
45  "Unable to read image type. GD driver is only able to decode JPG, PNG or GIF files."
46  );
47  }
48 
49  // build image
50  $image = $this->initFromGdResource($core);
51  $image->mime = $info['mime'];
52  $image->setFileInfoFromPath($path);
53 
54  return $image;
55  }
56 
63  public function initFromGdResource($resource)
64  {
65  return new Image(new Driver, $resource);
66  }
67 
74  public function initFromImagick(\Imagick $object)
75  {
76  throw new \Intervention\Image\Exception\NotSupportedException(
77  "Gd driver is unable to init from Imagick object."
78  );
79  }
80 
87  public function initFromBinary($binary)
88  {
89  $resource = @imagecreatefromstring($binary);
90 
91  if ($resource === false) {
92  throw new \Intervention\Image\Exception\NotReadableException(
93  "Unable to init from given binary data."
94  );
95  }
96 
97  $image = $this->initFromGdResource($resource);
98  $image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary);
99 
100  return $image;
101  }
102 
109  public function gdResourceToTruecolor(&$resource)
110  {
111  $width = imagesx($resource);
112  $height = imagesy($resource);
113 
114  // new canvas
115  $canvas = imagecreatetruecolor($width, $height);
116 
117  // fill with transparent color
118  imagealphablending($canvas, false);
119  $transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
120  imagefilledrectangle($canvas, 0, 0, $width, $height, $transparent);
121  imagecolortransparent($canvas, $transparent);
122  imagealphablending($canvas, true);
123 
124  // copy original
125  imagecopy($canvas, $resource, 0, 0, 0, 0, $width, $height);
126  imagedestroy($resource);
127 
128  $resource = $canvas;
129 
130  return true;
131  }
132 }
Intervention\Image\Gd\Decoder\initFromPath
initFromPath($path)
Definition: Decoder.php:16
php
Intervention\Image\Gd\Decoder\gdResourceToTruecolor
gdResourceToTruecolor(&$resource)
Definition: Decoder.php:109
Intervention\Image\Gd
Definition: Color.php:3
Intervention\Image\Gd\Decoder\initFromGdResource
initFromGdResource($resource)
Definition: Decoder.php:63
Intervention\Image\AbstractDecoder
Definition: AbstractDecoder.php:6
Intervention\Image\Image
Definition: Image.php:50
$path
$path
Definition: header.inc.php:12
Intervention\Image\Gd\Decoder\initFromBinary
initFromBinary($binary)
Definition: Decoder.php:87
Intervention\Image\Gd\Driver
Definition: Driver.php:8
Intervention\Image\Gd\Decoder
Definition: Decoder.php:9
Intervention\Image\Gd\Decoder\initFromImagick
initFromImagick(\Imagick $object)
Definition: Decoder.php:74