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

Commit

Permalink
Merge pull request #4 from vkabachenko/master
Browse files Browse the repository at this point in the history
error handler function added
  • Loading branch information
himiklab committed Nov 12, 2015
2 parents 0013b7d + 1355a34 commit c19f129
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
16 changes: 12 additions & 4 deletions EasyThumbnailImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
}
}

}
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
```

0 comments on commit c19f129

Please sign in to comment.