Skip to content

Commit 6374c69

Browse files
committed
minor improvements and added freecrop method
1 parent 87e7200 commit 6374c69

File tree

4 files changed

+82
-19
lines changed

4 files changed

+82
-19
lines changed

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ use \Eventviva\ImageResize;
4141
$image = new ImageResize();
4242
```
4343

44-
Note: This library uses GD class which do not support resizing animated gif files
44+
> Note: This library uses GD class which do not support resizing animated gif files
4545
46-
Usage
47-
-----
46+
Resize
47+
------
4848

4949
To scale an image, in this case to half it's size (scaling is percentage based):
5050

@@ -53,7 +53,7 @@ $image = new ImageResize('image.jpg');
5353
$image->scale(50);
5454
$image->save('image2.jpg')
5555
```
56-
56+
5757
To resize an image according to one dimension (keeping aspect ratio):
5858

5959
```php
@@ -73,6 +73,27 @@ $image->resizeToBestFit(500, 300);
7373
$image->save('image2.jpg');
7474
```
7575

76+
All resize functions have ```$allow_enlarge``` option which is set to false by default.
77+
You can enable by passing ```true``` to any resize function:
78+
```php
79+
$image = new ImageResize('image.jpg');
80+
$image->resize(500, 300, $allow_enlarge = True);
81+
$image->save('image2.jpg');
82+
```
83+
84+
If you are happy to handle aspect ratios yourself, you can resize directly:
85+
86+
```php
87+
$image = new ImageResize('image.jpg');
88+
$image->resize(800, 600);
89+
$image->save('image2.jpg');
90+
```
91+
92+
This will cause your image to skew if you do not use the same width/height ratio as the source image.
93+
94+
Crop
95+
----
96+
7697
To to crop an image:
7798

7899
```php
@@ -85,16 +106,15 @@ This will scale the image to as close as it can to the passed dimensions, and th
85106

86107
In the case of the example above, an image of 400px × 600px will be resized down to 200px × 300px, and then 50px will be taken off the top and bottom, leaving you with 200px × 200px.
87108

88-
If you are happy to handle aspect ratios yourself, you can resize directly:
109+
There is also a way to define custom crop position.
110+
You can define $x and $y in ```freecrop``` method:
89111

90112
```php
91113
$image = new ImageResize('image.jpg');
92-
$image->resize(800, 600);
114+
$image->freecrop(200, 200, $x = 20, $y = 20);
93115
$image->save('image2.jpg');
94116
```
95117

96-
This will cause your image to skew if you do not use the same width/height ratio as the source image.
97-
98118
Loading and saving images from string
99119
-------------------------------------
100120

@@ -183,12 +203,12 @@ We're passing `null` for the image type in the example above to skip over it and
183203
Interlacing
184204
-----------
185205

186-
By default, [image interlacing](http://php.net/manual/en/function.imageinterlace.php) is turned off. It can be enabled by setting `$interlace` to `1`:
206+
By default, [image interlacing](http://php.net/manual/en/function.imageinterlace.php) is turned on. It can be disabled by setting `$interlace` to `0`:
187207

188208
```php
189209
$image = new ImageResize('image.jpg');
190210
$image->scale(50);
191-
$image->interlace = 1;
211+
$image->interlace = 0;
192212
$image->save('image2.jpg')
193213
```
194214

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
"ext-exif": "*"
1818
},
1919
"autoload": {
20-
"classmap": ["src"]
20+
"classmap": ["lib"]
2121
}
2222
}

src/ImageResize.php renamed to lib/ImageResize.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ImageResize
1919
public $quality_jpg = 75;
2020
public $quality_png = 0;
2121

22-
public $interlace = 0;
22+
public $interlace = 1;
2323

2424
public $source_type;
2525

@@ -213,14 +213,12 @@ public function save($filename, $image_type = null, $quality = null, $permission
213213
*/
214214
public function getImageAsString($image_type = null, $quality = null)
215215
{
216-
$string_temp = tempnam(sys_get_temp_dir(), '');
216+
$string_temp = "php://temp/" . substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5)),0,40);
217217

218-
$this->save($string_temp, $image_type, $quality);
218+
$this->save($string_temp , $image_type, $quality);
219219

220220
$string = file_get_contents($string_temp);
221221

222-
unlink($string_temp);
223-
224222
return $string;
225223
}
226224

@@ -312,7 +310,7 @@ public function resizeToBestFit($max_width, $max_height, $allow_enlarge = false)
312310
* Resizes image according to given scale (proportionally)
313311
*
314312
* @param integer|float $scale
315-
* @return \Eventviva\ImageResize
313+
* @return \static
316314
*/
317315
public function scale($scale)
318316
{
@@ -366,7 +364,7 @@ public function resize($width, $height, $allow_enlarge = false)
366364
* @param integer $position
367365
* @return \static
368366
*/
369-
public function crop($width, $height, $allow_enlarge = false, $position = self::CROPCENTER)
367+
public function crop($width, $height, $position = self::CROPCENTER, $allow_enlarge = false)
370368
{
371369
if (!$allow_enlarge) {
372370
// this logic is slightly different to resize(),
@@ -408,6 +406,40 @@ public function crop($width, $height, $allow_enlarge = false, $position = self::
408406
return $this;
409407
}
410408

409+
/**
410+
* Crops image according to the given width, height, x and y
411+
*
412+
* @param integer $width
413+
* @param integer $height
414+
* @param integer $x
415+
* @param integer $y
416+
* @return \static
417+
*/
418+
public function freecrop($width, $height, $x = false, $y = false)
419+
{
420+
if($x === false or $y === false){
421+
return $this->crop($width, $height);
422+
}
423+
$this->source_x = $x;
424+
$this->source_y = $y;
425+
if($width > $this->getSourceWidth() - $x){
426+
$this->source_w = $this->getSourceWidth() - $x;
427+
} else {
428+
$this->source_w = $width;
429+
}
430+
431+
if($height > $this->getSourceHeight() - $h){
432+
$this->source_h = $this->getSourceHeight() - $h;
433+
} else {
434+
$this->source_h = $height;
435+
}
436+
437+
$this->dest_w = $width;
438+
$this->dest_h = $height;
439+
440+
return $this;
441+
}
442+
411443
/**
412444
* Gets source width
413445
*

test/Test.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
include __DIR__.'/../src/ImageResize.php';
3+
include __DIR__.'/../lib/ImageResize.php';
44

55
use \Eventviva\ImageResize;
66

@@ -209,6 +209,17 @@ public function testCrop()
209209
$this->assertEquals(50, $resize->getDestHeight());
210210
}
211211

212+
public function testFreeCrop()
213+
{
214+
$image = $this->createImage(200, 100, 'png');
215+
$resize = new ImageResize($image);
216+
217+
$resize->freecrop(50, 50 , $x = 20, $y = 20);
218+
219+
$this->assertEquals(50, $resize->getDestWidth());
220+
$this->assertEquals(50, $resize->getDestHeight());
221+
}
222+
212223
public function testCropPosition()
213224
{
214225
$image = $this->createImage(200, 100, 'png');

0 commit comments

Comments
 (0)