diff --git a/common/components/GD.php b/common/components/GD.php index 9bafe16..7236bf1 100644 --- a/common/components/GD.php +++ b/common/components/GD.php @@ -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; } } \ No newline at end of file diff --git a/common/components/Image.php b/common/components/Image.php index f72edc3..c43f48e 100644 --- a/common/components/Image.php +++ b/common/components/Image.php @@ -1,4 +1,5 @@ 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'); } } diff --git a/doc/image-036276f377.jpg b/doc/image-036276f377.jpg new file mode 100644 index 0000000..c5e3efa Binary files /dev/null and b/doc/image-036276f377.jpg differ