Skip to content
Closed
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
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,49 @@ $image
;
```

Filters
--------

ImageResize allows you to apply filters to the image such as grayscale and colorizing

Grayscale:

```php
$image->grayscale();
```

Invert the image colors:

```php
$image->invert();
```

Pixelate the image:

```php
$image->pixelate();
$image->pixelate($amount = 20);
```

Colorize:

`$red`, `$green` and `$blue` variables aree all between 0 and 255. `$alpha` is between 0 (transparent) and 100 (opaque)

```php
$image->colorize($red = 255, $green = 10, $blue = 80, $alpha = 50);
```

Filters can be chained too:

```php
$image = new ImageResize('image.jpg');
$image
->invert()
->pixelate(2)
->grayscale();
```


Exceptions
--------

Expand Down
96 changes: 80 additions & 16 deletions lib/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ImageResize

public $source_type;

private $filter = array();

protected $source_image;

protected $original_w;
Expand All @@ -39,7 +41,7 @@ class ImageResize

protected $source_w;
protected $source_h;

protected $source_info;

/**
Expand All @@ -51,9 +53,9 @@ class ImageResize
*/
public static function createFromString($image_data)
{
if(empty($image_data) || $image_data === null) {
throw new ImageResizeException('image_data must not be empty');
}
if(empty($image_data) || $image_data === null) {
throw new ImageResizeException('image_data must not be empty');
}
$resize = new self('data://application/octet-stream;base64,' . base64_encode($image_data));
return $resize;
}
Expand All @@ -69,14 +71,14 @@ public function __construct($filename)
{

if($filename === null || empty($filename) || (substr($filename,0,7) !== 'data://' && !is_file($filename))) {
throw new ImageResizeException('File does not exist');
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if(strstr(finfo_file($finfo, $filename),'image') === false) {
throw new ImageResizeException('Unsupported file type');
}
throw new ImageResizeException('File does not exist');
}

$finfo = finfo_open(FILEINFO_MIME_TYPE);
if(strstr(finfo_file($finfo, $filename),'image') === false) {
throw new ImageResizeException('Unsupported file type');
}

if (!$image_info = getimagesize($filename, $this->source_info)) {
$image_info = getimagesize($filename);
}
Expand Down Expand Up @@ -124,11 +126,11 @@ public function __construct($filename)
// http://stackoverflow.com/a/28819866
public function imageCreateJpegfromExif($filename){
$img = imagecreatefromjpeg($filename);

if (!function_exists('exif_read_data') || !isset($this->source_info['APP1']) || strpos ($this->source_info['APP1'], 'Exif') !== 0) {
return $img;
}

$exif = exif_read_data($filename);

if (!$exif || !isset($exif['Orientation'])){
Expand Down Expand Up @@ -214,6 +216,14 @@ public function save($filename, $image_type = null, $quality = null, $permission
$this->source_h
);

// If filters have been applied loop through and apply each one with params
if(count($this->filter) > 0) {
foreach($this->filter as $filter) {
array_unshift($filter, $dest_image);
call_user_func_array('imagefilter', $filter);
}
}

switch ($image_type) {
case IMAGETYPE_GIF:
imagegif($dest_image, $filename);
Expand Down Expand Up @@ -309,7 +319,7 @@ public function resizeToShortSide($max_short, $allow_enlarge = false)

$this->resize($max_short, $long, $allow_enlarge);
}

return $this;
}

Expand All @@ -333,7 +343,7 @@ public function resizeToLongSide($max_long, $allow_enlarge = false)

$this->resize($max_long, $short, $allow_enlarge);
}

return $this;
}

Expand Down Expand Up @@ -595,6 +605,60 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
}
return $size;
}

/**
* Makes the image grayscale
*
* @return \static
*/
public function grayscale()
{
$this->filter[] = array(IMG_FILTER_GRAYSCALE);
return $this;
}

/**
* Inverts the image colors
*
* @return \static
*/
public function invert()
{
$this->filter[] = array(IMG_FILTER_NEGATE);
return $this;
}

/**
* Pixellates the image by the specified amount
*
* @param integer $amount
* @return \static
*/
public function pixelate($amount = 5)
{
$this->filter[] = array(IMG_FILTER_PIXELATE, $amount, false);
return $this;
}

/**
* Colorises the image with the specificed rgba color
*
* @param integer $red 0 - 255
* @param integer $green 0 - 255
* @param integer $blue 0 - 255
* @param integer $alpha 0 - 100
* @return \static
*/
public function colorize($red = 0, $green = 0, $blue = 0, $alpha = 100)
{
// Function has alpha from 0 - 100, but alpha is actually 0 - 127.
$alpha = ($alpha * 127) / 100;
// Alpha is also oposite (1 is opaque and 127 transparent)
$alpha = 127 - $alpha;

$this->filter[] = array(IMG_FILTER_COLORIZE, $red, $green, $blue, $alpha);
return $this;
}
}

// imageflip definition for PHP < 5.5
Expand Down