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)
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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);
|
||||||
@ -25,27 +27,27 @@ class Image
|
|||||||
? self::copyResizedImage($fileInstance->tempName, $fileName, $resizeWidth, $resizeHeight, $resizeCrop)
|
? self::copyResizedImage($fileInstance->tempName, $fileName, $resizeWidth, $resizeHeight, $resizeCrop)
|
||||||
: $fileInstance->saveAs($fileName);
|
: $fileInstance->saveAs($fileName);
|
||||||
|
|
||||||
if(!$uploaded){
|
if (!$uploaded) {
|
||||||
throw new HttpException(500, 'Cannot upload file "'.$fileName.'". Please check write permissions.');
|
throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.');
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
||||||
|
|
||||||
$uploaded = file_put_contents( $fileName, $binary_data );
|
$uploaded = file_put_contents($fileName, $binary_data);
|
||||||
|
|
||||||
if(!$uploaded){
|
if (!$uploaded) {
|
||||||
throw new HttpException(500, 'Cannot upload file "'.$fileName.'". Please check write permissions.');
|
throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return Upload::getLink($fileName);
|
return Upload::getLink($fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @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,12 +74,12 @@ 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) {
|
||||||
if($width && $crop){
|
if ($width && $crop) {
|
||||||
$image->cropThumbnail($width, $height);
|
$image->cropThumbnail($width, $height);
|
||||||
} else {
|
} else {
|
||||||
$image->resize($width, $height);
|
$image->resize($width, $height);
|
||||||
@ -88,25 +88,24 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($height && $crop){
|
if ($height && $crop) {
|
||||||
$image->cropThumbnailImage($width, $height);
|
$image->cropThumbnailImage($width, $height);
|
||||||
}
|
}
|
||||||
|
|
||||||
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
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