Skip to content

Commit

Permalink
add crop mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AlloVince committed Aug 8, 2012
1 parent 9099e32 commit 341d76b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 15 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Sometimes we don't want to expose images source url, EvaCloudImage allow you cre
- Image source file : [http://evacloudimage.avnpc.com/upload/demo.jpg](http://evacloudimage.avnpc.com/upload/demo.jpg)
- Shadow image : [http://evacloudimage.avnpc.com/thumb/demo.jpg](http://evacloudimage.avnpc.com/thumb/demo.jpg), use this url in your website, visitor will not know source file url.

Shadow pretect support multi-level directory structure, if cache enabled, all folders in URL will be created.

Resize Dimensions
-----------------

Expand All @@ -32,11 +34,11 @@ The following URL points to a 300px width dynamically created image, pass the '*

###Resize by height:

The following URL points to a 200px height dynamically created image, pass the '*h*' parameter by '*h_200*':
The following URL points to a 150px height dynamically created image, pass the '*h*' parameter by '*h_150*':

http://evacloudimage.avnpc.com/thumb/demo,h_200.jpg
http://evacloudimage.avnpc.com/thumb/demo,h_150.jpg

![EvaCloudImage Resized Image](http://evacloudimage.avnpc.com/thumb/demo,h_200.jpg)
![EvaCloudImage Resized Image](http://evacloudimage.avnpc.com/thumb/demo,h_150.jpg)

###Resize by percent:

Expand All @@ -48,6 +50,23 @@ For example, resizing the demo image to *40%* of its original size is done by se

![EvaCloudImage Resized Image](http://evacloudimage.avnpc.com/thumb/demo,w_0.4.jpg)


Crop
----

Crop use '*c*' as parameter, under crop mode, you could pass an integer value for croppin from the center of the image.

http://evacloudimage.avnpc.com/thumb/demo,c_100.jpg

![EvaCloudImage Resized Image](http://evacloudimage.avnpc.com/thumb/demo,c_100.jpg)

Cropping fixed coordinates of image, by using the 'x' and 'y' parameters. Also the width and height parameters is required.

http://evacloudimage.avnpc.com/thumb/demo,c_crop,h_200,w_100,x_80.jpg

![EvaCloudImage Resized Image](http://evacloudimage.avnpc.com/thumb/demo,c_crop,h_200,w_100,x_80.jpg)


Rotate
-----------------

Expand Down Expand Up @@ -114,6 +133,3 @@ EvaCloudImage uses below open source projects to work properly:
Thanks to
---------
Demo image is from [Рыбачка](http://nzakonova.35photo.ru/photo_391467/), great shot!



86 changes: 77 additions & 9 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public function getUniqueTargetImageName()

$sourceImageName = $this->getSourceImageName();

$this->getUniqueParameters();
$argString = $this->parametersToString();
if(!$argString){
return $this->uniqueTargetImageName = $sourceImageName;
Expand All @@ -257,6 +258,8 @@ public function getUniqueTargetImageName()
return $this->uniqueTargetImageName = $uniqueName;
}



public function getSourceImage()
{
if($this->sourceImage){
Expand Down Expand Up @@ -339,36 +342,101 @@ public function show()
}
}

public function transferImage(GdThumb $thumb)
public function getUniqueParameters()
{
$params = $this->getTransferParameters();
if($params['width'] || $params['height']) {

//Convert string to float or int
$requireResize = true;

if($params['width'] || $params['height']) {
$params['width'] = $params['width'] ? $params['width'] + 0 : null;
$params['height'] = $params['height'] ? $params['height'] + 0 : null;

if(is_int($params['width']) && is_int($params['height'])){
$thumb->resize($params['width'], $params['height']);
} elseif(is_int($params['width']) || is_int($params['height'])){
//resize by fixed number first
$params['width'] = !$params['width'] || is_float($params['width']) ? 0 : $params['width'];
$params['height'] = !$params['height'] || is_float($params['height']) ? 0 : $params['height'];
$thumb->resize($params['width'], $params['height']);
} else {
$percent = $params['width'];
$percent = !$percent || $percent > 0 && $percent < $params['height'] ? $params['height'] : $percent;
$percent = $percent * 100;
$thumb->resizePercent($percent);
$params['width'] = $percent;
$params['height'] = null;
}
}

if($params['x'] || $params['y']) {
$params['x'] = $params['x'] ? $params['x'] + 0 : null;
$params['y'] = $params['y'] ? $params['y'] + 0 : null;
}

if($params['crop']){
$requireResize = false;

if(is_numeric($params['crop'])){
$params['crop'] = $params['crop'] + 0;
} elseif($params['crop'] == 'crop'){
if(!$params['x'] && !$params['y']){
$params['crop'] = null;
}
} else {
$params['crop'] = null;
}
}

if($params['rotate']){
$allowRotate = array('CW', 'CCW');
if(is_numeric($params['rotate'])){
$params['rotate'] = $params['rotate'] + 0;
} elseif(!in_array($params['rotate'], $allowRotate)) {
$params['rotate'] = null;
}
}

if($params['quality']){
if(is_numeric($params['quality'])){
$params['quality'] = $params['quality'] + 0;
} else {
$params['quality'] = null;
}
}

return $this->transferParameters = $params;
}

public function transferImage(GdThumb $thumb)
{
$params = $this->getTransferParameters();

$requireResize = true;

if($params['crop']){
if(is_int($params['crop'])){
$thumb->cropFromCenter($params['crop']);
$requireResize = false;
}

if($params['crop'] == 'crop' && ($params['x'] || $params['y'])){
$params['x'] = $params['x'] ? $params['x'] : 0;
$params['y'] = $params['y'] ? $params['y'] : 0;
$thumb->crop($params['x'], $params['y'], $params['width'], $params['height']);
$requireResize = false;
}
}

if(true === $requireResize && ($params['width'] || $params['height'])) {
if(is_int($params['width']) && is_int($params['height'])){
$thumb->resize($params['width'], $params['height']);
} else {
$percent = $params['width'];
$percent = $percent * 100;
$thumb->resizePercent($percent);
}
}

if($params['rotate']){
if(is_int($params['rotate'])){
$thumb->rotateImageNDegrees($params['rotate']);
} elseif(in_array($params['rotate'], $allowRotate)) {
} else {
$thumb->rotateImage($params['rotate']);
}
}
Expand Down

0 comments on commit 341d76b

Please sign in to comment.