public function getFileUrl($width = null, $height = null, $fit = null) { assert(null === $fit || self::COVER === $fit || self::CONTAIN === $fit); $path = $this->path($width, $height, $fit); if (!file_exists($path)) { $originalPath = $this->path(); set_error_handler(create_function('$severity, $message, $file, $line', 'throw new ErrorException($message, $severity, $severity, $file, $line);')); try { $image = EWideImage::load($originalPath); if (self::COVER === $fit) { $image = $image->resize($width, $height, 'outside')->crop('center', 'center', $width, $height); } else { $image = $image->resize($width, $height, 'fill'); } $image->saveToFile($path); } catch (Exception $exception) { Yii::log('Failed to resize image: ' . print_r(array('id' => $this->id, 'width' => $width, 'height' => $height, 'fit' => $fit), true), 'error', 'app.image.getUrl'); } restore_error_handler(); } return strtr('{baseUrl}/{basePath}/{filename}', array('{baseUrl}' => Yii::app()->baseUrl, '{basePath}' => Yii::app()->params['images']['basePath'], '{filename}' => $this->filename($width, $height, $fit))); }
/** * 按比例产生缩略图 * @param unknown_type $fileName 相对于Yii::app()->uploads * @param unknown_type $width * @param unknown_type $height */ public static function xImage($fileName, $width, $height) { //文件是否存在 $sourceFile = Yii::app()->basePath . "/../" . $fileName; if (!file_exists($sourceFile)) { return false; } $ext = DxdUtil::fileExt($fileName); if (!in_array(strtolower($ext), array('jpg', 'png', 'jpeg'))) { return false; } //新文件名,形如 filename_120_100.jpg $newFileName = substr($fileName, 0, strrpos($fileName, ".")) . "_" . $width . "_" . $height . "." . $ext; $targetFile = Yii::app()->basePath . "/../caches/" . $newFileName; //文件是否已经被缩略过 if (file_exists($targetFile)) { return "caches/" . $newFileName; } //开始压缩 Yii::import('application.extensions.EWideImage.EWideImage'); $dir = dirname($targetFile); if (!file_exists($dir)) { DxdUtil::createFolders($dir); } try { EWideImage::load(Yii::app()->basePath . "/../" . $fileName)->resize($width, $height)->saveToFile($targetFile); } catch (Exception $e) { return false; } return "caches/" . $newFileName; }
public function createImages($path, $bigfilename, $thumbfilename) { Yii::import('common.extensions.EWideImage.EWideImage'); //include library for image manipulation $srcfile = $this->makeFileName($path, 'raw', $bigfilename); if (file_exists($srcfile)) { $srcImg = EWideImage::load($srcfile); //load image from raw //create main image $fileName = $this->makeFileName($path, '', $bigfilename); $dstImg = $this->fitwidth($srcImg, $this->_fullSizeW, $this->_fullSizeH); $dstImg->saveToFile($fileName); //create thumb image $fileName = $this->makeFileName($path, '', $thumbfilename); $dstImg = $this->fitwidth($srcImg, $this->_thumbSizeW, $this->_thumbSizeH); $dstImg->saveToFile($fileName); foreach ($this->_imageSizes as $size) { switch ($size['operation']) { case 'crop': $dstImg = $srcImg->crop('center', 'center', $size['options']['width'], $size['options']['height']); break; case 'resize': $dstImg = $srcImg->resize($size['options']['width'], $size['options']['height'], 'fill'); break; case 'bestfit': $dstImg = $this->bestfit($srcImg, $size['options']['width'], $size['options']['height']); break; case 'fitwidth': $dstImg = $this->fitwidth($srcImg, $size['options']['width'], $size['options']['height']); break; case 'resizewidth': $dstImg = $this->resizewidth($srcImg, $size['options']['width'], $size['options']['height']); break; default: break; } $fileName = $this->makeFileName($path, $size['name'], $bigfilename); $this->makeDir(dirname($fileName)); $dstImg->saveToFile($fileName); } } }