Skip to content

imagick && gd support webp format,solve php warning bug,imagick handle gif resize size bug #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions src/Grafika/Gd/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public function resizeExactHeight(&$image, $newHeight)
$resizeHeight = $newHeight;
$resizeWidth = $newHeight * $ratio;

$this->_resize($image, $resizeWidth, $resizeHeight);
$this->_resize($image, intval(round($resizeWidth)), $resizeHeight);

return $this;
}
Expand All @@ -596,7 +596,7 @@ public function resizeExactWidth(&$image, $newWidth)
$ratio = $width / $height;

$resizeWidth = $newWidth;
$resizeHeight = round($newWidth / $ratio);
$resizeHeight = intval(round($newWidth / $ratio));

$this->_resize($image, $resizeWidth, $resizeHeight);

Expand Down Expand Up @@ -628,7 +628,7 @@ public function resizeFill(&$image, $newWidth, $newHeight)
$optimumHeight = $newHeight;
}

$this->_resize($image, $optimumWidth, $optimumHeight);
$this->_resize($image, intval(round($optimumWidth)), intval(round($optimumHeight)));
$this->crop($image, $newWidth, $newHeight); // Trim excess parts

return $this;
Expand Down Expand Up @@ -748,6 +748,14 @@ public function save($image, $file, $type = null, $quality = null, $interlace =
imagepng($image->getCore(), $file);
break;

case ImageType::BMP :
imagebmp($image->getCore(), $file);
break;

case ImageType::WEBP :
imagewebp($image->getCore(), $file, $quality);
break;

default: // Defaults to jpeg
$quality = ($quality === null) ? 75 : $quality; // Default to 75 (GDs default) if null.
$quality = ($quality > 100) ? 100 : $quality;
Expand Down Expand Up @@ -1058,6 +1066,10 @@ private function _getImageTypeFromFileName($imageFile)
return ImageType::PNG;
} else if ('wbm' === $ext or 'wbmp' === $ext) {
return ImageType::WBMP;
} else if ('bmp' == $ext) {
return ImageType::BMP;
} else if ('webp' == $ext) {
return ImageType::WEBP;
} else {
return ImageType::UNKNOWN;
}
Expand Down Expand Up @@ -1176,4 +1188,4 @@ private function _smartCrop($oldImage, $cropW, $cropH){

return array($x,$y);
}
}
}
6 changes: 3 additions & 3 deletions src/Grafika/Gd/Helper/GifHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ public function resize($blocks, $newW, $newH){
$dY * $cY, // dy
0,
0,
$image['frames'][0]['imageWidth'] * $cX,
$image['frames'][0]['imageHeight'] * $cY,
intval(round($image['frames'][0]['imageWidth'] * $cX)),
intval(round($image['frames'][0]['imageHeight'] * $cY)),
$width,
$height
);
Expand Down Expand Up @@ -602,4 +602,4 @@ private function _fixSize($string, $size, $char='0'){
private function _switchEndian($hexString) {
return implode('', array_reverse(str_split($hexString, 2)));
}
}
}
22 changes: 21 additions & 1 deletion src/Grafika/Gd/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public function blob( $type = 'PNG' ) {

imagewbmp( $this->gd );

} else if ( ImageType::WEBP == $type ) {

imagewebp( $this->gd );

} else {
throw new \Exception( sprintf( 'File type "%s" not supported.', $type ) );
}
Expand Down Expand Up @@ -141,6 +145,10 @@ public static function createFromFile( $imageFile ) {

return self::_createWbmp( $imageFile );

} else if ( ImageType::WEBP == $type ) {

return self::_createWebp( $imageFile );

} else {
throw new \Exception( sprintf( 'Could not open "%s". File type not supported.', $imageFile ) );
}
Expand Down Expand Up @@ -418,6 +426,16 @@ private static function _createWbmp( $imageFile ){
return new self( $gd, $imageFile, imagesx( $gd ), imagesy( $gd ), ImageType::WBMP );
}

