handle image error in reception view
This commit is contained in:
parent
4946421e38
commit
0e566c1d77
@ -147,6 +147,7 @@ class GD
|
||||
|
||||
public function save($file, $quality = 90)
|
||||
{
|
||||
try{
|
||||
switch($this->_mime) {
|
||||
case 'image/jpeg':
|
||||
return imagejpeg($this->_image, $file, $quality);
|
||||
@ -159,6 +160,9 @@ class GD
|
||||
return imagegif($this->_image, $file);
|
||||
break;
|
||||
}
|
||||
}catch (\Exception $e){
|
||||
\Yii::error("Failed to save image image:", $e->getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace common\components;
|
||||
|
||||
use Yii;
|
||||
@ -9,14 +10,15 @@ use common\components\GD;
|
||||
|
||||
class Image
|
||||
{
|
||||
|
||||
/**
|
||||
* @param UploadedFile $fileInstance
|
||||
* @param string $dir relative dir from upload dir
|
||||
* @param unknown $resizeWidth
|
||||
* @param unknown $resizeHeight
|
||||
* @param bool $resizeCrop
|
||||
* @throws HttpException*/
|
||||
|
||||
/**
|
||||
* @param UploadedFile $fileInstance
|
||||
* @param string $dir relative dir from upload dir
|
||||
* @param unknown $resizeWidth
|
||||
* @param unknown $resizeHeight
|
||||
* @param bool $resizeCrop
|
||||
* @throws HttpException
|
||||
*/
|
||||
public static function upload(UploadedFile $fileInstance, $dir = '', $resizeWidth = null, $resizeHeight = null, $resizeCrop = false)
|
||||
{
|
||||
$fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::getFileName($fileInstance);
|
||||
@ -25,29 +27,29 @@ class Image
|
||||
? self::copyResizedImage($fileInstance->tempName, $fileName, $resizeWidth, $resizeHeight, $resizeCrop)
|
||||
: $fileInstance->saveAs($fileName);
|
||||
|
||||
if(!$uploaded){
|
||||
throw new HttpException(500, 'Cannot upload file "'.$fileName.'". Please check write permissions.');
|
||||
if (!$uploaded) {
|
||||
throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.');
|
||||
}
|
||||
|
||||
return Upload::getLink($fileName);
|
||||
}
|
||||
|
||||
public static function saveBinary($binary_data, $dir = '')
|
||||
{
|
||||
$fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::genFileName("jpg");
|
||||
|
||||
$uploaded = file_put_contents( $fileName, $binary_data );
|
||||
$uploaded = file_put_contents($fileName, $binary_data);
|
||||
|
||||
if(!$uploaded){
|
||||
throw new HttpException(500, 'Cannot upload file "'.$fileName.'". Please check write permissions.');
|
||||
if (!$uploaded) {
|
||||
throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.');
|
||||
}
|
||||
|
||||
return Upload::getLink($fileName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param unknown $filename
|
||||
* @param unknown $width
|
||||
* @param unknown $height
|
||||
@ -56,16 +58,14 @@ class Image
|
||||
*/
|
||||
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);
|
||||
$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;
|
||||
$thumbWebFile = '/' . Upload::$UPLOADS_DIR . '/thumbs/' . $thumbName;
|
||||
if(file_exists($thumbFile)){
|
||||
if (file_exists($thumbFile)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -74,39 +74,38 @@ class Image
|
||||
|
||||
static function copyResizedImage($inputFile, $outputFile, $width, $height = null, $crop = true)
|
||||
{
|
||||
if (extension_loaded('gd'))
|
||||
{
|
||||
$image = new GD($inputFile);
|
||||
if (extension_loaded('gd')) {
|
||||
try {
|
||||
$image = new GD($inputFile);
|
||||
|
||||
if($height) {
|
||||
if($width && $crop){
|
||||
$image->cropThumbnail($width, $height);
|
||||
if ($height) {
|
||||
if ($width && $crop) {
|
||||
$image->cropThumbnail($width, $height);
|
||||
} else {
|
||||
$image->resize($width, $height);
|
||||
}
|
||||
} else {
|
||||
$image->resize($width, $height);
|
||||
$image->resize($width);
|
||||
}
|
||||
} else {
|
||||
$image->resize($width);
|
||||
return $image->save($outputFile);
|
||||
} catch (\Exception $e) {
|
||||
\Yii::error("Failed to create thumbnail: ". $e->getMessage());
|
||||
}
|
||||
return $image->save($outputFile);
|
||||
}
|
||||
elseif(extension_loaded('imagick'))
|
||||
{
|
||||
} elseif (extension_loaded('imagick')) {
|
||||
$image = new \Imagick($inputFile);
|
||||
|
||||
if($height && !$crop) {
|
||||
if ($height && !$crop) {
|
||||
$image->resizeImage($width, $height, \Imagick::FILTER_LANCZOS, 1, true);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
$image->resizeImage($width, null, \Imagick::FILTER_LANCZOS, 1);
|
||||
}
|
||||
|
||||
if($height && $crop){
|
||||
if ($height && $crop) {
|
||||
$image->cropThumbnailImage($width, $height);
|
||||
}
|
||||
|
||||
return $image->writeImage($outputFile);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
throw new HttpException(500, 'Please install GD or Imagick extension');
|
||||
}
|
||||
}
|
||||
|
||||
BIN
doc/image-036276f377.jpg
Normal file
BIN
doc/image-036276f377.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 170 KiB |
Loading…
Reference in New Issue
Block a user