self::$cacheExpire) { unlink($thumbnailFile); } else { return $relativelFile; } } if (!is_dir($thumbnailFilePath)) { mkdir($thumbnailFilePath, 0755, true); } $box = new Box($width, $height); $image = Image::getImagine()->open($filename); $image = $image->thumbnail($box, $mode); $image->save($thumbnailFile); return $relativelFile; } /** * @param string $filename * @param integer $width * @param integer $height * @param string $mode * @return string */ public static function thumbnailFileUrl($filename, $width, $height, $mode = self::THUMBNAIL_OUTBOUND) { $cachePath = \yii::getAlias('@webroot/' . self::$cacheAlias); $filename = FileHelper::normalizePath(\yii::getAlias($filename)); $cacheUrl = \yii::getAlias('@web/' . self::$cacheAlias); $thumbnailFilePath = self::thumbnailFile($cachePath, $filename, $width, $height, $mode); return $cacheUrl . '/' . $thumbnailFilePath; } /** * Creates and caches the image thumbnail and returns tag. * @param string $cacheRoot 缓存根目录 * @param string $filename * @param integer $width * @param integer $height * @param string $mode * @param array $options options similarly with \yii\helpers\Html::img() * @return string */ public static function thumbnailImg($filename, $width, $height, $mode = self::THUMBNAIL_OUTBOUND, $options = []) { $cachePath = \yii::getAlias('@webroot/' . self::$cacheAlias); $filename = FileHelper::normalizePath(Yii::getAlias($filename)); try { $thumbnailFileUrl = self::thumbnailFileUrl($cachePath, $filename, $width, $height, $mode); } catch (FileNotFoundException $e) { return 'File doesn\'t exist'; } catch (\Exception $e) { Yii::warning("{$e->getCode()}\n{$e->getMessage()}\n{$e->getFile()}"); return 'Error ' . $e->getCode(); } return Html::img( $thumbnailFileUrl, $options ); } /** * Clear cache directory. * * @return bool */ public static function clearCache() { $cacheDir = \yii::getAlias('@webroot/' . self::$cacheAlias); self::removeDir($cacheDir); return @mkdir($cacheDir, 0755, true); } /** * @param $path */ protected static function removeDir($path) { if (is_file($path)) { @unlink($path); } else { array_map('self::removeDir', glob($path . DIRECTORY_SEPARATOR . '*')); @rmdir($path); } } }