Skip to content

Commit 645b1ce

Browse files
committed
Merge pull request #4 from cainmi/master
Better crop, cleaner chaining
2 parents 5ddd47d + 6b6f91f commit 645b1ce

File tree

2 files changed

+344
-111
lines changed

2 files changed

+344
-111
lines changed

README.md

Lines changed: 144 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,160 @@
11
php-image-resize
22
================
33

4-
PHP class to re-size and scale images
4+
PHP class to resize and scale images.
55

6-
### USAGE:
7-
If you want to scale image:
6+
Setup
7+
-----
8+
9+
This package is available through Packagist with the vendor and package identifier the same as this repo.
10+
11+
If using [Composer](https://getcomposer.org/), in your `composer.json` file add:
12+
13+
```json
14+
{
15+
"require": {
16+
"eventviva/php-image-resize": "dev-master"
17+
}
18+
}
19+
```
20+
21+
Otherwise:
822

923
```php
10-
$image = new ImageResize('picture.jpg');
24+
include '/path/to/ImageResize.php';
25+
```
26+
27+
Because this class uses namespacing, when instantiating the object, you need to either use the fully qualified namespace:
28+
29+
```php
30+
$image = new \Eventviva\ImageResize();
31+
```
32+
33+
Or alias it:
34+
35+
```php
36+
37+
use \Eventviva\ImageResize;
38+
39+
$image = new ImageResize();
40+
```
41+
42+
Usage
43+
-----
44+
45+
To scale an image, in this case to half it's size (scaling is percentage based):
46+
47+
```php
48+
$image = new ImageResize('image.jpg');
1149
$image->scale(50);
12-
$image->save('picture2.jpg')
50+
$image->save('image2.jpg')
1351
```
14-
15-
If you want to resize image:
52+
53+
To resize an image according to one dimension (keeping aspect ratio):
54+
1655
```php
17-
$image = new ImageResize('picture.jpg');
56+
$image = new ImageResize('image.jpg');
1857
$image->resizeToHeight(500);
19-
$image->save('picture2.jpg');
20-
$image->resizeToWidth(200);
21-
$image->save('picture3.jpg');
58+
$image->save('image2.jpg');
59+
60+
$image = new ImageResize('image.jpg');
61+
$image->resizeToWidth(300);
62+
$image->save('image2.jpg');
63+
```
64+
65+
To to crop an image:
66+
67+
```php
68+
$image = new ImageResize('image.jpg');
69+
$image->crop(200, 200);
70+
$image->save('image2.jpg');
71+
```
72+
73+
This will scale the image to as close as it can to the passed dimensions, and then crop and center the rest.
74+
75+
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.
76+
77+
If you are happy to handle aspect ratios yourself, you can resize directly:
78+
79+
```php
80+
$image = new ImageResize('image.jpg');
81+
$image->resize(800, 600);
82+
$image->save('image2.jpg');
2283
```
2384

24-
If you want to scale image:
85+
This will cause your image to skew if you do not use the same width/height ratio as the source image.
86+
87+
Displaying
88+
----------
89+
90+
As seen above, you can call `$image->save('image.jpg');`
91+
92+
To render the image directly into the browser, you can call `$image->output()`;
93+
94+
Image Types
95+
-----------
96+
97+
When saving to disk or outputting into the browser, the script assumes the same output type as input.
98+
99+
If you would like to save/output in a different image type, you need to pass a (supported) PHP [`IMAGETYPE_`* constant](http://www.php.net/manual/en/image.constants.php):
100+
101+
- `IMAGETYPE_GIF`
102+
- `IMAGETYPE_JPEG`
103+
- `IMAGETYPE_PNG`
104+
105+
This allows you to save in a different type to the source:
106+
25107
```php
26-
$image = new ImageResize('picture.jpg');
27-
$image->scale(256,512);
28-
$image->save('picture4.jpg');
108+
$image = new ImageResize('image.jpg');
109+
$image->resize(800, 600);
110+
$image->save('image.png', IMAGETYPE_PNG);
29111
```
30-
31-
If you want to crop image:
112+
113+
Quality
114+
-------
115+
116+
The properties `$quality_jpg` and `$quality_png` are available for you to configure:
117+
32118
```php
33-
$image = new ImageResize('picture.jpg');
34-
$image->crop(512,512);
35-
$image->save('picture5.jpg');
119+
$image = new ImageResize('image.jpg');
120+
$image->quality_png = 100;
121+
$image->resize(800, 600);
122+
$image->save('image2.jpg');
36123
```
124+
125+
By default they are set to 75 and 0 respectively. See the manual entries for [`imagejpeg()`](http://www.php.net/manual/en/function.imagejpeg.php) and [`imagepng()`](http://www.php.net/manual/en/function.imagepng.php) for more info.
126+
127+
You can also pass the quality directly to the `save()` and `output()` methods:
128+
129+
```php
130+
$image = new ImageResize('image.jpg');
131+
$image->crop(200, 200);
132+
$image->save('image2.jpg', null, 100);
133+
134+
$image = new ImageResize('image.jpg');
135+
$image->resizeToWidth(300);
136+
$image->output(IMAGETYPE_PNG, 4);
137+
```
138+
139+
We're passing `null` for the image type in the example above to skip over it and provide the quality. In this case, the image type is assumed to be the same as the input.
140+
141+
Chaining
142+
--------
143+
144+
When performing operations, the original image is retained, so that you can chain operations without excessive destruction.
145+
146+
This is useful for creating multiple sizes:
147+
148+
```php
149+
$image = new ImageResize('image.jpg');
150+
$image
151+
->scale(50)
152+
->save('image2.jpg')
153+
154+
->resizeToWidth(300)
155+
->save('image3.jpg')
156+
157+
->crop(100, 100)
158+
->save('image4.jpg')
159+
;
160+
```

0 commit comments

Comments
 (0)