Skip to content

Commit

Permalink
v3.1.0
Browse files Browse the repository at this point in the history
* Add `statusCode` property for use in translation. The status code will use class constants.
* Add `$options` argument to `watermarkText()` method to allow fill options.
* Add `$wmTextBottomPadding` property to set watermark text bottom padding to let characters that is long to the bottom can be displayed. Example p, g, ฤ, ฎ, etc. This also known as baseline.
* Add `$wmTextBoundingBoxYPadding` property to set watermark text bounding box vertical padding.
* Add supported for **WEBP**. There are known bugs that can't fix. ( a32fd0d , 48a7b10 )

* Update Imagick minimum requirement.
* Update PNG compression in Imagick driver.

* Fix transparent wetermark text background on the transparent GIF for GD.
* Fix transparent watermark png on transparent gif for GD.

* Push up minimum requirements to PHP 5.4+
  • Loading branch information
ve3 committed Jul 29, 2021
1 parent 3a49483 commit d3276cd
Show file tree
Hide file tree
Showing 24 changed files with 436 additions and 123 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.phpunit.result.cache
.phpunit.result.cache
.phpdoc
.wiki
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ $Image->crop(500, 500, 'center', 'middle');
$Image->save('/path/to/new-file-name.jpg');
```

For more details, please look in tests folder
For more details, please look in tests folder or see [API doc][1]

---
Remark:

* `*` WEBP<br>
There are known bugs that prior PHP 7.0, the transparent PNG or GIF that converted to WEBP will be filled with the background color.
There are known bugs that prior PHP 7.0, the transparent PNG or GIF that converted to WEBP will be filled with the background color.

[1]: http://apidocs.rundiz.com/image/
41 changes: 28 additions & 13 deletions Rundiz/Image/AbstractImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class AbstractImage extends AbstractProperties implements ImageInterfac
* Class constructor.
*
* @param string $source_image_path Path to source image file.
* @return bool Return true on success, false on failed. Call to status_msg property to see the details on failure.
* @return bool Return true on success, false on failed. Call to `statusCode` or `status_msg` property to see the details on failure.
*/
public function __construct($source_image_path)
{
Expand Down Expand Up @@ -77,7 +77,7 @@ public function __set($name, $value)
* Build source image data
*
* @param string $source_image_path Path to source image file.
* @return bool Return true on success, false on failed. Call to status_msg property to see the details on failure.
* @return bool Return true on success, false on failed. Call to `statusCode` or `status_msg` property to see the details on failure.
*/
protected function buildSourceImageData($source_image_path)
{
Expand All @@ -87,7 +87,14 @@ protected function buildSourceImageData($source_image_path)

if (is_file($source_image_path)) {
$source_image_path = realpath($source_image_path);
$image_data = $this->getImageFileData($source_image_path);
try {
$image_data = $this->getImageFileData($source_image_path);
} catch (\Exception $ex) {
$this->status = false;
$this->statusCode = $ex->getCode();
$this->status_msg = $ex->getMessage();
return false;
}

if (false !== $image_data && is_array($image_data) && !empty($image_data)) {
$this->source_image_path = $source_image_path;
Expand All @@ -100,17 +107,20 @@ protected function buildSourceImageData($source_image_path)
unset($image_data);

$this->status = true;
$this->statusCode = null;
$this->status_msg = null;
return true;
} else {
unset($image_data);

$this->status = false;
$this->status_msg = 'Unable to get image data. This file maybe a fake image.';
$this->statusCode = static::RDIERROR_SRC_NOTIMAGE;
$this->status_msg = 'Unable to get image data. This file is not an image.';
return false;
}
} else {
$this->status = false;
$this->statusCode = static::RDIERROR_SRC_NOTEXISTS;
$this->status_msg = 'Source image is not exists.';
return false;
}
Expand All @@ -120,22 +130,26 @@ protected function buildSourceImageData($source_image_path)
/**
* {@inheritDoc}
*
* This will be reset all properties that is commonly use but not reset specific properties for GD, Imagick.
* This method will be reset all properties that is commonly use but not reset specific properties for GD, Imagick.<br>
* The properties that will be clear is based on `ImageInterface` interface that was described.
*/
public function clear()
{
$this->watermark_image_height = null;
$this->watermark_image_type = null;
$this->watermark_image_width = null;
$this->wmTextBottomPadding = null;
// don't reset setting properties to let it continuous run.

// reset result properties.
$this->status = false;
$this->statusCode = null;
$this->status_msg = null;

$this->destination_image_height = null;
$this->destination_image_width = null;
// reset working process properties.
$this->last_modified_image_height = null;
$this->last_modified_image_width = null;
$this->destination_image_height = null;
$this->destination_image_width = null;
$this->watermark_image_type = null;
$this->watermark_image_width = null;
$this->watermark_image_height = null;
}// clear


Expand Down Expand Up @@ -195,7 +209,7 @@ protected function isClassSetup()
*/
protected function isPreviousError()
{
if ($this->status == false && $this->status_msg != null) {
if ($this->status == false && ($this->statusCode != null || $this->status_msg != null)) {
return true;
}
return false;
Expand All @@ -221,7 +235,8 @@ public function resize($width, $height)
)
) {
$this->status = false;
$this->status_msg = 'Unable to calculate sizes, please try to calculate on your own and call to resizeNoRatio instead.';
$this->statusCode = static::RDIERROR_CALCULATEFAILED_USE_RESIZENORATIO;
$this->status_msg = 'Unable to calculate sizes, please try to calculate on your own and call to resizeNoRatio() instead.';
return false;
}

Expand Down
114 changes: 102 additions & 12 deletions Rundiz/Image/AbstractProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ abstract class AbstractProperties
{


// setting properties. ----------------------------------------------------------------------------------------------------
/**
* Allow to set resize larger than source image.
* @var bool Set to `true` to allow, `false` to disallow. Default is `false`.
Expand All @@ -51,16 +52,6 @@ abstract class AbstractProperties
* @var string Master dimension value is 'auto', 'width', or 'height'. Default is 'auto'.
*/
public $master_dim = 'auto';
/**
* Contain status of action methods.
* @var bool Return `false` if there is something error.
*/
public $status = false;
/**
* Contain status error message of action methods.
* @var string Return error message. Default is `null`.
*/
public $status_msg = null;
/**
* Add bottom padding to watermark text to let characters that long to the bottom can be displayed. Example p, g, ฤ, ฎ, etc.<br>
* This bottom padding should be different for each font size.
Expand All @@ -73,6 +64,25 @@ abstract class AbstractProperties
*/
public $wmTextBoundingBoxYPadding = 0;


// result properties. -----------------------------------------------------------------------------------------------------
/**
* Contain status of action methods.
* @var bool Return `false` if there is something error.
*/
public $status = false;
/**
* Contain status code refer from class constants.
* @var int Return status code. See class constants. Default is `null`.
*/
public $statusCode = null;
/**
* Contain status error message of action methods.
* @var string Return error message. Default is `null`.
*/
public $status_msg = null;


// Most of the properties below are unable to set or access directly from outside this class. --------------------
/**
* @var string Path to source image file.
Expand Down Expand Up @@ -137,9 +147,89 @@ abstract class AbstractProperties


/**
* Unable to set source image from this kind of image.
* Not support alpha transparency WebP for current PHP version.
*/
const RDIERROR_SRC_WEBP_ALPHA_NOTSUPPORTED = 1;
/**
* Source file is not image.
*/
const RDIERROR_SRC_NOTIMAGE = 2;
/**
* Source file is not exists.
*/
const RDIERROR_SRC_NOTEXISTS = 3;
/**
* Unable to set source from this kind of image.
*/
const RDIERROR_SRC_UNKNOWN = 16;
/**
* Unable to calculated, please try to calculate on your own and use `resizeNoRatio()` instead.
*/
const RDIERROR_CALCULATEFAILED_USE_RESIZENORATIO = 4;
/**
* Unable to crop this kind of image.
*/
const RDIERROR_CROP_UNKNOWNIMG = 5;
/**
* Unable to resize this kind of image.
*/
const RDIERROR_RESIZE_UNKNOWIMG = 6;
/**
* Unable to rotate this kind of image.
*/
const RDIERROR_ROTATE_UNKNOWIMG = 7;
/**
* Unable to flip this kind of image.
*/
const RDIERROR_FLIP_UNKNOWIMG = 8;
/**
* Not support flip for current PHP version. (PHP 5.5 or newer are required to use GD flip.)
*/
const RDIERROR_FLIP_NOTSUPPORTED = 9;
/**
* Unable to set watermark from this kind of image.
*/
const RDIERROR_WMI_UNKNOWIMG = 10;
/**
* Watermark image not exists.
*/
const RDIERROR_WMI_NOTEXISTS = 11;
/**
* Unable to save to this extension. or target file extension is not supported.
*/
const RDIERROR_SAVE_UNSUPPORT = 12;
/**
* Failed to save an image.
*/
const RDIERROR_SAVE_FAILED = 13;
/**
* Unable to show image in this extension. File extension is not supported.
*/
const RDIERROR_SHOW_UNSUPPORT = 14;
/**
* Failed to show an image to browser.
*/
const RDIERROR_SHOW_FAILED = 15;
/**
* Watermark text font file is not exists.
*/
const RDIERROR_WMT_FONT_NOTEXISTS = 17;
/**
* Imagick extension was not loaded.
*/
const RDIERROR_IMAGICK_NOTLOAD = 18;
/**
* Imagick version does not meet requirement.
*/
const RDIERROR_IMAGICK_NOTMEETREQUIREMENT = 19;
/**
* Could not verify Imagick version.
*/
const RDIERROR_IMAGICK_VERSIONUNKNOW = 20;
/**
* Image Magick version does not meet requirement.
*/
const RDIERROR_SOURCE_IMG_NOT_SUPPORTED = 1;
const RDIERROR_IMAGEMAGICK_NOTMEETREQUIREMENT = 21;


}
27 changes: 24 additions & 3 deletions Rundiz/Image/Drivers/Gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ public function crop($width, $height, $start_x = '0', $start_y = '0', $fill = 't
}// crop


/**
* Get this class as static call.
*
* @return static
*/
public function getStatic()
{
return new static($this->source_image_path);
}// getStatic


/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -193,6 +204,7 @@ public function rotate($degree = 90)

if (!$this->isPreviousError()) {
$this->status = true;
$this->statusCode = null;
$this->status_msg = null;
}
return true;
Expand Down Expand Up @@ -228,7 +240,7 @@ public function save($file_name)
*
* @param int $width Destination image object width.
* @param int $height Destination image object height.
* @return bool Return true on success, false on failed. Call to status_msg property to see the details on failure.
* @return bool Return true on success, false on failed.
*/
private function setupDestinationImageObjectWithSize($width, $height)
{
Expand All @@ -246,6 +258,7 @@ private function setupDestinationImageObjectWithSize($width, $height)

// come to this means destination image object is already set.
$this->status = true;
$this->statusCode = null;
$this->status_msg = null;
return true;
}// setupDestinationImageObjectWithSize
Expand All @@ -255,7 +268,7 @@ private function setupDestinationImageObjectWithSize($width, $height)
* Setup source image object.
* After calling this the source_image_object will get new image resource by chaining from previous destination or from image file.
*
* @return bool Return true on success, false on failed. Call to status_msg property to see the details on failure.
* @return bool Return true on success, false on failed.
*/
private function setupSourceImageObject()
{
Expand All @@ -273,6 +286,7 @@ private function setupSourceImageObject()
$this->destination_image_object = null;

$this->status = true;
$this->statusCode = null;
$this->status_msg = null;
return true;
} else {
Expand Down Expand Up @@ -300,10 +314,12 @@ private function setupSourceImageObject()

if ($this->source_image_object != null) {
$this->status = true;
$this->statusCode = null;
$this->status_msg = null;
return true;
} else {
$this->status = false;
$this->statusCode = static::RDIERROR_SRC_UNKNOWN;
$this->status_msg = 'Unable to set source from this kind of image.';
return false;
}
Expand All @@ -312,6 +328,7 @@ private function setupSourceImageObject()

// come to this means source image object is already set.
$this->status = true;
$this->statusCode = null;
$this->status_msg = null;
return true;
}// setupSourceImageObject
Expand Down Expand Up @@ -353,6 +370,7 @@ public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_
// check watermark image path exists
if (!is_file($wm_img_path)) {
$this->status = false;
$this->statusCode = static::RDIERROR_WMI_NOTEXISTS;
$this->status_msg = 'Watermark image was not found.';
return false;
}
Expand Down Expand Up @@ -406,7 +424,7 @@ public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_
* @param string $wm_img_path Path to watermark image.
* @param int $wm_img_start_x Position to begin in x axis. The value is integer or 'left', 'center', 'right'.
* @param int $wm_img_start_y Position to begin in x axis. The value is integer or 'top', 'middle', 'bottom'.
* @return bool Return true on success, false on failed. Call to status_msg property to see the details on failure.
* @return bool Return true on success, false on failed.
*/
private function watermarkImageProcess($wm_img_path, $wm_img_start_x = 0, $wm_img_start_y = 0)
{
Expand All @@ -423,6 +441,7 @@ private function watermarkImageProcess($wm_img_path, $wm_img_start_x = 0, $wm_im
unset($result);

$this->status = true;
$this->statusCode = null;
$this->status_msg = null;
return true;
}// watermarkImageProcess
Expand Down Expand Up @@ -457,6 +476,7 @@ public function watermarkText(

if (!is_file($wm_txt_font_path)) {
$this->status = false;
$this->statusCode = static::RDIERROR_WMT_FONT_NOTEXISTS;
$this->status_msg = 'Unable to load font file.';
return false;
}
Expand All @@ -470,6 +490,7 @@ public function watermarkText(
unset($result);

$this->status = true;
$this->statusCode = null;
$this->status_msg = null;
return true;
}// watermarkText
Expand Down
Loading

0 comments on commit d3276cd

Please sign in to comment.