diff --git a/EasyThumbnailImage.php b/EasyThumbnailImage.php index 9387ef3..06bf398 100644 --- a/EasyThumbnailImage.php +++ b/EasyThumbnailImage.php @@ -131,11 +131,8 @@ public static function thumbnailImg($filename, $width, $height, $mode = self::TH $filename = FileHelper::normalizePath(Yii::getAlias($filename)); try { $thumbnailFileUrl = self::thumbnailFileUrl($filename, $width, $height, $mode); - } catch (FileNotFoundException $e) { - return 'File doesn\'t exist'; } catch (\Exception $e) { - Yii::warning("{$e->getCode()}\n{$e->getMessage()}\n{$e->getFile()}"); - return 'Error ' . $e->getCode(); + return static::errorHandler($e, $filename); } return Html::img( @@ -165,4 +162,15 @@ protected static function removeDir($path) @rmdir($path); } } + + protected static function errorHandler($error, $filename) + { + if ($error instanceof FileNotFoundException) { + return 'File doesn\'t exist'; + } else { + Yii::warning("{$error->getCode()}\n{$error->getMessage()}\n{$error->getFile()}"); + return 'Error ' . $error->getCode(); + } + } + } diff --git a/README.md b/README.md index 12f0af3..d2b68de 100644 --- a/README.md +++ b/README.md @@ -56,3 +56,21 @@ echo EasyThumbnailImage::thumbnailImg( ``` For other functions please see the source code. + +If you want to handle errors that appear while converting to thumbnail by yourself, please make your own class and inherit it from EasyThumbnailImage. In your class replace only protected method errorHandler. For example + +```php +class ThumbHelper extends \himiklab\thumbnail\EasyThumbnailImage +{ + + protected static function errorHandler($error, $filename) + { + if ($error instanceof \himiklab\thumbnail\FileNotFoundException) { + return \yii\helpers\Html::img('@web/images/notfound.png'); + } else { + $filename = basename($filename); + return \yii\helpers\Html::a($filename,"@web/files/$filename"); + } + } +} +```