Skip to content

Commit

Permalink
1.99.0
Browse files Browse the repository at this point in the history
- [R] PHP 7.4 & PHP 8.0 compatible version
- [+] `applyImageFilter` method
- [+] GDImageException class
  • Loading branch information
Karel Wintersky committed Aug 27, 2024
1 parent d911e11 commit e5cd3ca
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 16 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# 1.99.0

- [R] PHP 7.4 & PHP 8.0 compatible version
- [+] `applyImageFilter` method
- [+] GDImageException class

# 1.3.0

- Latest PHP7.4 version. It will not work on PHP8, 'cause in PHP8 Image is `GdImage`, not an abstract `resource`.
- Latest PHP7.4 version. It will not work on PHP8, 'cause in PHP8 Image is `GdImage`, not an abstract `resource` (fixed!)

# 1.2.1

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"platform-check": false
},
"require": {
"php": "^7.4",
"php": "7.4 | 8.*",
"psr/log": "*",
"ext-gd": "*"
},
Expand Down
35 changes: 35 additions & 0 deletions sources/GDImageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace AJUR\Wrappers;

class GDImageException extends \RuntimeException
{
protected array $_info;

public function __construct(string $message = "", int $code = 0 , array $info = [])
{
$this->_info = $info;

parent::__construct($message, $code, null);
}

/**
* Get custom field
*
* @param $key
* @return array|mixed|null
*/
public function getInfo($key = null)
{
return
is_null($key)
? $this->_info
: (array_key_exists($key, $this->_info) ? $this->_info[$key] : null);
}

public function getError()
{
return 'Exception thrown from [' . $this->getFile() . '] with message: [' . $this->getMessage() . '] at line # ' . $this->getLine();
}

}
42 changes: 30 additions & 12 deletions sources/GDImageInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ class GDImageInfo implements GDImageInfoInterface
public int $type = 0;

/**
* MimeType
* @var string
*/
public string $mime = '';

/**
* Extension based in MimeType
* @var string
*/
public string $mime_extension;

/**
* @var string
*/
public $filename;
public string $filename;

/**
* @var string
*/
public $extension = '';
public string $extension = '';

