Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- nightly

before_script:
- composer install

script:
- mkdir -p build/logs
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_script:
- php vendor/bin/coveralls -v
37 changes: 25 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ In the case of the example above, an image of 400px × 600px will be resize

Crop modes:

Few crop mode options are available in order for you to choose how you want to handle the eventual exceeding width or height after resizing down your image.
Few crop mode options are available in order for you to choose how you want to handle the eventual exceeding width or height after resizing down your image.
The default crop mode used is the `CROPCENTER`.
As a result those pieces of code are equivalent:
As a result those pieces of code are equivalent:

```php
$image = new ImageResize('image.jpg');
Expand Down Expand Up @@ -269,7 +269,7 @@ By default, [image interlacing](http://php.net/manual/en/function.imageinterlace
$image = new ImageResize('image.jpg');
$image->scale(50);
$image->interlace = 0;
$image->save('image2.jpg')
$image->save('image2.jpg');
```

Chaining
Expand Down Expand Up @@ -305,26 +305,26 @@ try{
$image = new ImageResize(null);
echo "This line will not be printed";
} catch (ImageResizeException $e) {
echo "Something went wrong" . $e->getMessage();
echo "Something went wrong" . $e->getMessage();
}
```

Filters
--------
You can apply special effects for new image like blur or add banner.

You can apply special effects for new image like blur or add banner.

```php
$image = new ImageResize('image.jpg');

