diff --git a/Rundiz/Image/AbstractImage.php b/Rundiz/Image/AbstractImage.php index 69303d4..4d5d433 100644 --- a/Rundiz/Image/AbstractImage.php +++ b/Rundiz/Image/AbstractImage.php @@ -81,6 +81,10 @@ public function __set($name, $value) */ protected function buildSourceImageData($source_image_path) { + $WebP = new Extensions\WebP(); + $WebP->checkWebPConstant(); + unset($WebP); + if (is_file($source_image_path)) { $source_image_path = realpath($source_image_path); $image_data = $this->getImageFileData($source_image_path); diff --git a/Rundiz/Image/AbstractProperties.php b/Rundiz/Image/AbstractProperties.php index 256b02e..1964d6d 100644 --- a/Rundiz/Image/AbstractProperties.php +++ b/Rundiz/Image/AbstractProperties.php @@ -136,4 +136,10 @@ abstract class AbstractProperties protected $watermark_image_height; + /** + * Unable to set source image from this kind of image. + */ + const RDIERROR_SOURCE_IMG_NOT_SUPPORTED = 1; + + } diff --git a/Rundiz/Image/Drivers/Gd.php b/Rundiz/Image/Drivers/Gd.php index 889c5be..0cea629 100644 --- a/Rundiz/Image/Drivers/Gd.php +++ b/Rundiz/Image/Drivers/Gd.php @@ -290,7 +290,13 @@ private function setupSourceImageObject() // add alpha, alpha blending to support transparency png imagealphablending($this->source_image_object, false); imagesavealpha($this->source_image_object, true); - } + } elseif ($this->source_image_type === IMAGETYPE_WEBP) { + // webp + $this->source_image_object = imagecreatefromwebp($this->source_image_path); + // add alpha, alpha blending to support transparency png + imagealphablending($this->source_image_object, false); + imagesavealpha($this->source_image_object, true); + }// endif; if ($this->source_image_object != null) { $this->status = true; diff --git a/Rundiz/Image/Drivers/Gd/Crop.php b/Rundiz/Image/Drivers/Gd/Crop.php index 81dc625..c6f841f 100644 --- a/Rundiz/Image/Drivers/Gd/Crop.php +++ b/Rundiz/Image/Drivers/Gd/Crop.php @@ -58,48 +58,38 @@ public function execute($width, $height, $start_x = '0', $start_y = '0', $fill = } // begins crop - if ($this->Gd->source_image_type === IMAGETYPE_GIF) { - // gif - if ($fill == 'transparent') { - imagefill($this->Gd->destination_image_object, 0, 0, $transwhite); - imagecolortransparent($this->Gd->destination_image_object, $transwhite); - } else { - imagefill($this->Gd->destination_image_object, 0, 0, $$fill); - } - - imagecopy($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, $start_x, $start_y, $width, $height); - - // fill "again" in case that cropping image is larger than source image. - if ($width > imagesx($this->Gd->source_image_object) || $height > imagesy($this->Gd->source_image_object)) { + switch ($this->Gd->source_image_type) { + case IMAGETYPE_GIF: + // fill first time to prevent transparency become black. if ($fill == 'transparent') { imagefill($this->Gd->destination_image_object, 0, 0, $transwhite); imagecolortransparent($this->Gd->destination_image_object, $transwhite); } else { imagefill($this->Gd->destination_image_object, 0, 0, $$fill); } - } - } elseif ($this->Gd->source_image_type === IMAGETYPE_JPEG) { - // jpg - imagecopy($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, $start_x, $start_y, $width, $height); - - if ($fill != 'transparent') { - imagefill($this->Gd->destination_image_object, 0, 0, $$fill); - } - } elseif ($this->Gd->source_image_type === IMAGETYPE_PNG) { - // png - if ($fill == 'transparent') { - imagefill($this->Gd->destination_image_object, 0, 0, $transwhite); - imagecolortransparent($this->Gd->destination_image_object, $black); - imagealphablending($this->Gd->destination_image_object, false); - imagesavealpha($this->Gd->destination_image_object, true); - } else { - imagefill($this->Gd->destination_image_object, 0, 0, $$fill); - } - - imagecopy($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, $start_x, $start_y, $width, $height); - - // fill "again" in case that cropping image is larger than source image. - if ($width > imagesx($this->Gd->source_image_object) || $height > imagesy($this->Gd->source_image_object)) { + // do crop. + imagecopy($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, $start_x, $start_y, $width, $height); + // fill "again" in case that cropping image is larger than source image. + if ($width > imagesx($this->Gd->source_image_object) || $height > imagesy($this->Gd->source_image_object)) { + if ($fill == 'transparent') { + imagefill($this->Gd->destination_image_object, 0, 0, $transwhite); + imagecolortransparent($this->Gd->destination_image_object, $transwhite); + } else { + imagefill($this->Gd->destination_image_object, 0, 0, $$fill); + } + } + break; + case IMAGETYPE_JPEG: + // do crop. + imagecopy($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, $start_x, $start_y, $width, $height); + // do fill. + if ($fill != 'transparent') { + imagefill($this->Gd->destination_image_object, 0, 0, $$fill); + } + break; + case IMAGETYPE_PNG: + case IMAGETYPE_WEBP: + // fill first time to prevent transparency become black. if ($fill == 'transparent') { imagefill($this->Gd->destination_image_object, 0, 0, $transwhite); imagecolortransparent($this->Gd->destination_image_object, $black); @@ -108,12 +98,25 @@ public function execute($width, $height, $start_x = '0', $start_y = '0', $fill = } else { imagefill($this->Gd->destination_image_object, 0, 0, $$fill); } - } - } else { - $this->Gd->status = false; - $this->Gd->status_msg = 'Unable to crop this kind of image.'; - return false; - } + // do crop. + imagecopy($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, $start_x, $start_y, $width, $height); + // fill "again" in case that cropping image is larger than source image. + if ($width > imagesx($this->Gd->source_image_object) || $height > imagesy($this->Gd->source_image_object)) { + if ($fill == 'transparent') { + imagefill($this->Gd->destination_image_object, 0, 0, $transwhite); + imagecolortransparent($this->Gd->destination_image_object, $black); + imagealphablending($this->Gd->destination_image_object, false); + imagesavealpha($this->Gd->destination_image_object, true); + } else { + imagefill($this->Gd->destination_image_object, 0, 0, $$fill); + } + } + break; + default: + $this->Gd->status = false; + $this->Gd->status_msg = 'Unable to crop this kind of image.'; + return false; + }// endswitch; // clear unused variables if ($this->isResourceOrGDObject($this->Gd->source_image_object)) { diff --git a/Rundiz/Image/Drivers/Gd/Resize.php b/Rundiz/Image/Drivers/Gd/Resize.php index 05a20ee..03dae4a 100644 --- a/Rundiz/Image/Drivers/Gd/Resize.php +++ b/Rundiz/Image/Drivers/Gd/Resize.php @@ -32,27 +32,29 @@ public function execute($width, $height) } // begins resize - if ($this->Gd->source_image_type === IMAGETYPE_GIF) { - // gif - $transwhite = imagecolorallocatealpha($this->Gd->destination_image_object, 255, 255, 255, 127); - imagefill($this->Gd->destination_image_object, 0, 0, $transwhite); - imagecolortransparent($this->Gd->destination_image_object, $transwhite); - imagecopyresampled($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, 0, 0, $width, $height, $source_image_width, $source_image_height); - unset($transwhite); - } elseif ($this->Gd->source_image_type === IMAGETYPE_JPEG) { - // jpg - imagecopyresampled($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, 0, 0, $width, $height, $source_image_width, $source_image_height); - } elseif ($this->Gd->source_image_type === IMAGETYPE_PNG) { - // png - imagealphablending($this->Gd->destination_image_object, false); - imagesavealpha($this->Gd->destination_image_object, true); - imagecopyresampled($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, 0, 0, $width, $height, $source_image_width, $source_image_height); - } else { - $this->Gd->status = false; - $this->Gd->status_msg = 'Unable to resize this kind of image.'; - unset($source_image_height, $source_image_width); - return false; - } + switch ($this->Gd->source_image_type) { + case IMAGETYPE_GIF: + $transwhite = imagecolorallocatealpha($this->Gd->destination_image_object, 255, 255, 255, 127); + imagefill($this->Gd->destination_image_object, 0, 0, $transwhite); + imagecolortransparent($this->Gd->destination_image_object, $transwhite); + imagecopyresampled($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, 0, 0, $width, $height, $source_image_width, $source_image_height); + unset($transwhite); + break; + case IMAGETYPE_JPEG: + imagecopyresampled($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, 0, 0, $width, $height, $source_image_width, $source_image_height); + break; + case IMAGETYPE_PNG: + case IMAGETYPE_WEBP: + imagealphablending($this->Gd->destination_image_object, false); + imagesavealpha($this->Gd->destination_image_object, true); + imagecopyresampled($this->Gd->destination_image_object, $this->Gd->source_image_object, 0, 0, 0, 0, $width, $height, $source_image_width, $source_image_height); + break; + default: + $this->Gd->status = false; + $this->Gd->status_msg = 'Unable to resize this kind of image.'; + unset($source_image_height, $source_image_width); + return false; + }// endswitch; // clear unused variable if ($this->isResourceOrGDObject($this->Gd->source_image_object)) { diff --git a/Rundiz/Image/Drivers/Gd/Rotate.php b/Rundiz/Image/Drivers/Gd/Rotate.php index efc4ba9..2c23158 100644 --- a/Rundiz/Image/Drivers/Gd/Rotate.php +++ b/Rundiz/Image/Drivers/Gd/Rotate.php @@ -32,7 +32,6 @@ public function execute($degree = 90) // rotate by degree switch ($this->Gd->source_image_type) { case IMAGETYPE_GIF: - // gif // set source image width and height $source_image_width = imagesx($this->Gd->source_image_object); $source_image_height = imagesy($this->Gd->source_image_object); @@ -46,13 +45,12 @@ public function execute($degree = 90) unset($source_image_height, $source_image_width, $transwhite); break; case IMAGETYPE_JPEG: - // jpg $white = imagecolorallocate($this->Gd->source_image_object, 255, 255, 255); $this->Gd->destination_image_object = imagerotate($this->Gd->source_image_object, $degree, $white); unset($white); break; case IMAGETYPE_PNG: - // png + case IMAGETYPE_WEBP: $transwhite = imageColorAllocateAlpha($this->Gd->source_image_object, 0, 0, 0, 127); $this->Gd->destination_image_object = imagerotate($this->Gd->source_image_object, $degree, $transwhite); imagealphablending($this->Gd->destination_image_object, false); diff --git a/Rundiz/Image/Drivers/Gd/Save.php b/Rundiz/Image/Drivers/Gd/Save.php index 8d4f45d..0159bc3 100644 --- a/Rundiz/Image/Drivers/Gd/Save.php +++ b/Rundiz/Image/Drivers/Gd/Save.php @@ -29,8 +29,11 @@ public function execute($file_name) // save to file. each image types use different ways to save. if ($check_file_ext === 'gif') { // if save to gif - if ($this->Gd->source_image_type === IMAGETYPE_PNG) { - // if source image is png file. + if ( + $this->Gd->source_image_type === IMAGETYPE_PNG || + $this->Gd->source_image_type === IMAGETYPE_WEBP + ) { + // if source image is png or webp file. // preserve transparency part. $this->fillTransparentDestinationImage(); } @@ -38,8 +41,12 @@ public function execute($file_name) $save_result = imagegif($this->Gd->destination_image_object, $file_name); } elseif ($check_file_ext === 'jpg') { // if save to jpg - if ($this->Gd->source_image_type === IMAGETYPE_PNG || $this->Gd->source_image_type === IMAGETYPE_GIF) { - // if source image is png or gif file. + if ( + $this->Gd->source_image_type === IMAGETYPE_PNG || + $this->Gd->source_image_type === IMAGETYPE_GIF || + $this->Gd->source_image_type === IMAGETYPE_WEBP + ) { + // if source image is png or gif or webp file. // convert transparency png to white before save. $this->fillWhiteDestinationImage(); } diff --git a/Rundiz/Image/Drivers/Gd/Show.php b/Rundiz/Image/Drivers/Gd/Show.php index 53efbaf..e9c7dda 100644 --- a/Rundiz/Image/Drivers/Gd/Show.php +++ b/Rundiz/Image/Drivers/Gd/Show.php @@ -33,8 +33,11 @@ public function execute($file_ext = '') // show image to browser. if ($check_file_ext === 'gif') { // if save to gif - if ($this->Gd->source_image_type === IMAGETYPE_PNG) { - // if source image is png file. + if ( + $this->Gd->source_image_type === IMAGETYPE_PNG || + $this->Gd->source_image_type === IMAGETYPE_WEBP + ) { + // if source image is png or webp file. // preserve transparency part. $this->fillTransparentDestinationImage(); } @@ -47,8 +50,12 @@ public function execute($file_ext = '') } } elseif ($check_file_ext === 'jpg') { // if save to jpg - if ($this->Gd->source_image_type === IMAGETYPE_PNG || $this->Gd->source_image_type === IMAGETYPE_GIF) { - // if source image is png or gif file. + if ( + $this->Gd->source_image_type === IMAGETYPE_PNG || + $this->Gd->source_image_type === IMAGETYPE_GIF || + $this->Gd->source_image_type === IMAGETYPE_WEBP + ) { + // if source image is png or gif or webp file. // convert transparency png to white before save. $this->fillWhiteDestinationImage(); } diff --git a/Rundiz/Image/Drivers/Gd/Watermark.php b/Rundiz/Image/Drivers/Gd/Watermark.php index cc6877a..c88d541 100644 --- a/Rundiz/Image/Drivers/Gd/Watermark.php +++ b/Rundiz/Image/Drivers/Gd/Watermark.php @@ -29,13 +29,10 @@ public function applyImage($wm_img_start_x = 0, $wm_img_start_y = 0) { switch ($this->Gd->watermark_image_type) { case IMAGETYPE_GIF: - // gif case IMAGETYPE_JPEG: - // jpg imagecopy($this->Gd->source_image_object, $this->Gd->watermark_image_object, $wm_img_start_x, $wm_img_start_y, 0, 0, $this->Gd->watermark_image_width, $this->Gd->watermark_image_height); break; case IMAGETYPE_PNG: - // png if ($this->Gd->source_image_type === IMAGETYPE_GIF) { // if source image is gif (which maybe transparent) and watermark image is png. so, this cannot just use imagecopy() function. // see more at http://stackoverflow.com/questions/4437557/using-gd-in-php-how-can-i-make-a-transparent-png-watermark-on-png-and-gif-files @@ -208,7 +205,6 @@ public function applyText( // copy text to image switch ($this->Gd->source_image_type) { case IMAGETYPE_GIF: - // gif $this->applyWatermarkToGifImage( $wm_txt_object, $wm_txt_width, @@ -218,10 +214,9 @@ public function applyText( ); break; case IMAGETYPE_PNG: - // png + case IMAGETYPE_WEBP: imagealphablending($this->Gd->source_image_object, true); case IMAGETYPE_JPEG: - // jpg default: imagecopy($this->Gd->source_image_object, $wm_txt_object, $wm_txt_start_x, $wm_txt_start_y, 0, 0, $wm_txt_width, $wm_txt_height); break; diff --git a/Rundiz/Image/Drivers/Imagick/Crop.php b/Rundiz/Image/Drivers/Imagick/Crop.php index 6188bb4..9062b3e 100644 --- a/Rundiz/Image/Drivers/Imagick/Crop.php +++ b/Rundiz/Image/Drivers/Imagick/Crop.php @@ -56,56 +56,73 @@ public function execute($width, $height, $start_x = '0', $start_y = '0', $fill = } // begins crop - if ($this->ImagickD->source_image_type === IMAGETYPE_GIF) { - // gif - if ($this->ImagickD->source_image_frames > 1) { - $this->ImagickD->Imagick = $this->ImagickD->Imagick->coalesceImages(); - if (is_object($this->ImagickD->Imagick)) { - $i = 1; - foreach ($this->ImagickD->Imagick as $Frame) { - if ($fill != 'transparent') { - $Frame->setImageBackgroundColor($$fill); - $Frame->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE); - } else { - $Frame->setImageBackgroundColor($transparent); + switch ($this->ImagickD->source_image_type) { + case IMAGETYPE_GIF: + if ($this->ImagickD->source_image_frames > 1) { + $this->ImagickD->Imagick = $this->ImagickD->Imagick->coalesceImages(); + if (is_object($this->ImagickD->Imagick)) { + $i = 1; + foreach ($this->ImagickD->Imagick as $Frame) { + if ($fill != 'transparent') { + $Frame->setImageBackgroundColor($$fill); + $Frame->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE); + } else { + $Frame->setImageBackgroundColor($transparent); + } + $Frame->cropImage($width, $height, $start_x, $start_y); + $Frame->extentImage( + $width, + $height, + $this->calculateStartXOfCenter($width, $this->ImagickD->Imagick->getImageWidth()), + $this->calculateStartXOfCenter($height, $this->ImagickD->Imagick->getImageHeight()) + ); + $Frame->setImagePage($width, $height, 0, 0); + if ($i == 1) { + $this->ImagickD->ImagickFirstFrame = $Frame->getImage(); + } + $i++; } - $Frame->cropImage($width, $height, $start_x, $start_y); - $Frame->extentImage($width, $height, $this->calculateStartXOfCenter($width, $this->ImagickD->Imagick->getImageWidth()), $this->calculateStartXOfCenter($height, $this->ImagickD->Imagick->getImageHeight())); - $Frame->setImagePage($width, $height, 0, 0); - if ($i == 1) { - $this->ImagickD->ImagickFirstFrame = $Frame->getImage(); - } - $i++; + unset($Frame, $i); + } + } else { + if ($fill != 'transparent') { + $this->ImagickD->Imagick->setImageBackgroundColor($$fill); + $this->ImagickD->Imagick->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE); + } else { + $this->ImagickD->Imagick->setImageBackgroundColor($transparent); } - unset($Frame, $i); + $this->ImagickD->Imagick->cropImage($width, $height, $start_x, $start_y); + $this->ImagickD->Imagick->setImagePage(0, 0, 0, 0); + $this->ImagickD->Imagick->extentImage( + $width, + $height, + $this->calculateStartXOfCenter($width, $this->ImagickD->Imagick->getImageWidth()), + $this->calculateStartXOfCenter($height, $this->ImagickD->Imagick->getImageHeight()) + ); + $this->ImagickD->ImagickFirstFrame = null; } - } else { + break; + case IMAGETYPE_JPEG: + case IMAGETYPE_PNG: + case IMAGETYPE_WEBP: + $this->ImagickD->Imagick->cropImage($width, $height, $start_x, $start_y); + $this->ImagickD->Imagick->setImageBackgroundColor($transwhite);// for transparent png and allow to fill other bg color than black in jpg. + $this->ImagickD->Imagick->extentImage( + $width, + $height, + $this->calculateStartXOfCenter($width, $this->ImagickD->Imagick->getImageWidth()), + $this->calculateStartXOfCenter($height, $this->ImagickD->Imagick->getImageHeight()) + ); if ($fill != 'transparent') { $this->ImagickD->Imagick->setImageBackgroundColor($$fill); $this->ImagickD->Imagick->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE); - } else { - $this->ImagickD->Imagick->setImageBackgroundColor($transparent); } - $this->ImagickD->Imagick->cropImage($width, $height, $start_x, $start_y); - $this->ImagickD->Imagick->setImagePage(0, 0, 0, 0); - $this->ImagickD->Imagick->extentImage($width, $height, $this->calculateStartXOfCenter($width, $this->ImagickD->Imagick->getImageWidth()), $this->calculateStartXOfCenter($height, $this->ImagickD->Imagick->getImageHeight())); - $this->ImagickD->ImagickFirstFrame = null; - } - } elseif ($this->ImagickD->source_image_type === IMAGETYPE_JPEG || $this->ImagickD->source_image_type === IMAGETYPE_PNG ) { - // jpg OR png - $this->ImagickD->Imagick->cropImage($width, $height, $start_x, $start_y); - $this->ImagickD->Imagick->setImageBackgroundColor($transwhite);// for transparent png and allow to fill other bg color than black in jpg. - $this->ImagickD->Imagick->extentImage($width, $height, $this->calculateStartXOfCenter($width, $this->ImagickD->Imagick->getImageWidth()), $this->calculateStartXOfCenter($height, $this->ImagickD->Imagick->getImageHeight())); - - if ($fill != 'transparent') { - $this->ImagickD->Imagick->setImageBackgroundColor($$fill); - $this->ImagickD->Imagick->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE); - } - } else { - $this->ImagickD->status = false; - $this->ImagickD->status_msg = 'Unable to crop this kind of image.'; - return false; - } + break; + default: + $this->ImagickD->status = false; + $this->ImagickD->status_msg = 'Unable to crop this kind of image.'; + return false; + }// endswitch; $black->destroy(); $white->destroy(); diff --git a/Rundiz/Image/Drivers/Imagick/Resize.php b/Rundiz/Image/Drivers/Imagick/Resize.php index e3bc733..475d29d 100644 --- a/Rundiz/Image/Drivers/Imagick/Resize.php +++ b/Rundiz/Image/Drivers/Imagick/Resize.php @@ -19,35 +19,38 @@ class Resize extends \Rundiz\Image\Drivers\AbstractImagickCommand public function execute($width, $height) { // begins resize - if ($this->ImagickD->source_image_type === IMAGETYPE_GIF) { - // gif - if ($this->ImagickD->source_image_frames > 1) { - $this->ImagickD->Imagick = $this->ImagickD->Imagick->coalesceImages(); - if (is_object($this->ImagickD->Imagick)) { - $i = 1; - foreach ($this->ImagickD->Imagick as $Frame) { - $Frame->resizeImage($width, $height, $this->ImagickD->imagick_filter, 1); - $Frame->setImagePage(0, 0, 0, 0); - if ($i == 1) { - $this->ImagickD->ImagickFirstFrame = $Frame->getImage(); + switch ($this->ImagickD->source_image_type) { + case IMAGETYPE_GIF: + if ($this->ImagickD->source_image_frames > 1) { + $this->ImagickD->Imagick = $this->ImagickD->Imagick->coalesceImages(); + if (is_object($this->ImagickD->Imagick)) { + $i = 1; + foreach ($this->ImagickD->Imagick as $Frame) { + $Frame->resizeImage($width, $height, $this->ImagickD->imagick_filter, 1); + $Frame->setImagePage(0, 0, 0, 0); + if ($i == 1) { + $this->ImagickD->ImagickFirstFrame = $Frame->getImage(); + } + $i++; } - $i++; + unset($Frame, $i); } - unset($Frame, $i); + } else { + $this->ImagickD->Imagick->resizeImage($width, $height, $this->ImagickD->imagick_filter, 1); + $this->ImagickD->Imagick->setImagePage(0, 0, 0, 0); + $this->ImagickD->ImagickFirstFrame = null; } - } else { + break; + case IMAGETYPE_JPEG: + case IMAGETYPE_PNG: + case IMAGETYPE_WEBP: $this->ImagickD->Imagick->resizeImage($width, $height, $this->ImagickD->imagick_filter, 1); - $this->ImagickD->Imagick->setImagePage(0, 0, 0, 0); - $this->ImagickD->ImagickFirstFrame = null; - } - } elseif ($this->ImagickD->source_image_type === IMAGETYPE_JPEG || $this->ImagickD->source_image_type === IMAGETYPE_PNG) { - // jpg or png - $this->ImagickD->Imagick->resizeImage($width, $height, $this->ImagickD->imagick_filter, 1); - } else { - $this->ImagickD->status = false; - $this->ImagickD->status_msg = 'Unable to resize this kind of image.'; - return false; - } + break; + default: + $this->ImagickD->status = false; + $this->ImagickD->status_msg = 'Unable to resize this kind of image.'; + return false; + }// endswitch; return true; }// execute diff --git a/Rundiz/Image/Drivers/Imagick/Rotate.php b/Rundiz/Image/Drivers/Imagick/Rotate.php index 201cc93..b1be552 100644 --- a/Rundiz/Image/Drivers/Imagick/Rotate.php +++ b/Rundiz/Image/Drivers/Imagick/Rotate.php @@ -32,7 +32,6 @@ public function execute($degree = 90) // rotate by degree switch ($this->ImagickD->source_image_type) { case IMAGETYPE_GIF: - // gif if ($this->ImagickD->source_image_frames > 1) { $this->ImagickD->Imagick = $this->ImagickD->Imagick->coalesceImages(); if (is_object($this->ImagickD->Imagick)) { @@ -54,9 +53,8 @@ public function execute($degree = 90) } break; case IMAGETYPE_JPEG: - // jpg case IMAGETYPE_PNG: - // png + case IMAGETYPE_WEBP: $this->ImagickD->Imagick->rotateImage(new \ImagickPixel('rgba(255, 255, 255, 0)'), $this->calculateCounterClockwise($degree)); break; default: @@ -110,9 +108,8 @@ public function execute($degree = 90) } break; case IMAGETYPE_JPEG: - // jpg case IMAGETYPE_PNG: - // png + case IMAGETYPE_WEBP: if ($degree === 'hor') { $this->ImagickD->Imagick->flopImage(); } elseif ($degree == 'vrt') { diff --git a/Rundiz/Image/Drivers/Imagick/Save.php b/Rundiz/Image/Drivers/Imagick/Save.php index c544576..4922f8a 100644 --- a/Rundiz/Image/Drivers/Imagick/Save.php +++ b/Rundiz/Image/Drivers/Imagick/Save.php @@ -60,8 +60,11 @@ public function execute($file_name) // covnert from transparent to white before save $this->fillWhiteToImage(); - } elseif ($this->ImagickD->source_image_type === IMAGETYPE_PNG) { - // if source file is png + } elseif ( + $this->ImagickD->source_image_type === IMAGETYPE_PNG || + $this->ImagickD->source_image_type === IMAGETYPE_WEBP + ) { + // if source file is png or webp // set image page. $this->ImagickD->Imagick->setImagePage(0, 0, 0, 0); // convert from transparent to white before save diff --git a/Rundiz/Image/Drivers/Imagick/Show.php b/Rundiz/Image/Drivers/Imagick/Show.php index c634243..cde4681 100644 --- a/Rundiz/Image/Drivers/Imagick/Show.php +++ b/Rundiz/Image/Drivers/Imagick/Show.php @@ -66,8 +66,11 @@ public function execute($file_ext = '') // covnert from transparent to white before save $this->fillWhiteToImage(); - } elseif ($this->ImagickD->source_image_type === IMAGETYPE_PNG) { - // if source file is png + } elseif ( + $this->ImagickD->source_image_type === IMAGETYPE_PNG || + $this->ImagickD->source_image_type === IMAGETYPE_WEBP + ) { + // if source file is png or webp // convert from transparent to white before save $this->fillWhiteToImage(); } diff --git a/Rundiz/Image/Drivers/Imagick/Watermark.php b/Rundiz/Image/Drivers/Imagick/Watermark.php index aa0fb2c..0c5b789 100644 --- a/Rundiz/Image/Drivers/Imagick/Watermark.php +++ b/Rundiz/Image/Drivers/Imagick/Watermark.php @@ -26,11 +26,8 @@ public function applyImage($wm_img_start_x = 0, $wm_img_start_y = 0) { switch ($this->ImagickD->watermark_image_type) { case IMAGETYPE_GIF: - // gif case IMAGETYPE_JPEG: - // jpg case IMAGETYPE_PNG: - // png if ($this->ImagickD->source_image_frames > 1) { // if source image is animated gif $this->ImagickD->Imagick = $this->ImagickD->Imagick->coalesceImages(); diff --git a/Rundiz/Image/Extensions/WebP.php b/Rundiz/Image/Extensions/WebP.php index fae5ec3..06f150b 100644 --- a/Rundiz/Image/Extensions/WebP.php +++ b/Rundiz/Image/Extensions/WebP.php @@ -16,6 +16,19 @@ class WebP { + /** + * Check that WEBP constant (`IMAGETYPE_WEBP`) is already define, if not then define it. + * + * This constant is not define prior PHP 7.1. + */ + public function checkWebPConstant() + { + if (!defined('IMAGETYPE_WEBP')) { + define('IMAGETYPE_WEBP', 18); + } + }// checkWebPConstant + + /** * Get WebP file info. * diff --git a/Rundiz/Image/Traits/ImageTrait.php b/Rundiz/Image/Traits/ImageTrait.php index 53cc9c8..87ad3d5 100644 --- a/Rundiz/Image/Traits/ImageTrait.php +++ b/Rundiz/Image/Traits/ImageTrait.php @@ -42,12 +42,20 @@ protected function getImageFileData($imagePath) version_compare(PHP_VERSION, '7.1.0', '<') && function_exists('imagecreatefromwebp') ) { - // if it is .webp and current PHP version is not supported and webp feature for GD is enabled. + // if it is .webp and current PHP version is not supported (getimagesize not work prior PHP 7.1) + // and webp feature for GD is enabled. try { - $WebP = new Extensions\WebP(); + $WebP = new \Rundiz\Image\Extensions\WebP(); $webpInfo = $WebP->webPInfo($imagePath); - if (is_array($webpInfo) && isset($webpInfo['Animation']) && $webpInfo['Animation'] === false) { + if ( + is_array($webpInfo) && + (isset($webpInfo['ANIMATION']) && $webpInfo['ANIMATION'] === false) + ) { // if not animated webp. + if (version_compare(PHP_VERSION, '7.0', '<') && isset($webpInfo['ALPHA']) && $webpInfo['ALPHA'] === true) { + // if current PHP version is not supported for transparent webp. + return false; + } $output = []; // use gd to get width, height. @@ -55,9 +63,6 @@ function_exists('imagecreatefromwebp') if (false !== $GD) { $output[0] = imagesx($GD); $output[1] = imagesy($GD); - if (!defined('IMAGETYPE_WEBP')) { - define('IMAGETYPE_WEBP', 18); - } $output[2] = IMAGETYPE_WEBP; $output['mime'] = 'image/webp'; $output['ext'] = '.webp'; diff --git a/tests/phpunit/PHP71/Extensions/WebPTest.php b/tests/phpunit/PHP71/Extensions/WebPTest.php index 9f14f32..4a11cba 100644 --- a/tests/phpunit/PHP71/Extensions/WebPTest.php +++ b/tests/phpunit/PHP71/Extensions/WebPTest.php @@ -19,6 +19,9 @@ public function testWebPInfo() $Webp = new \Rundiz\Image\Extensions\WebP(); $webpInfo = $Webp->webPInfo(static::$source_images_dir . 'city-amsterdam.webp'); $this->assertFalse($webpInfo['ANIMATION']); + $this->assertTrue($webpInfo['ALPHA']); + $webpInfo = $Webp->webPInfo(static::$source_images_dir . 'city-amsterdam-non-transparent.webp'); + $this->assertFalse($webpInfo['ANIMATION']); $this->assertFalse($webpInfo['ALPHA']); $webpInfo = $Webp->webPInfo(static::$source_images_dir . 'city-amsterdam-animated.webp'); $this->assertTrue($webpInfo['ANIMATION']); diff --git a/tests/via-http/include-image-source.php b/tests/via-http/include-image-source.php index 9b963e5..ab30dd2 100644 --- a/tests/via-http/include-image-source.php +++ b/tests/via-http/include-image-source.php @@ -4,6 +4,7 @@ $source_image_pngnt = '../source-images/city-amsterdam-non-transparent.png'; $source_image_gif = '../source-images/city-amsterdam.gif'; $source_image_animated_gif = '../source-images/city-amsterdam-animated.gif'; +$source_image_webp = '../source-images/city-amsterdam.webp'; $source_image_fake = '../source-images/city-amsterdam-jpg.png'; $source_image_fake2 = '../source-images/city-amsterdam-text.jpg'; $source_image_404 = '../source-images/city-amsterdam.404'; @@ -18,4 +19,28 @@ 'GIF' => [ 'source_image_path' => $source_image_gif, ], -]; \ No newline at end of file + 'WEBP' => [ + 'source_image_path' => $source_image_webp, + ], +]; +$test_data_pngnt = [ + 'Non transparent PNG' => [ + 'source_image_path' => $source_image_pngnt, + ], +]; +$test_data_falsy = [ + 'Wrong image extension' => [ + 'source_image_path' => $source_image_fake, + ], + 'Fake image' => [ + 'source_image_path' => $source_image_fake2, + ], + 'Not exists image' => [ + 'source_image_path' => $source_image_404, + ], +]; +$test_data_gifa = [ + 'GIF Animation' => [ + 'source_image_path' => $source_image_animated_gif, + ], +]; diff --git a/tests/via-http/index.php b/tests/via-http/index.php index 962ddec..44cfee6 100644 --- a/tests/via-http/index.php +++ b/tests/via-http/index.php @@ -21,8 +21,8 @@
'.print_r($Image, true).''."\n"; + if ($Image->status !== true) { + echo '
' . $Image->status_msg . '
' . "\n"; + $sourceExt = strtolower(pathinfo($item['source_image_path'], PATHINFO_EXTENSION)); + if ($sourceExt === 'webp') { + $WebP = new \Rundiz\Image\Extensions\WebP(); + echo 'WebP info' . "\n"; + echo ''; + echo var_export($WebP->webPInfo($item['source_image_path']), true); + echo '' . "\n"; + unset($WebP); + }// endif; soure ext + unset($sourceExt); + }// endif status unset($Image); } echo "\n\n"; @@ -34,10 +51,13 @@ function displayTestSaveCrossExts(array $test_data_set) echo '