Skip to content

Commit e8db480

Browse files
author
stunami
committed
Added four new types to thumbnailer - bottom-left, bottom-right, top-left, top-right (caefer)
Updated README git-svn-id: http://svn.symfony-project.com/plugins/sfImageTransformPlugin/trunk@30689 ee427ae8-e902-0410-961c-c3ed070cd9f9
1 parent 3031eec commit e8db480

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ sfImageTransformPlugin supports several different types of thumbnailing:
7575
* fit - Creates an image of the specified size and fits the thumbnailed inside. This will produce uniform size thumbnails. (default)
7676
* scale - Scales the image to fit inside the specified dimensions.
7777
* inflate, deflate - simply resizes the image to the specified dimensions, proportions are not preserved.
78-
* left, right, top, bottom, center - scales the image to specified size and then crops
78+
* left, right, top, bottom, center, bottom-left, bottom-right, top-left, top-right - scales the image to specified size and then crops
7979

8080
*Example 3. Thumbnailing*
8181

lib/transforms/Generic/sfImageThumbnailGeneric.php

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class sfImageThumbnailGeneric extends sfImageTransformAbstract
4747
/**
4848
* available methods for thumbnail creation
4949
*/
50-
protected $methods = array('fit', 'scale', 'inflate','deflate', 'left' ,'right', 'top', 'bottom', 'center');
50+
protected $methods = array('fit', 'scale', 'inflate','deflate', 'left' ,'right', 'top', 'bottom', 'center', 'top-left', 'top-right', 'bottom-left', 'bottom-right');
5151

5252
/*
5353
* background color in hex or null for transparent
@@ -195,40 +195,53 @@ protected function transform(sfImage $image)
195195

196196
$scale_w = $this->getWidth()/$resource_w;
197197
$scale_h = $this->getHeight()/$resource_h;
198-
switch ($this->getMethod())
198+
$method = $this->getMethod();
199+
switch ($method)
199200
{
200201
case 'deflate':
201202
case 'inflate':
202203

203204
return $image->resize($this->getWidth(), $this->getHeight());
204205

205206
case 'left':
206-
$image->scale(max($scale_w, $scale_h));
207-
208-
return $image->crop(0, (int)round(($image->getHeight() - $this->getHeight()) / 2), $this->getWidth(), $this->getHeight());
209-
210207
case 'right':
211-
$image->scale(max($scale_w, $scale_h));
212-
213-
return $image->crop(($image->getWidth() - $this->getWidth()), (int)round(($image->getHeight() - $this->getHeight()) / 2),$this->getWidth(), $this->getHeight());
214-
215208
case 'top':
216-
$image->scale(max($scale_w, $scale_h));
217-
218-
return $image->crop((int)round(($image->getWidth() - $this->getWidth()) / 2), 0, $this->getWidth(), $this->getHeight());
219-
220209
case 'bottom':
221-
$image->scale(max($scale_w, $scale_h));
222-
223-
return $image->crop((int)round(($image->getWidth() - $this->getWidth()) / 2), ($image->getHeight() - $this->getHeight()), $this->getWidth(), $this->getHeight());
224-
210+
case 'top-left':
211+
case 'top-right':
212+
case 'bottom-left':
213+
case 'bottom-right':
225214
case 'center':
226215
$image->scale(max($scale_w, $scale_h));
227-
228-
$left = (int)round(($image->getWidth() - $this->getWidth()) / 2);
229-
$top = (int)round(($image->getHeight() - $this->getHeight()) / 2);
216+
217+
if(false !== strstr($method, 'top'))
218+
{
219+
$top = 0;
220+
}
221+
else if(false !== strstr($method, 'bottom'))
222+
{
223+
$top = $image->getHeight() - $this->getHeight();
224+
}
225+
else
226+
{
227+
$top = (int)round(($image->getHeight() - $this->getHeight()) / 2);
228+
}
229+
230+
if(false !== strstr($method, 'left'))
231+
{
232+
$left = 0;
233+
}
234+
else if(false !== strstr($method, 'right'))
235+
{
236+
$left = $image->getWidth() - $this->getWidth();
237+
}
238+
else
239+
{
240+
$left = (int)round(($image->getWidth() - $this->getWidth()) / 2);
241+
}
230242

231243
return $image->crop($left, $top, $this->getWidth(), $this->getHeight());
244+
232245
case 'scale':
233246
return $image->scale(min($scale_w, $scale_h));
234247

0 commit comments

Comments
 (0)