// Add blure
$image->addFIlter(function ($imageDesc) {
$image->addFilter(function ($imageDesc) {
imagefilter($imageDesc, IMG_FILTER_GAUSSIAN_BLUR);
});

// Add banner on bottom left corner
$image18Plus = 'banner.png'
$image->addFIlter(function ($imageDesc) use ($image18Plus) {
$image->addFilter(function ($imageDesc) use ($image18Plus) {
$logo = imagecreatefrompng($image18Plus);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
Expand All @@ -336,10 +336,23 @@ $image->addFIlter(function ($imageDesc) use ($image18Plus) {
});

```


Flip
--------

Flips an image using a given mode and this method is only for PHP version 5.4.

```php
$flip = new ImageResize('image.png');
$image = imagecreatetruecolor(200, 100);

$flip->imageFlip($image, 0);

```

Both functions will be used in the order in which they were added.


API Doc
-------

Expand Down
19 changes: 15 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@
}
],
"require": {
"php": ">=5.3.0",
"ext-gd": "*"
"php": ">=5.4.0",
"ext-gd": "*",
"ext-fileinfo": "*"
},
"suggest": {
"ext-exif": "Auto-rotate jpeg files"
},
"autoload": {
"classmap": ["lib"]
"psr-4": {
"Gumlet\\": "lib/"
}
},
"autoload-dev": {
"psr-4": {
"Gumlet\\": "test/"
}
},
"require-dev": {
"apigen/apigen": "^4.1"
"phpunit/phpunit": "^4.8",
"php-coveralls/php-coveralls": "dev-master || ^1.0",
"ext-exif": "*",
"ext-gd": "*"
}
}
107 changes: 53 additions & 54 deletions lib/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class ImageResize
const CROPLEFT = 4;
const CROPRIGHT = 5;
const CROPTOPCENTER = 6;
const IMG_FLIP_HORIZONTAL = 0;
const IMG_FLIP_VERTICAL = 1;
const IMG_FLIP_BOTH = 2;

public $quality_jpg = 85;
public $quality_webp = 85;
Expand Down Expand Up @@ -79,11 +82,12 @@ public function addFilter(callable $filter)
* Apply filters.
*
* @param $image resource an image resource identifier
* @param $filterType filter type and default value is IMG_FILTER_NEGATE
*/
protected function applyFilter($image)
protected function applyFilter($image, $filterType = IMG_FILTER_NEGATE)
{
foreach ($this->filters as $function) {
$function($image);
$function($image, $filterType);
}
}

Expand Down Expand Up @@ -149,7 +153,6 @@ public function __construct($filename)

default:
throw new ImageResizeException('Unsupported image type');
break;
}

if (!$this->source_image) {
Expand Down Expand Up @@ -185,7 +188,11 @@ public function imageCreateJpegfromExif($filename)
}

if ($orientation === 5 || $orientation === 4 || $orientation === 7) {
imageflip($img, IMG_FLIP_HORIZONTAL);
if(function_exists('imageflip')) {
imageflip($img, IMG_FLIP_HORIZONTAL);
} else {
$this->imageFlip($img, IMG_FLIP_HORIZONTAL);
}
}

return $img;
Expand Down Expand Up @@ -571,7 +578,7 @@ public function crop($width, $height, $allow_enlarge = false, $position = self::
*/
public function freecrop($width, $height, $x = false, $y = false)
{
if ($x === false or $y === false) {
if ($x === false || $y === false) {
return $this->crop($width, $height);
}
$this->source_x = $x;
Expand Down Expand Up @@ -658,59 +665,51 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
}
return $size;
}
}

// imageflip definition for PHP < 5.5
if (!function_exists('imageflip')) {
define('IMG_FLIP_HORIZONTAL', 0);
define('IMG_FLIP_VERTICAL', 1);
define('IMG_FLIP_BOTH', 2);

function imageflip($image, $mode)
/**
* Flips an image using a given mode if PHP version is lower than 5.5
*
* @param resource $image
* @param integer $mode
* @return null
*/
public function imageFlip($image, $mode)
{
switch ($mode) {
case IMG_FLIP_HORIZONTAL: {
$max_x = imagesx($image) - 1;
$half_x = $max_x / 2;
$sy = imagesy($image);
$temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy);
for ($x = 0; $x < $half_x; ++$x) {
imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy);
imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy);
imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy);
switch($mode) {
case self::IMG_FLIP_HORIZONTAL: {
$max_x = imagesx($image) - 1;
$half_x = $max_x / 2;
$sy = imagesy($image);
$temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy);
for ($x = 0; $x < $half_x; ++$x) {
imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy);
imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy);
imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy);
}
break;
}
break;
}
case IMG_FLIP_VERTICAL: {
$sx = imagesx($image);
$max_y = imagesy($image) - 1;
$half_y = $max_y / 2;
$temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1);
for ($y = 0; $y < $half_y; ++$y) {
imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1);
imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1);
imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1);
}
break;
}
case IMG_FLIP_BOTH: {
$sx = imagesx($image);
$sy = imagesy($image);
$temp_image = imagerotate($image, 180, 0);
imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy);
break;
}
default: {
return;
}
case self::IMG_FLIP_VERTICAL: {
$sx = imagesx($image);
$max_y = imagesy($image) - 1;
$half_y = $max_y / 2;
$temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1);
for ($y = 0; $y < $half_y; ++$y) {
imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1);
imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1);
imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1);
}
break;
}
case self::IMG_FLIP_BOTH: {
$sx = imagesx($image);
$sy = imagesy($image);
$temp_image = imagerotate($image, 180, 0);
imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy);
break;
}
default:
return null;
}
imagedestroy($temp_image);
}
}

/**
* PHP Exception used in the ImageResize class
*/
class ImageResizeException extends \Exception
{
}
10 changes: 10 additions & 0 deletions lib/ImageResizeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Gumlet;

/**
* PHP Exception used in the ImageResize class
*/
class ImageResizeException extends \Exception
{
}
17 changes: 8 additions & 9 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<phpunit colors="true">
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true" backupGlobals="false"
backupStaticAttributes="false" syntaxCheck="false">
<testsuites>
<testsuite name="php-image-resize tests">
<directory>test</directory>
<testsuite name="Tests">
<directory suffix="Test.php">test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">lib</directory>
</whitelist>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">lib</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-text" target="php://stdout"/>
</logging>
</phpunit>
44 changes: 44 additions & 0 deletions test/ImageResizeExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use \Gumlet\ImageResize;
use \Gumlet\ImageResizeException;
use \PHPUnit\Framework\TestCase;

class ImageResizeExceptionTest extends TestCase
{
public function testExceptionEmpty()
{
$e = new ImageResizeException();

$this->assertEquals("", $e->getMessage());
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
}

public function testExceptionMessage()
{
$e = new ImageResizeException("General error");

$this->assertEquals("General error", $e->getMessage());
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
}

public function testExceptionExtending()
{
$e = new ImageResizeException("General error");

$this->assertInstanceOf('\Exception', $e);
}

public function testExceptionThrown()
{
try{
throw new ImageResizeException("General error");
} catch (\Exception $e) {
$this->assertEquals("General error", $e->getMessage());
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
return;
}
$this->fail();
}
}
// It's pretty easy to get your attention these days, isn't it? :D
Loading