handle image error in reception view

This commit is contained in:
Schneider Roland 2022-12-27 15:54:09 +01:00
parent 4946421e38
commit 0e566c1d77
3 changed files with 44 additions and 41 deletions

View File

@ -147,6 +147,7 @@ class GD
public function save($file, $quality = 90) public function save($file, $quality = 90)
{ {
try{
switch($this->_mime) { switch($this->_mime) {
case 'image/jpeg': case 'image/jpeg':
return imagejpeg($this->_image, $file, $quality); return imagejpeg($this->_image, $file, $quality);
@ -159,6 +160,9 @@ class GD
return imagegif($this->_image, $file); return imagegif($this->_image, $file);
break; break;
} }
}catch (\Exception $e){
\Yii::error("Failed to save image image:", $e->getMessage());
}
return false; return false;
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
namespace common\components; namespace common\components;
use Yii; use Yii;
@ -16,7 +17,8 @@ class Image
* @param unknown $resizeWidth * @param unknown $resizeWidth
* @param unknown $resizeHeight * @param unknown $resizeHeight
* @param bool $resizeCrop * @param bool $resizeCrop
* @throws HttpException*/ * @throws HttpException
*/
public static function upload(UploadedFile $fileInstance, $dir = '', $resizeWidth = null, $resizeHeight = null, $resizeCrop = false) public static function upload(UploadedFile $fileInstance, $dir = '', $resizeWidth = null, $resizeHeight = null, $resizeCrop = false)
{ {
$fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::getFileName($fileInstance); $fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::getFileName($fileInstance);
@ -31,6 +33,7 @@ class Image
return Upload::getLink($fileName); return Upload::getLink($fileName);
} }
public static function saveBinary($binary_data, $dir = '') public static function saveBinary($binary_data, $dir = '')
{ {
$fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::genFileName("jpg"); $fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::genFileName("jpg");
@ -45,7 +48,6 @@ class Image
} }
/** /**
* *
* @param unknown $filename * @param unknown $filename
@ -56,16 +58,14 @@ class Image
*/ */
static function thumb($filename, $width = null, $height = null, $crop = true) static function thumb($filename, $width = null, $height = null, $crop = true)
{ {
if($filename && file_exists(($filename = Yii::getAlias('@frontend/web') . $filename))) if ($filename && file_exists(($filename = Yii::getAlias('@frontend/web') . $filename))) {
{
$info = pathinfo($filename); $info = pathinfo($filename);
$thumbName = $info['filename'] . '-' . md5(filemtime($filename) . (int)$width . (int)$height . (int)$crop) . '.' . $info['extension']; $thumbName = $info['filename'] . '-' . md5(filemtime($filename) . (int)$width . (int)$height . (int)$crop) . '.' . $info['extension'];
$thumbFile = Yii::getAlias('@frontend/web') . DIRECTORY_SEPARATOR . Upload::$UPLOADS_DIR . DIRECTORY_SEPARATOR . 'thumbs' . DIRECTORY_SEPARATOR . $thumbName; $thumbFile = Yii::getAlias('@frontend/web') . DIRECTORY_SEPARATOR . Upload::$UPLOADS_DIR . DIRECTORY_SEPARATOR . 'thumbs' . DIRECTORY_SEPARATOR . $thumbName;
$thumbWebFile = '/' . Upload::$UPLOADS_DIR . '/thumbs/' . $thumbName; $thumbWebFile = '/' . Upload::$UPLOADS_DIR . '/thumbs/' . $thumbName;
if (file_exists($thumbFile)) { if (file_exists($thumbFile)) {
return $thumbWebFile; return $thumbWebFile;
} } elseif (FileHelper::createDirectory(dirname($thumbFile), 0777) && self::copyResizedImage($filename, $thumbFile, $width, $height, $crop)) {
elseif(FileHelper::createDirectory(dirname($thumbFile), 0777) && self::copyResizedImage($filename, $thumbFile, $width, $height, $crop)){
return $thumbWebFile; return $thumbWebFile;
} }
} }
@ -74,8 +74,8 @@ class Image
static function copyResizedImage($inputFile, $outputFile, $width, $height = null, $crop = true) static function copyResizedImage($inputFile, $outputFile, $width, $height = null, $crop = true)
{ {
if (extension_loaded('gd')) if (extension_loaded('gd')) {
{ try {
$image = new GD($inputFile); $image = new GD($inputFile);
if ($height) { if ($height) {
@ -88,15 +88,15 @@ class Image
$image->resize($width); $image->resize($width);
} }
return $image->save($outputFile); return $image->save($outputFile);
} catch (\Exception $e) {
\Yii::error("Failed to create thumbnail: ". $e->getMessage());
} }
elseif(extension_loaded('imagick')) } elseif (extension_loaded('imagick')) {
{
$image = new \Imagick($inputFile); $image = new \Imagick($inputFile);
if ($height && !$crop) { if ($height && !$crop) {
$image->resizeImage($width, $height, \Imagick::FILTER_LANCZOS, 1, true); $image->resizeImage($width, $height, \Imagick::FILTER_LANCZOS, 1, true);
} } else {
else{
$image->resizeImage($width, null, \Imagick::FILTER_LANCZOS, 1); $image->resizeImage($width, null, \Imagick::FILTER_LANCZOS, 1);
} }
@ -105,8 +105,7 @@ class Image
} }
return $image->writeImage($outputFile); return $image->writeImage($outputFile);
} } else {
else {
throw new HttpException(500, 'Please install GD or Imagick extension'); throw new HttpException(500, 'Please install GD or Imagick extension');
} }
} }

BIN
doc/image-036276f377.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB