Skip to content

Commit 30266ef

Browse files
committed
Merge branch
2 parents 419a9f6 + f84e175 commit 30266ef

File tree

1 file changed

+270
-0
lines changed

1 file changed

+270
-0
lines changed

README.md

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
php-image-resize
23
================
34

@@ -269,3 +270,272 @@ API Doc
269270
-------
270271

271272
https://eventviva.github.io/php-image-resize/class-Eventviva.ImageResize.html
273+
=======
274+
php-image-resize
275+
================
276+
277+
PHP library to resize, scale and crop images.
278+
279+
[![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)
280+
281+
Setup
282+
-----
283+
284+
This package is available through Packagist with the vendor and package identifier the same as this repo.
285+
286+
If using [Composer](https://getcomposer.org/), in your `composer.json` file add:
287+
288+
```json
289+
{
290+
"require": {
291+
"eventviva/php-image-resize": "1.6.*"
292+
}
293+
}
294+
```
295+
296+
Otherwise:
297+
298+
```php
299+
include '/path/to/ImageResize.php';
300+
```
301+
302+
Because this class uses namespacing, when instantiating the object, you need to either use the fully qualified namespace:
303+
304+
```php
305+
$image = new \Eventviva\ImageResize();
306+
```
307+
308+
Or alias it:
309+
310+
```php
311+
312+
use \Eventviva\ImageResize;
313+
314+
$image = new ImageResize();
315+
```
316+
317+
> Note: This library uses GD class which do not support resizing animated gif files
318+
319+
Resize
320+
------
321+
322+
To scale an image, in this case to half it's size (scaling is percentage based):
323+
324+
```php
325+
$image = new ImageResize('image.jpg');
326+
$image->scale(50);
327+
$image->save('image2.jpg')
328+
```
329+
330+
To resize an image according to one dimension (keeping aspect ratio):
331+
332+
```php
333+
$image = new ImageResize('image.jpg');
334+
$image->resizeToHeight(500);
335+
$image->save('image2.jpg');
336+
337+
$image = new ImageResize('image.jpg');
338+
$image->resizeToWidth(300);
339+
$image->save('image2.jpg');
340+
```
341+
342+
To resize an image according to a given measure regardingless its orientation (keeping aspect ratio):
343+
344+
```php
345+
$image = new ImageResize('image.jpg');
346+
$image->resizeToLongSide(500);
347+
$image->save('image2.jpg');
348+
349+
$image = new ImageResize('image.jpg');
350+
$image->resizeToShortSide(300);
351+
$image->save('image2.jpg');
352+
```
353+
354+
To resize an image to best fit a given set of dimensions (keeping aspet ratio):
355+
```php
356+
$image = new ImageResize('image.jpg');
357+
$image->resizeToBestFit(500, 300);
358+
$image->save('image2.jpg');
359+
```
360+
361+
All resize functions have ```$allow_enlarge``` option which is set to false by default.
362+
You can enable by passing ```true``` to any resize function:
363+
```php
364+
$image = new ImageResize('image.jpg');
365+
$image->resize(500, 300, $allow_enlarge = True);
366+
$image->save('image2.jpg');
367+
```
368+
369+
If you are happy to handle aspect ratios yourself, you can resize directly:
370+
371+
```php
372+
$image = new ImageResize('image.jpg');
373+
$image->resize(800, 600);
374+
$image->save('image2.jpg');
375+
```
376+
377+
This will cause your image to skew if you do not use the same width/height ratio as the source image.
378+
379+
Crop
380+
----
381+
382+
To to crop an image:
383+
384+
```php
385+
$image = new ImageResize('image.jpg');
386+
$image->crop(200, 200);
387+
$image->save('image2.jpg');
388+
```
389+
390+
This will scale the image to as close as it can to the passed dimensions, and then crop and center the rest.
391+
392+
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.
393+
394+
There is also a way to define custom crop position.
395+
You can define $x and $y in ```freecrop``` method:
396+
397+
```php
398+
$image = new ImageResize('image.jpg');
399+
$image->freecrop(200, 200, $x = 20, $y = 20);
400+
$image->save('image2.jpg');
401+
```
402+
403+
Loading and saving images from string
404+
-------------------------------------
405+
406+
To load an image from a string:
407+
408+
```php
409+
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
410+
$image->scale(50);
411+
$image->save('image.jpg');
412+
```
413+
414+
You can also return the result as a string:
415+
416+
```php
417+
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
418+
$image->scale(50);
419+
echo $image->getImageAsString();
420+
```
421+
422+
Magic `__toString()` is also supported:
423+
424+
```php
425+
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
426+
$image->resize(10, 10);
427+
echo (string)$image;
428+
```
429+
430+
Displaying
431+
----------
432+
433+
As seen above, you can call `$image->save('image.jpg');`
434+
435+
To render the image directly into the browser, you can call `$image->output()`;
436+
437+
Image Types
438+
-----------
439+
440+
When saving to disk or outputting into the browser, the script assumes the same output type as input.
441+
442+
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):
443+
444+
- `IMAGETYPE_GIF`
445+
- `IMAGETYPE_JPEG`
446+
- `IMAGETYPE_PNG`
447+
448+
This allows you to save in a different type to the source:
449+
450+
```php
451+
$image = new ImageResize('image.jpg');
452+
$image->resize(800, 600);
453+
$image->save('image.png', IMAGETYPE_PNG);
454+
```
455+
456+
Quality
457+
-------
458+
459+
The properties `$quality_jpg` and `$quality_png` are available for you to configure:
460+
461+
```php
462+
$image = new ImageResize('image.jpg');
463+
$image->quality_jpg = 100;
464+
$image->resize(800, 600);
465+
$image->save('image2.jpg');
466+
```
467+
468+
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.
469+
470+
You can also pass the quality directly to the `save()`, `output()` and `getImageAsString()` methods:
471+
472+
```php
473+
$image = new ImageResize('image.jpg');
474+
$image->crop(200, 200);
475+
$image->save('image2.jpg', null, 100);
476+
477+
$image = new ImageResize('image.jpg');
478+
$image->resizeToWidth(300);
479+
$image->output(IMAGETYPE_PNG, 4);
480+
481+
$image = new ImageResize('image.jpg');
482+
$image->scale(50);
483+
$result = $image->getImageAsString(IMAGETYPE_PNG, 4);
484+
```
485+
486+
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.
487+
488+
Interlacing
489+
-----------
490+
491+
By default, [image interlacing](http://php.net/manual/en/function.imageinterlace.php) is turned on. It can be disabled by setting `$interlace` to `0`:
492+
493+
```php
494+
$image = new ImageResize('image.jpg');
495+
$image->scale(50);
496+
$image->interlace = 0;
497+
$image->save('image2.jpg')
498+
```
499+
500+
Chaining
501+
--------
502+
503+
When performing operations, the original image is retained, so that you can chain operations without excessive destruction.
504+
505+
This is useful for creating multiple sizes:
506+
507+
```php
508+
$image = new ImageResize('image.jpg');
509+
$image
510+
->scale(50)
511+
->save('image2.jpg')
512+
513+
->resizeToWidth(300)
514+
->save('image3.jpg')
515+
516+
->crop(100, 100)
517+
->save('image4.jpg')
518+
;
519+
```
520+
521+
Exceptions
522+
--------
523+
524+
ImageResize throws ImageResizeException for it's own for errors. You can catch that or catch the general \Exception which it's extending.
525+
526+
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)
527+
528+
```php
529+
try{
530+
$image = new ImageResize(null);
531+
echo "This line will not be printed";
532+
} catch (ImageResizeException $e) {
533+
echo "Something went wrong" . $e->getMessage();
534+
}
535+
```
536+
537+
API Doc
538+
-------
539+
540+
https://eventviva.github.io/php-image-resize/class-Eventviva.ImageResize.html
541+
>>>>>>> f84e175149be8a6058e196408d59a1c05b509345

0 commit comments

Comments
 (0)