private static function _createWebp( $imageFile ){
$gd = @imagecreatefromwebp( $imageFile );

if(!$gd){
throw new \Exception( sprintf('Could not open "%s". Not a valid %s file.', $imageFile, ImageType::WBMP) );
}

return new self( $gd, $imageFile, imagesx( $gd ), imagesy( $gd ), ImageType::WEBP );
}

/**
* @param $imageFile
*
Expand Down Expand Up @@ -450,8 +468,10 @@ private static function _guessType( $imageFile ){

return ImageType::WBMP;

} else if ( IMG_WEBP == $type ) {
return ImageType::WEBP;
}

return ImageType::UNKNOWN;
}
}
}
6 changes: 5 additions & 1 deletion src/Grafika/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ class ImageType {
const PNG = 'PNG';

const WBMP = 'WBMP';
}

const BMP = 'BMP';

const WEBP = 'WEBP';
}
20 changes: 15 additions & 5 deletions src/Grafika/Imagick/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public function resizeExactHeight(&$image, $newHeight)
$ratio = $width / $height;

$resizeHeight = $newHeight;
$resizeWidth = $newHeight * $ratio;
$resizeWidth = intval(round($newHeight * $ratio));

$this->_resize($image, $resizeWidth, $resizeHeight);

Expand All @@ -494,7 +494,7 @@ public function resizeExactWidth(&$image, $newWidth)
$ratio = $width / $height;

$resizeWidth = $newWidth;
$resizeHeight = round($newWidth / $ratio);
$resizeHeight = intval(round($newWidth / $ratio));

$this->_resize($image, $resizeWidth, $resizeHeight);

Expand Down Expand Up @@ -526,7 +526,7 @@ public function resizeFill(&$image, $newWidth, $newHeight)
$optimumHeight = $newHeight;
}

$this->_resize($image, $optimumWidth, $optimumHeight);
$this->_resize($image, intval(round($optimumWidth)), intval(round($optimumHeight)));
$this->crop($image, $newWidth, $newHeight); // Trim excess parts

return $this;
Expand Down Expand Up @@ -624,6 +624,12 @@ public function save( $image, $file, $type = null, $quality = null, $interlace =
$image->getCore()->writeImages($file, true); // Support animated image. Eg. GIF
break;

case ImageType::BMP :
case ImageType::WEBP :
$image->getCore()->setFormat($type);
$image->getCore()->setImageCompressionQuality($quality);
$image->getCore()->writeImages($file, true);
break;
case ImageType::PNG :
// PNG is lossless and does not need compression. Although GD allow values 0-9 (0 = no compression), we leave it alone.
$image->getCore()->setImageFormat($type);
Expand Down Expand Up @@ -791,7 +797,7 @@ private function _resize(&$image, $newWidth, $newHeight)

// Assign new image with frames
$image = new Image($imagick->deconstructImages(), $image->getImageFile(), $newWidth, $newHeight,
$image->getType());
$image->getType(), $image->isAnimated());
} else { // Single frame image. Eg. JPEG, PNG

$image->getCore()->resizeImage($newWidth, $newHeight, \Imagick::FILTER_LANCZOS, 1, false);
Expand Down Expand Up @@ -819,9 +825,13 @@ private function _getImageTypeFromFileName($imageFile)
return ImageType::GIF;
} else if ('png' == $ext) {
return ImageType::PNG;
} else if ('bmp' == $ext) {
return ImageType::BMP;
} else if ('webp' == $ext) {
return ImageType::WEBP;
} else {
return ImageType::UNKNOWN;
}
}

}
}
4 changes: 2 additions & 2 deletions src/Grafika/Imagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function createFromFile( $imageFile ){

$imagick = new \Imagick( realpath($imageFile) );
$animated = false;
if ($imagick->getImageIterations() > 0) {
if ($imagick->getNumberImages() > 0) {
$animated = true;
}

Expand Down Expand Up @@ -266,4 +266,4 @@ public function isAnimated() {
return $this->animated;
}

}
}