Skip to content

Commit

Permalink
Merge pull request #20 from yurkinx/resize_image_quality
Browse files Browse the repository at this point in the history
Resize image quality
  • Loading branch information
yurkinx authored Jan 25, 2019
2 parents d1e1ea3 + 8347d5f commit 96a2e44
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 28 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ http://www.yiiframework.com/extension/image/ and Kohana Image Library https://gi

Installation
------------
```code
**Install as a [composer package](https://packagist.org/packages/yurkinx/yii2-image)**
> Use this method to get continuous updates.
```
composer require yurkinx/yii2-image
```
or include the dependency in the `composer.json` file:
```json
{
"require":
{
"yurkinx/yii2-image": "dev-master"
}
"require": {
"yurkinx/yii2-image": "^1.2"
}
}
```
Configuration
Expand Down Expand Up @@ -57,6 +64,12 @@ $image->watermark(Image $watermark, $offset_x = NULL, $offset_y = NULL, $opacity
$image->resize($width, $height, \yii\image\drivers\Image::HEIGHT);
$image->resize($width, $height, \yii\image\drivers\Image::ADAPT)->background('#fff');
```
**Using resize with resize constrains and best quality output image [for Imagick driver only]**
Use 1 for best speed and lower quality, 100 for best quality and lower speed. Only values 1,100 currently supported
```php
$image->resize($width, NULL, \yii\image\drivers\Image::HEIGHT, 100);
```

Possible resize constrains:
```php
// Resizing constraints ($master)
Expand Down
43 changes: 28 additions & 15 deletions yii/image/drivers/Kohana/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function factory($file, $driver = NULL)
{
if ($driver === NULL)
{

// Use the default driver
$driver = Image::$default_driver;
}
Expand Down Expand Up @@ -174,20 +174,29 @@ public function __toString()
*
* // Resize to 200x500 pixels, ignoring aspect ratio
* $image->resize(200, 500, Image::NONE);
*
* // Resize to 400 pixels on the shortest side, puts it in the center
* // of the image with the transparent edges, keeping aspect ratio,
*
* // Resize to 400 pixels on the shortest side, puts it in the center
* // of the image with the transparent edges, keeping aspect ratio,
* // output size will be 400x400 pixels
* $image->resize(400, 400, Image::ADAPT);
*
* // Resize to 500 pixels width, keeping aspect ratio
* // and trying to achieve best quality, using slower filters
*
* $image->resize(500, NULL, NULL, 100)
*
* @param integer $width new width
* @param integer $height new height
* @param integer $master master dimension
* @param integer $quality quality of the output image 1-100 (range is reserved for future use) use 1 for best speed, 100 for best quality. Default 1, best speed
* @return $this
* @uses Image::_do_resize
*/
public function resize($width = NULL, $height = NULL, $master = NULL)
public function resize($width = NULL, $height = NULL, $master = NULL, $quality = NULL)
{
if($quality === NULL){
$quality = 1;
}
if ($master === NULL)
{
// Choose the master dimension automatically
Expand All @@ -203,7 +212,7 @@ public function resize($width = NULL, $height = NULL, $master = NULL)
$master = $this->width / $this->height > $width / $height ? Image::HEIGHT : Image::WIDTH;
$this->resize($width, $height, $master);

if ($this->width !== $width || $this->height !== $height)
if ($this->width !== $width || $this->height !== $height)
{
$offset_x = round(($this->width - $width) / 2);
$offset_y = round(($this->height - $height) / 2);
Expand All @@ -228,13 +237,13 @@ public function resize($width = NULL, $height = NULL, $master = NULL)
// Set empty width for backward compatibility
$width = NULL;
}
elseif ($master === Image::ADAPT)
elseif ($master === Image::ADAPT)
{
if (empty($width))
if (empty($width))
{
$width = $this->width * $height / $this->height;
}
elseif (empty($height))
}
elseif (empty($height))
{
$height = $this->height * $width / $this->width;
}
Expand Down Expand Up @@ -317,12 +326,12 @@ public function resize($width = NULL, $height = NULL, $master = NULL)

$offset_x = $offset_y = 0;

if ($width / $height > $image_width / $image_height)
if ($width / $height > $image_width / $image_height)
{
$bg_width = floor($image_height * $width / $height);
$offset_x = abs(floor(($bg_width - $image_width) / 2));
}
else
else
{
$bg_height = floor($image_width * $height / $width);
$offset_y = abs(floor(($bg_height - $image_height) / 2));
Expand All @@ -331,7 +340,10 @@ public function resize($width = NULL, $height = NULL, $master = NULL)
$this->_do_adapt($image_width, $image_height, $bg_width, $bg_height, $offset_x, $offset_y);
}

$this->_do_resize($width, $height);
// The quality must be in the range of 1 to 100
$quality = min(max($quality, 1), 100);

$this->_do_resize($width, $height, $quality);

return $this;
}
Expand Down Expand Up @@ -748,13 +760,14 @@ public function render($type = NULL, $quality = 100)
*
* @param integer $width new width
* @param integer $height new height
* @param integer $quality quality of output image: 1-100. default 1 for best resizing speed
* @return void
*/
abstract protected function _do_resize($width, $height);
abstract protected function _do_resize($width, $height, $quality);

/**
* Adaptation the image.
*
*
* @param integer $width image width
* @param integer $height image height
* @param integer $bg_width background width
Expand Down
3 changes: 2 additions & 1 deletion yii/image/drivers/Kohana/Image/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ protected function _load_image()
*
* @param integer $width new width
* @param integer $height new height
* @param integer $quality - reserved for future use
* @return void
*/
protected function _do_resize($width, $height)
protected function _do_resize($width, $height, $quality)
{
// Presize width and height
$pre_width = $this->width;
Expand Down
31 changes: 24 additions & 7 deletions yii/image/drivers/Kohana/Image/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,33 @@ public function __destruct()
$this->im->destroy();
}

protected function _do_resize($width, $height)
/**
* Scales an image using resizeImage for best quality @link http://php.net/manual/en/imagick.resizeimage.php
* but lower speed, or scaleImage for best speed and lower quality @link http://php.net/manual/en/imagick.scaleimage.php
* @param int $width
* @param int $height
* @param int $quality
* @return bool
*/
protected function _do_resize($width, $height, $quality)
{
if ($this->im->scaleImage($width, $height))
{
// Reset the width and height
$this->width = $this->im->getImageWidth();
$this->height = $this->im->getImageHeight();
if($quality == 100) {
if ($this->im->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1)) {
// Reset the width and height
$this->width = $this->im->getImageWidth();
$this->height = $this->im->getImageHeight();

return TRUE;
return TRUE;
}
}else{
if ($this->im->sampleImage($width, $height)) {
// Reset the width and height
$this->width = $this->im->getImageWidth();
$this->height = $this->im->getImageHeight();

return TRUE;
}
}

return FALSE;
}
Expand Down

0 comments on commit 96a2e44

Please sign in to comment.