Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
himiklab committed Jan 10, 2017
1 parent 053a6ad commit 2e4f8be
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion EasyThumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EasyThumbnail extends Object
/** @var string $cacheAlias path alias relative with @web where the cache files are kept */
public $cacheAlias = 'assets/thumbnails';

/** @var int $cacheExpire seconds */
/** @var integer $cacheExpire seconds */
public $cacheExpire = 0;

public function init()
Expand Down
63 changes: 42 additions & 21 deletions EasyThumbnailImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace himiklab\thumbnail;

use Imagine\Image\Box;
use Imagine\Image\ManipulatorInterface;
use Yii;
use yii\helpers\Html;
use yii\helpers\FileHelper;
use yii\helpers\Html;
use yii\imagine\Image;
use Imagine\Image\Box;
use Imagine\Image\ManipulatorInterface;

/**
* Yii2 helper for creating and caching thumbnails on real time
Expand All @@ -24,6 +24,7 @@ class EasyThumbnailImage
const THUMBNAIL_OUTBOUND = ManipulatorInterface::THUMBNAIL_OUTBOUND;
const THUMBNAIL_INSET = ManipulatorInterface::THUMBNAIL_INSET;
const QUALITY = 50;
const MKDIR_MODE = 0755;

/** @var string $cacheAlias path alias relative with @web where the cache files are kept */
public static $cacheAlias = 'assets/thumbnails';
Expand All @@ -38,6 +39,7 @@ class EasyThumbnailImage
* @param integer $width the width in pixels to create the thumbnail
* @param integer $height the height in pixels to create the thumbnail
* @param string $mode self::THUMBNAIL_INSET, the original image
* @param integer $quality
* is scaled down so it is fully contained within the thumbnail dimensions.
* The specified $width and $height (supplied via $size) will be considered
* maximum limits. Unless the given dimensions are equal to the original image’s
Expand All @@ -47,7 +49,11 @@ class EasyThumbnailImage
* corresponding side in the original image. Any excess outside of the scaled
* thumbnail’s area will be cropped, and the returned thumbnail will have
* the exact $width and $height specified
* @throws \Imagine\Exception\RuntimeException
* @throws \Imagine\Exception\InvalidArgumentException
* @throws FileNotFoundException
* @return \Imagine\Image\ImageInterface
* @throws \yii\base\InvalidParamException
*/
public static function thumbnail($filename, $width, $height, $mode = self::THUMBNAIL_OUTBOUND, $quality = null)
{
Expand All @@ -57,12 +63,16 @@ public static function thumbnail($filename, $width, $height, $mode = self::THUMB
/**
* Creates and caches the image thumbnail and returns full path from thumbnail file.
*
* @param string $filename
* @param integer $width
* @param integer $height
* @param string $mode
* @param string $filename the image file path or path alias
* @param integer $width the width in pixels to create the thumbnail
* @param integer $height the height in pixels to create the thumbnail
* @param string $mode self::THUMBNAIL_INSET, the original image
* @param integer $quality
* @return string
* @throws FileNotFoundException
* @throws \Imagine\Exception\InvalidArgumentException
* @throws \Imagine\Exception\RuntimeException
* @throws \yii\base\InvalidParamException
*/
public static function thumbnailFile($filename, $width, $height, $mode = self::THUMBNAIL_OUTBOUND, $quality = null)
{
Expand All @@ -85,16 +95,16 @@ public static function thumbnailFile($filename, $width, $height, $mode = self::T
}
}
if (!is_dir($thumbnailFilePath)) {
mkdir($thumbnailFilePath, 0755, true);
mkdir($thumbnailFilePath, self::MKDIR_MODE, true);
}

$box = new Box($width, $height);
$image = Image::getImagine()->open($filename);
$image = $image->thumbnail($box, $mode);

$options = [
'quality'=> $quality === null ? self::QUALITY : $quality
];
'quality' => $quality === null ? self::QUALITY : $quality
];

$image->save($thumbnailFile, $options);
return $thumbnailFile;
Expand All @@ -103,11 +113,16 @@ public static function thumbnailFile($filename, $width, $height, $mode = self::T
/**
* Creates and caches the image thumbnail and returns URL from thumbnail file.
*
* @param string $filename
* @param integer $width
* @param integer $height
* @param string $mode
* @param string $filename the image file path or path alias
* @param integer $width the width in pixels to create the thumbnail
* @param integer $height the height in pixels to create the thumbnail
* @param string $mode self::THUMBNAIL_INSET, the original image
* @param integer $quality
* @return string
* @throws FileNotFoundException
* @throws \Imagine\Exception\InvalidArgumentException
* @throws \Imagine\Exception\RuntimeException
* @throws \yii\base\InvalidParamException
*/
public static function thumbnailFileUrl($filename, $width, $height, $mode = self::THUMBNAIL_OUTBOUND, $quality = null)
{
Expand All @@ -124,11 +139,12 @@ public static function thumbnailFileUrl($filename, $width, $height, $mode = self
/**
* Creates and caches the image thumbnail and returns <img> tag.
*
* @param string $filename
* @param integer $width
* @param integer $height
* @param string $mode
* @param string $filename the image file path or path alias
* @param integer $width the width in pixels to create the thumbnail
* @param integer $height the height in pixels to create the thumbnail
* @param string $mode self::THUMBNAIL_INSET, the original image
* @param array $options options similarly with \yii\helpers\Html::img()
* @param integer $quality
* @return string
*/
public static function thumbnailImg($filename, $width, $height, $mode = self::THUMBNAIL_OUTBOUND, $options = [], $quality = null)
Expand All @@ -149,13 +165,14 @@ public static function thumbnailImg($filename, $width, $height, $mode = self::TH
/**
* Clear cache directory.
*
* @throws \yii\base\InvalidParamException
* @return bool
*/
public static function clearCache()
{
$cacheDir = Yii::getAlias('@webroot/' . self::$cacheAlias);
self::removeDir($cacheDir);
return @mkdir($cacheDir, 0755, true);
return @mkdir($cacheDir, self::MKDIR_MODE, true);
}

protected static function removeDir($path)
Expand All @@ -168,6 +185,11 @@ protected static function removeDir($path)
}
}

/**
* @param \Exception $error
* @param string $filename
* @return string
*/
protected static function errorHandler($error, $filename)
{
if ($error instanceof FileNotFoundException) {
Expand All @@ -177,5 +199,4 @@ protected static function errorHandler($error, $filename)
return 'Error ' . $error->getCode();
}
}

}

0 comments on commit 2e4f8be

Please sign in to comment.