Skip to content

Commit 88dfe8b

Browse files
committed
Merge pull request #12 from hugoseabra/master
Adding crop positions and documentation blocks
2 parents 24cfa7d + d4244fa commit 88dfe8b

File tree

1 file changed

+117
-15
lines changed

1 file changed

+117
-15
lines changed

src/ImageResize.php

Lines changed: 117 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
namespace Eventviva;
44

5+
use \Exception;
6+
7+
/**
8+
* Handles thumb image(s) according to the original source given.
9+
*/
510
class ImageResize
611
{
7-
12+
const cropTOP = 1;
13+
const cropCENTRE = 2;
14+
const cropCENTER = 2;
15+
const cropBOTTOM = 3;
16+
const cropLEFT = 4;
17+
const cropRIGHT = 5;
18+
819
public $quality_jpg = 75;
920
public $quality_png = 0;
1021

@@ -27,11 +38,21 @@ class ImageResize
2738
protected $source_w;
2839
protected $source_h;
2940

41+
/**
42+
* Constructor
43+
* @param string $filename
44+
*/
3045
public function __construct($filename)
3146
{
3247
$this->load($filename);
3348
}
3449

50+
/**
51+
* Loads image source and its properties to the instanciated object
52+
* @param string $filename
53+
* @return \static
54+
* @throws Exception
55+
*/
3556
protected function load($filename)
3657
{
3758
$image_info = getimagesize($filename);
@@ -61,12 +82,19 @@ protected function load($filename)
6182

6283
default:
6384
throw new \Exception('Unsupported image type');
64-
break;
6585
}
6686

6787
return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
6888
}
69-
89+
90+
/**
91+
* Saves new image
92+
* @param string $filename
93+
* @param string $image_type
94+
* @param integer $quality
95+
* @param integer $permissions
96+
* @return \static
97+
*/
7098
public function save($filename, $image_type = null, $quality = null, $permissions = null)
7199
{
72100
$image_type = $image_type ?: $this->source_type;
@@ -133,7 +161,12 @@ public function save($filename, $image_type = null, $quality = null, $permission
133161

134162
return $this;
135163
}
136-
164+
165+
/**
166+
* Outpus image source to browser
167+
* @param string $image_type
168+
* @param integer $quality
169+
*/
137170
public function output($image_type = null, $quality = null)
138171
{
139172
$image_type = $image_type ?: $this->source_type;
@@ -142,7 +175,13 @@ public function output($image_type = null, $quality = null)
142175

143176
$this->save(null, $image_type, $quality);
144177
}
145-
178+
179+
/**
180+
* Resizes image according to the given height. Width is proportional.
181+
* @param integer $height
182+
* @param boolean $allow_enlarge
183+
* @return \static
184+
*/
146185
public function resizeToHeight($height, $allow_enlarge = false)
147186
{
148187
$ratio = $height / $this->getSourceHeight();
@@ -152,7 +191,13 @@ public function resizeToHeight($height, $allow_enlarge = false)
152191

153192
return $this;
154193
}
155-
194+
195+
/**
196+
* Resizes image according to the given width. Height is proportional.
197+
* @param integer $width
198+
* @param boolean $allow_enlarge
199+
* @return \static
200+
*/
156201
public function resizeToWidth($width, $allow_enlarge = false)
157202
{
158203
$ratio = $width / $this->getSourceWidth();
@@ -163,6 +208,11 @@ public function resizeToWidth($width, $allow_enlarge = false)
163208
return $this;
164209
}
165210

211+
/**
212+
* Resizes image according to given scale (proportionally)
213+
* @param type $scale
214+
* @return \Eventviva\ImageResize
215+
*/
166216
public function scale($scale)
167217
{
168218
$width = $this->getSourceWidth() * $scale / 100;
@@ -173,6 +223,13 @@ public function scale($scale)
173223
return $this;
174224
}
175225

226+
/**
227+
* Resizes image according to the given width and height
228+
* @param integer $width
229+
* @param integer $height
230+
* @param boolean $allow_enlarge
231+
* @return \static
232+
*/
176233
public function resize($width, $height, $allow_enlarge = false)
177234
{
178235
if (!$allow_enlarge) {
@@ -197,8 +254,17 @@ public function resize($width, $height, $allow_enlarge = false)
197254

198255
return $this;
199256
}
200-
201-
public function crop($width, $height, $allow_enlarge = false)
257+
258+
/**
259+
* Crops image according to the given width and height for the new saved
260+
* image. Crop's position may be configured.
261+
* @param integer $width
262+
* @param integer $height
263+
* @param boolean $allow_enlarge
264+
* @param integer $position
265+
* @return \static
266+
*/
267+
public function crop($width, $height, $allow_enlarge = false, $position = self::cropCENTER)
202268
{
203269
if (!$allow_enlarge) {
204270
// this logic is slightly different to resize(),
@@ -213,17 +279,17 @@ public function crop($width, $height, $allow_enlarge = false)
213279
$height = $this->getSourceHeight();
214280
}
215281
}
216-
282+
217283
$ratio_source = $this->getSourceWidth() / $this->getSourceHeight();
218284
$ratio_dest = $width / $height;
219-
285+
220286
if ($ratio_dest < $ratio_source) {
221287
$this->resizeToHeight($height, $allow_enlarge);
222288

223289
$excess_width = ($this->getDestWidth() - $width) / $this->getDestWidth() * $this->getSourceWidth();
224290

225291
$this->source_w = $this->getSourceWidth() - $excess_width;
226-
$this->source_x = $excess_width / 2;
292+
$this->source_x = $this->getCropPosition($excess_width, $position);
227293

228294
$this->dest_w = $width;
229295
} else {
@@ -232,32 +298,68 @@ public function crop($width, $height, $allow_enlarge = false)
232298
$excess_height = ($this->getDestHeight() - $height) / $this->getDestHeight() * $this->getSourceHeight();
233299

234300
$this->source_h = $this->getSourceHeight() - $excess_height;
235-
$this->source_y = $excess_height / 2;
301+
$this->source_y = $this->getCropPosition($excess_height, $position);
236302

237303
$this->dest_h = $height;
238304
}
239305

240306
return $this;
241307
}
242-
308+
309+
/**
310+
* Gets source width
311+
* @return integer
312+
*/
243313
public function getSourceWidth()
244314
{
245315
return $this->original_w;
246316
}
247-
317+
318+
/**
319+
* Gets source height
320+
* @return integer
321+
*/
248322
public function getSourceHeight()
249323
{
250324
return $this->original_h;
251325
}
252326

327+
/**
328+
* Gets width of the destination image
329+
* @return integer
330+
*/
253331
public function getDestWidth()
254332
{
255333
return $this->dest_w;
256334
}
257335

336+
/**
337+
* Gets height of the destination image
338+
* @return integer
339+
*/
258340
public function getDestHeight()
259341
{
260342
return $this->dest_h;
261343
}
262-
344+
345+
/**
346+
* Gets crop position (X or Y) according to the given position.
347+
* @param integer $expectedSize
348+
* @param integer $position
349+
* @return integer
350+
*/
351+
protected function getCropPosition($expectedSize, $position = self::cropCENTER)
352+
{
353+
$size = 0;
354+
switch ($position) {
355+
case self::cropBOTTOM:
356+
case self::cropRIGHT:
357+
$size = $expectedSize;
358+
break;
359+
case self::cropCENTER:
360+
case self::cropCENTRE:
361+
$size = $expectedSize / 2;
362+
}
363+
return $size;
364+
}
263365
}

0 commit comments

Comments
 (0)