Cheetah
AbstractEncoder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Intervention\Image;
4 
5 abstract class AbstractEncoder
6 {
12  public $result;
13 
19  public $image;
20 
26  public $format;
27 
33  public $quality;
34 
40  abstract protected function processJpeg();
41 
47  abstract protected function processPng();
48 
54  abstract protected function processGif();
55 
61  abstract protected function processTiff();
62 
68  abstract protected function processBmp();
69 
75  abstract protected function processIco();
76 
85  public function process(Image $image, $format = null, $quality = null)
86  {
87  $this->setImage($image);
88  $this->setFormat($format);
89  $this->setQuality($quality);
90 
91  switch (strtolower($this->format)) {
92 
93  case 'data-url':
94  $this->result = $this->processDataUrl();
95  break;
96 
97  case 'gif':
98  case 'image/gif':
99  $this->result = $this->processGif();
100  break;
101 
102  case 'png':
103  case 'image/png':
104  case 'image/x-png':
105  $this->result = $this->processPng();
106  break;
107 
108  case 'jpg':
109  case 'jpeg':
110  case 'image/jpg':
111  case 'image/jpeg':
112  case 'image/pjpeg':
113  $this->result = $this->processJpeg();
114  break;
115 
116  case 'tif':
117  case 'tiff':
118  case 'image/tiff':
119  case 'image/tif':
120  case 'image/x-tif':
121  case 'image/x-tiff':
122  $this->result = $this->processTiff();
123  break;
124 
125  case 'bmp':
126  case 'image/bmp':
127  case 'image/ms-bmp':
128  case 'image/x-bitmap':
129  case 'image/x-bmp':
130  case 'image/x-ms-bmp':
131  case 'image/x-win-bitmap':
132  case 'image/x-windows-bmp':
133  case 'image/x-xbitmap':
134  $this->result = $this->processBmp();
135  break;
136 
137  case 'ico':
138  case 'image/x-icon':
139  case 'image/vnd.microsoft.icon':
140  $this->result = $this->processIco();
141  break;
142 
143  case 'psd':
144  case 'image/vnd.adobe.photoshop':
145  $this->result = $this->processPsd();
146  break;
147 
148  default:
149  throw new \Intervention\Image\Exception\NotSupportedException(
150  "Encoding format ({$format}) is not supported."
151  );
152  }
153 
154  return $image->setEncoded($this->result);
155  }
156 
162  protected function processDataUrl()
163  {
164  $mime = $this->image->mime ? $this->image->mime : 'image/png';
165 
166  return sprintf('data:%s;base64,%s',
167  $mime,
168  base64_encode($this->process($this->image, $mime, $this->quality))
169  );
170  }
171 
177  protected function setImage($image)
178  {
179  $this->image = $image;
180  }
181 
187  protected function setFormat($format = null)
188  {
189  if ($format == '' && $this->image instanceof Image) {
190  $format = $this->image->mime;
191  }
192 
193  $this->format = $format ? $format : 'jpg';
194 
195  return $this;
196  }
197 
203  protected function setQuality($quality)
204  {
205  $quality = is_null($quality) ? 90 : $quality;
206  $quality = $quality === 0 ? 1 : $quality;
207 
208  if ($quality < 0 || $quality > 100) {
209  throw new \Intervention\Image\Exception\InvalidArgumentException(
210  'Quality must range from 0 to 100.'
211  );
212  }
213 
214  $this->quality = intval($quality);
215 
216  return $this;
217  }
218 }
Intervention\Image\AbstractEncoder\setImage
setImage($image)
Definition: AbstractEncoder.php:177
Intervention\Image\AbstractEncoder\$result
$result
Definition: AbstractEncoder.php:12
Intervention\Image\AbstractEncoder\processIco
processIco()
Intervention\Image\AbstractEncoder\processBmp
processBmp()
Intervention\Image\AbstractEncoder\$format
$format
Definition: AbstractEncoder.php:26
Intervention\Image\AbstractEncoder\processGif
processGif()
php
Intervention\Image\AbstractEncoder\$image
$image
Definition: AbstractEncoder.php:19
Intervention\Image\AbstractEncoder\process
process(Image $image, $format=null, $quality=null)
Definition: AbstractEncoder.php:85
Intervention\Image\AbstractEncoder\setFormat
setFormat($format=null)
Definition: AbstractEncoder.php:187
Intervention\Image\Image
Definition: Image.php:50
Intervention\Image\AbstractEncoder\processJpeg
processJpeg()
Intervention\Image\AbstractEncoder\processTiff
processTiff()
Intervention\Image\AbstractEncoder\setQuality
setQuality($quality)
Definition: AbstractEncoder.php:203
Intervention\Image\AbstractEncoder
Definition: AbstractEncoder.php:6
Intervention\Image\AbstractEncoder\processPng
processPng()
image
License THE YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE TO THE EXTENT THIS LICENSE MAY BE CONSIDERED TO BE A THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS Definitions Adaptation means a work based upon the or upon the Work and other pre existing such as a derivative arrangement of music or other alterations of a literary or artistic or phonogram or performance and includes cinematographic adaptations or any other form in which the Work may be or adapted including in any form recognizably derived from the except that a work that constitutes a Collection will not be considered an Adaptation for the purpose of this License For the avoidance of where the Work is a musical performance or the synchronization of the Work in timed relation with a moving image("synching") will be considered an Adaptation for the purpose of this License. 2. "Collection" means a collection of literary or artistic works
Intervention\Image\AbstractEncoder\$quality
$quality
Definition: AbstractEncoder.php:33
Intervention\Image
Definition: AbstractColor.php:3
Intervention\Image\AbstractEncoder\processDataUrl
processDataUrl()
Definition: AbstractEncoder.php:162