ThumbnailImage.php
4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace common\helpers;
use yii\helpers\Html;
use yii\helpers\FileHelper;
use common\helpers\BaseImage as Image;
use Imagine\Image\Box;
use Imagine\Image\ManipulatorInterface;
/**
* 用来处理图片路径的统一方法
*
* Yii2 helper for creating and caching thumbnails on real time
* @author HimikLab
* @package himiklab\thumbnail
*/
class ThumbnailImage
{
const THUMBNAIL_OUTBOUND = ManipulatorInterface::THUMBNAIL_OUTBOUND;
const THUMBNAIL_INSET = ManipulatorInterface::THUMBNAIL_INSET;
public static $cacheAlias = 'thumbnails';
/** @var int $cacheExpire */
public static $cacheExpire = 0;
/**
* @param string $filename
* @param integer $width
* @param integer $height
* @param string $mode
* @return string
*/
public static function thumbnailFile($filename, $width, $height, $mode = self::THUMBNAIL_OUTBOUND)
{
$filename = FileHelper::normalizePath(\yii::getAlias($filename));
if (!is_file($filename)) {
return '';
}
$cachePath = \yii::getAlias('@webroot/' . self::$cacheAlias);
$thumbnailFileExt = strrchr($filename, '.');
$thumbnailFileName = md5($filename . $width . $height . $mode . filemtime($filename));
$thumbnailFilePath = $cachePath . DIRECTORY_SEPARATOR . substr($thumbnailFileName, 0, 2);
$thumbnailFile = $thumbnailFilePath . DIRECTORY_SEPARATOR . $thumbnailFileName . $thumbnailFileExt;
// 相对路径
$relativePath = self::$cacheAlias . '/' . substr($thumbnailFileName, 0, 2);
$relativelFile = $relativePath . DIRECTORY_SEPARATOR . $thumbnailFileName . $thumbnailFileExt;
if (file_exists($thumbnailFile)) {
if (self::$cacheExpire !== 0 && (time() - filemtime($thumbnailFile)) > 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 <img> 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);
}
}
}