Skip to content

Commit cf40f74

Browse files
committed
Added new functions to the Readme
1 parent 30266ef commit cf40f74

File tree

1 file changed

+0
-273
lines changed

1 file changed

+0
-273
lines changed

README.md

Lines changed: 0 additions & 273 deletions
Original file line numberDiff line numberDiff line change
@@ -1,276 +1,3 @@
1-
<<<<<<< HEAD
2-
php-image-resize
3-
================
4-
5-
PHP library to resize, scale and crop images.
6-
7-
[![Build Status](https://travis-ci.org/eventviva/php-image-resize.svg?branch=master)](https://travis-ci.org/eventviva/php-image-resize) [![Latest Stable Version](https://poser.pugx.org/eventviva/php-image-resize/v/stable)](https://packagist.org/packages/eventviva/php-image-resize) [![Monthly Downloads](https://poser.pugx.org/eventviva/php-image-resize/d/monthly)](https://packagist.org/packages/eventviva/php-image-resize)
8-
9-
Setup
10-
-----
11-
12-
This package is available through Packagist with the vendor and package identifier the same as this repo.
13-
14-
If using [Composer](https://getcomposer.org/), in your `composer.json` file add:
15-
16-
```json
17-
{
18-
"require": {
19-
"eventviva/php-image-resize": "1.6.*"
20-
}
21-
}
22-
```
23-
24-
Otherwise:
25-
26-
```php
27-
include '/path/to/ImageResize.php';
28-
```
29-
30-
Because this class uses namespacing, when instantiating the object, you need to either use the fully qualified namespace:
31-
32-
```php
33-
$image = new \Eventviva\ImageResize();
34-
```
35-
36-
Or alias it:
37-
38-
```php
39-
40-
use \Eventviva\ImageResize;
41-
42-
$image = new ImageResize();
43-
```
44-
45-
> Note: This library uses GD class which do not support resizing animated gif files
46-
47-
Resize
48-
------
49-
50-
To scale an image, in this case to half it's size (scaling is percentage based):
51-
52-
```php
53-
$image = new ImageResize('image.jpg');
54-
$image->scale(50);
55-
$image->save('image2.jpg')
56-
```
57-
58-
To resize an image according to one dimension (keeping aspect ratio):
59-
60-
```php
61-
$image = new ImageResize('image.jpg');
62-
$image->resizeToHeight(500);
63-
$image->save('image2.jpg');
64-
65-
$image = new ImageResize('image.jpg');
66-
$image->resizeToWidth(300);
67-
$image->save('image2.jpg');
68-
```
69-
70-
To resize an image according to a given measure regardingless its orientation (keeping aspect ratio):
71-
72-
```php
73-
$image = new ImageResize('image.jpg');
74-
$image->resizeToLongSide(500);
75-
$image->save('image2.jpg');
76-
77-
$image = new ImageResize('image.jpg');
78-
$image->resizeToShortSide(300);
79-
$image->save('image2.jpg');
80-
```
81-
82-
To resize an image to best fit a given set of dimensions (keeping aspet ratio):
83-
```php
84-
$image = new ImageResize('image.jpg');
85-
$image->resizeToBestFit(500, 300);
86-
$image->save('image2.jpg');
87-
```
88-
89-
All resize functions have ```$allow_enlarge``` option which is set to false by default.
90-
You can enable by passing ```true``` to any resize function:
91-
```php
92-
$image = new ImageResize('image.jpg');
93-
$image->resize(500, 300, $allow_enlarge = True);
94-
$image->save('image2.jpg');
95-
96-
$image = new ImageResize('image.jpg');
97-
$image->resizeToShortSide(300, $allow_enlarge = True);
98-
$image->save('image2.jpg');
99-
```
100-
101-
If you are happy to handle aspect ratios yourself, you can resize directly:
102-
103-
```php
104-
$image = new ImageResize('image.jpg');
105-
$image->resize(800, 600);
106-
$image->save('image2.jpg');
107-
```
108-
109-
This will cause your image to skew if you do not use the same width/height ratio as the source image.
110-
111-
Crop
112-
----
113-
114-
To to crop an image:
115-
116-
```php
117-
$image = new ImageResize('image.jpg');
118-
$image->crop(200, 200);
119-
$image->save('image2.jpg');
120-
```
121-
122-
This will scale the image to as close as it can to the passed dimensions, and then crop and center the rest.
123-
124-
In the case of the example above, an image of 400px &times; 600px will be resized down to 200px &times; 300px, and then 50px will be taken off the top and bottom, leaving you with 200px &times; 200px.
125-
126-
There is also a way to define custom crop position.
127-
You can define $x and $y in ```freecrop``` method:
128-
129-
```php
130-
$image = new ImageResize('image.jpg');
131-
$image->freecrop(200, 200, $x = 20, $y = 20);
132-
$image->save('image2.jpg');
133-
```
134-
135-
Loading and saving images from string
136-
-------------------------------------
137-
138-
To load an image from a string:
139-
140-
```php
141-
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
142-
$image->scale(50);
143-
$image->save('image.jpg');
144-
```
145-
146-
You can also return the result as a string:
147-
148-
```php
149-
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
150-
$image->scale(50);
151-
echo $image->getImageAsString();
152-
```
153-
154-
Magic `__toString()` is also supported:
155-
156-
```php
157-
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
158-
$image->resize(10, 10);
159-
echo (string)$image;
160-
```
161-
162-
Displaying
163-
----------
164-
165-
As seen above, you can call `$image->save('image.jpg');`
166-
167-
To render the image directly into the browser, you can call `$image->output()`;
168-
169-
Image Types
170-
-----------
171-
172-
When saving to disk or outputting into the browser, the script assumes the same output type as input.
173-
174-
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):
175-
176-
- `IMAGETYPE_GIF`
177-
- `IMAGETYPE_JPEG`
178-
- `IMAGETYPE_PNG`
179-
180-
This allows you to save in a different type to the source:
181-
182-
```php
183-
$image = new ImageResize('image.jpg');
184-
$image->resize(800, 600);
185-
$image->save('image.png', IMAGETYPE_PNG);
186-
```
187-
188-
Quality
189-
-------
190-
191-
The properties `$quality_jpg` and `$quality_png` are available for you to configure:
192-
193-
```php
194-
$image = new ImageResize('image.jpg');
195-
$image->quality_jpg = 100;
196-
$image->resize(800, 600);
197-
$image->save('image2.jpg');
198-
```
199-
200-
By default they are set to 85 and 6 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.
201-
202-
You can also pass the quality directly to the `save()`, `output()` and `getImageAsString()` methods:
203-
204-
```php
205-
$image = new ImageResize('image.jpg');
206-
$image->crop(200, 200);
207-
$image->save('image2.jpg', null, 100);
208-
209-
$image = new ImageResize('image.jpg');
210-
$image->resizeToWidth(300);
211-
$image->output(IMAGETYPE_PNG, 4);
212-
213-
$image = new ImageResize('image.jpg');
214-
$image->scale(50);
215-
$result = $image->getImageAsString(IMAGETYPE_PNG, 4);
216-
```
217-
218-
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.
219-
220-
Interlacing
221-
-----------
222-
223-
By default, [image interlacing](http://php.net/manual/en/function.imageinterlace.php) is turned on. It can be disabled by setting `$interlace` to `0`:
224-
225-
```php
226-
$image = new ImageResize('image.jpg');
227-
$image->scale(50);
228-
$image->interlace = 0;
229-
$image->save('image2.jpg')
230-
```
231-
232-
Chaining
233-
--------
234-
235-
When performing operations, the original image is retained, so that you can chain operations without excessive destruction.
236-
237-
This is useful for creating multiple sizes:
238-
239-
```php
240-
$image = new ImageResize('image.jpg');
241-
$image
242-
->scale(50)
243-
->save('image2.jpg')
244-
245-
->resizeToWidth(300)
246-
->save('image3.jpg')
247-
248-
->crop(100, 100)
249-
->save('image4.jpg')
250-
;
251-
```
252-
253-
Exceptions
254-
--------
255-
256-
ImageResize throws ImageResizeException for it's own for errors. You can catch that or catch the general \Exception which it's extending.
257-
258-
It is not to be expected, but should anything go horribly wrong mid way then notice or warning Errors could be shown from the PHP GD and Image Functions (http://php.net/manual/en/ref.image.php)
259-
260-
```php
261-
try{
262-
$image = new ImageResize(null);
263-
echo "This line will not be printed";
264-
} catch (ImageResizeException $e) {
265-
echo "Something went wrong" . $e->getMessage();
266-
}
267-
```
268-
269-
API Doc
270-
-------
271-
272-
https://eventviva.github.io/php-image-resize/class-Eventviva.ImageResize.html
273-
=======
2741
php-image-resize
2752
================
2763

0 commit comments

Comments
 (0)