/**
* Целевая степень сжатия
Expand Down Expand Up @@ -64,7 +66,7 @@ public function __construct($filename = '', $error_message = '')
*
* @param string $fn_source
* @param string $fn_target
* @return void
* @return GDImageInfo
*/
public static function clone(string $fn_source, string $fn_target):GDImageInfo
{
Expand All @@ -88,7 +90,6 @@ public function getFileInfo():GDImageInfo
$this->width = $image_info[0];
$this->height = $image_info[1];
$this->type = $image_info[2];
// $this->attr = $image_info[3];
$this->mime = image_type_to_mime_type($this->type);
$this->mime_extension = image_type_to_extension($this->type); // расширение на основе MIME-типа
} else {
Expand Down Expand Up @@ -164,12 +165,26 @@ public function load():GDImageInfo
*/
public function destroyImage():GDImageInfo
{
$data_type = get_resource_type($this->data);

if ($this->data !== null && ($data_type === 'gd' || $data_type =='resource')) {
imagedestroy($this->data);
if (
(PHP_VERSION_ID >= 80000 && !$this->data instanceof \GdImage)
||
(PHP_VERSION_ID < 80000 && get_resource_type($this->data) != 'gd')
) {
throw new GDImageException("Not a GdImage resource: ", 0, [
'type' => $this->type,
'filename' => $this->filename,
'extension' => $this->extension,
'mime' => $this->mime,
'mime_extension' => $this->mime_extension,
'width' => $this->width,
'height' => $this->height,
'quality' => $this->quality,
'data' => $this->data
]);
}

imagedestroy($this->data);

$this->data = null;

return $this;
Expand Down Expand Up @@ -206,21 +221,24 @@ public function getImageData()
public function setCompressionQuality($image_quality = null):GDImageInfo
{
switch ($this->extension) {
case 'jpg':
case 'jpg': {
$this->quality = $image_quality ? GDWrapper::toRange($image_quality, 1, 100) : GDWrapper::$default_jpeg_quality;
break;
}
case 'webp': {
$this->quality = GDWrapper::toRange($image_quality, 1, 100);
$this->quality = $image_quality ? GDWrapper::toRange($image_quality, 1, 100) : GDWrapper::$default_webp_quality;
break;
}
case 'png': {
$this->quality = GDWrapper::toRangeMin($image_quality, 0, 9);
$this->quality = $image_quality ? GDWrapper::toRange($image_quality, 1, 10) : GDWrapper::$default_png_quality;
break;
}
default: {
$this->quality = $image_quality;
}
}

$this->quality = $image_quality;
// $this->quality = $image_quality;
return $this;
}

Expand Down
14 changes: 12 additions & 2 deletions sources/GDWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ public static function imageFillColor(string $fn_target, int $width, int $height

[$red, $green, $blue, $alpha] = $color;

$target->data = imagecreatetruecolor($width, $height);

if ($target->extension == 'png') {
imagesavealpha($target->data, true);
}

$target->data = imagecreatetruecolor($width, $height);

$color
= $alpha == 0
? imagecolorallocate($target->data, $red, $green, $blue)
Expand Down Expand Up @@ -487,6 +487,16 @@ public static function rotate(string $fn_source, $roll_direction = "", $quality
return $source;
}

public static function applyImageFilter(string $fn_source, int $filter, ...$args):GDImageInfo
{
$source = new GDImageInfo($fn_source);
$source->load();

imagefilter($source->data, $filter, $args);

return $source;
}

/**
*
* @param $value
Expand Down
110 changes: 110 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';

use AJUR\Wrappers\GDWrapper;
use AJUR\Wrappers\GDWrapperInterface;

if (!is_readable(__DIR__ . '/test.jpg')) {
die(PHP_EOL . PHP_EOL . 'Any `test.jpg` is needed for testing' . PHP_EOL . PHP_EOL);
}

GDWrapper::init([
'JPEG_COMPRESSION_QUALITY' => 80,
'WEBP_COMPRESSION_QUALITY' => 100,
'PNG_COMPRESSION_QUALITY' => 0
]);

echo 'Making watermark file' . PHP_EOL;
GDWrapper::resizeImageAspect('test.jpg', 'watermark.png', 100, 100);
$wm = GDWrapper::applyImageFilter('watermark.png', IMG_FILTER_NEGATE);
$wm->store()->destroyImage();

echo "cropImage: " . PHP_EOL;
var_dump(
GDWrapper::cropImage('test.jpg', 'test_crop.jpg', [ 100, 100], [ 900, 900 ], [ 900, 900 ], 70)
);

echo "resizeImageAspect: " . PHP_EOL;
var_dump(
GDWrapper::resizeImageAspect('test.jpg', 'test_RIA.png', 700, 200)
);

echo "resizeImageAspect: " . PHP_EOL;
var_dump(
GDWrapper::resizeImageAspect('test.jpg', 'test_RIA.jpg', 200, 500, 40)
);

echo "resizePictureAspect: " . PHP_EOL;
var_dump(
GDWrapper::resizePictureAspect('test.jpg', 'test_RPA.png', 700, 200)
);

echo "resizePictureAspect: " . PHP_EOL;
var_dump(
GDWrapper::resizePictureAspect('test.jpg', 'test_RPA.jpg', 200, 500, 40)
);

echo "verticalImage: " . PHP_EOL;
var_dump(
GDWrapper::verticalImage('test.jpg', 'test_VI.jpg', 300, 300, 80)
);

echo "getFixedPicture: " . PHP_EOL;
var_dump(
GDWrapper::getFixedPicture('test.jpg', 'test_GFP.jpg', 470, 370, 90)
);

echo "addWaterMark: " . PHP_EOL;
var_dump(
GDWrapper::addWaterMark('test.jpg', ['watermark' => 'watermark.png', 'margin' => 10] , GDWrapperInterface::WM_POSITION_LEFT_TOP, null, 'test-wm-10.jpg')
);


echo "addWaterMark: " . PHP_EOL;
var_dump(
GDWrapper::addWaterMark('test.jpg', ['watermark' => 'watermark.png', 'margin' => 20] , GDWrapperInterface::WM_POSITION_RIGHT_BOTTOM, null, 'test-wm-20.jpg')
);

echo "addWaterMark: " . PHP_EOL;
var_dump(
GDWrapper::addWaterMark('test.jpg', ['watermark' => 'watermark.png', 'margin' => 50] , GDWrapperInterface::WM_POSITION_LEFT_BOTTOM, null, 'test-wm-50.jpg')
);

echo "addWaterMark: " . PHP_EOL;
var_dump(
GDWrapper::addWaterMark('test.jpg', ['watermark' => '_watermark_m.png', 'margin' => 50] , GDWrapperInterface::WM_POSITION_LEFT_BOTTOM, null, 'test-2.jpg')
);

echo "rotate: " . PHP_EOL;
var_dump(
GDWrapper::rotate('test.jpg', 'left', 90, 'test_kid_rotate90.jpg')
);

echo "cropImage: " . PHP_EOL;
var_dump(
GDWrapper::cropImage('test.jpg', 'test_crop.jpg', [ 100, 100 ] , [ 800, 1000 ], [ 1000, 1000 ], 90)
);

echo "resizeImageAspect: " . PHP_EOL;
var_dump(
GDWrapper::resizeImageAspect('test.jpg', 'test_RIA.jpg', 200, 500, 40)
);

echo "imageFillColor: " . PHP_EOL;
var_dump(
GDWrapper::imageFillColor('test_red.png', 500, 500, [ 255 ], 90)
);

echo "imageFillColor: " . PHP_EOL;
var_dump(
GDWrapper::imageFillColor('test_yellow.webp', 500, 500, [ 255, 255 ], 90)
);


/*
$f = new \AJUR\Wrappers\GDImageInfo('test.jpg');
$f->changeExtension('webp');
var_dump($f->filename);
*/

0 comments on commit e5cd3ca

Please sign in to comment.