Blame view

backend/components/noam148/imageresize/ImageResize.php 4.77 KB
a8aa0f7d   alex   Add new noam148 m...
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  <?php
  
  namespace backend\components\noam148\imageresize;
  
  use Yii;
  use yii\helpers\Url;
  use yii\helpers\FileHelper;
  use yii\imagine\Image;
  use yii\base\Exception;
  use Imagine\Image\Box;
  use Imagine\Image\ManipulatorInterface;
  
  class ImageResize
  {
  
  	const IMAGE_OUTBOUND = ManipulatorInterface::THUMBNAIL_OUTBOUND;
  	const IMAGE_INSET = ManipulatorInterface::THUMBNAIL_INSET;
  
  	/** @var string $cachePath path alias relative with webroot where the cache files are kept */
  	public $cachePath = 'assets/images';
  
  	/** @var int $cacheExpire */
  	public $cacheExpire = 0;
  
  	/** @var int $imageQuality */
  	public $imageQuality = 50;
  
  	/** @var int $useFilename if true show filename in url */
  	public $useFilename = true;
  
  	/** @var int $absoluteUrl if true include domain in url */
  	public $absoluteUrl = false;
  
  	/**
  	 * Creates and caches the image thumbnail and returns full path from thumbnail file.
  	 *
  	 * @param string $filePath to original file
  	 * @param integer $width
  	 * @param integer $height
  	 * @param string $mode
  	 * @param integer $quality (1 - 100 quality)
  	 * @param string $chosenFileName (custome filename)
  	 * @return string
  	 * @throws Exception
  	 */
  	public function generateImage($filePath, $width, $height, $mode = "outbound", $quality = null, $chosenFileName = null)
  	{
  		$filePath = FileHelper::normalizePath(Yii::getAlias($filePath));
  		if (!is_file($filePath)) {
  			throw new Exception("File $filePath doesn't exist");
  		}
  
  		//set resize mode
  		$resizeMode = null;
  		switch ($mode) {
  			case "outbound":
  				$resizeMode = ImageResize::IMAGE_OUTBOUND;
  				break;
  			case "inset":
  				$resizeMode = ImageResize::IMAGE_INSET;
  				break;
  			default:
  				throw new Exception('generateImage $mode is not valid choose for "outbound" or "inset"');
  		}
  
  		//create some vars
  		$cachePath = Yii::getAlias('@webroot/' . $this->cachePath);
  		//get fileinfo
  		$aFileInfo = pathinfo($filePath);
  		//set default filename
  		$sFileHash = md5($filePath . $width . $height . $resizeMode . filemtime($filePath));
  		$imageFileName = null;
  		//if $this->useFilename set to true? use seo friendly name
  		if ($this->useFilename === true) {
  			//set hash and default name
  			$sFileHashShort = substr($sFileHash, 0, 6);
  			$sFileName = $aFileInfo['filename'];
  			//set choosen filename if $chosenFileName not null.
  			if ($chosenFileName !== null) {
  				$sFileName = preg_replace('/(\.\w+)$/', '', $chosenFileName);
  			}
  			//replace for seo friendly file name
  			$sFilenameReplace = preg_replace("/[^\w\.\-]+/", '-', $sFileName);
  			//set filename
  			$imageFileName = $sFileHashShort . "_" . $sFilenameReplace;
  			//else use file hash as filename	
  		} else {
  			$imageFileName = $sFileHash;
  		}
  
  		$imageFileExt = "." . $aFileInfo['extension'];
  		$imageFilePath = $cachePath . DIRECTORY_SEPARATOR . substr($imageFileName, 0, 2);
  		$imageFile = $imageFilePath . DIRECTORY_SEPARATOR . $imageFileName . $imageFileExt;
  
  		if (file_exists($imageFile)) {
  			if ($this->cacheExpire !== 0 && (time() - filemtime($imageFile)) > $this->cacheExpire) {
  				unlink($imageFile);
  			} else {
  				return $imageFile;
  			}
  		}
  		//if dir not exist create cache edir
  		if (!is_dir($imageFilePath)) {
  			FileHelper::createDirectory($imageFilePath, 0755);
  		}
  		//create image
  		$box = new Box($width, $height);
  		$image = Image::getImagine()->open($filePath);
  		$image = $image->thumbnail($box, $resizeMode);
  
  		$options = [
  			'quality' => $quality === null ? $this->imageQuality : $quality
  		];
  		$image->save($imageFile, $options);
  		return $imageFile;
  	}
  
  	/**
  	 * Creates and caches the image and returns URL from resized file.
  	 *
  	 * @param string $filePath to original file
  	 * @param integer $width
  	 * @param integer $height
  	 * @param string $mode
  	 * @param integer $quality (1 - 100 quality)
  	 * @param string $fileName (custome filename)
  	 * @return string
  	 */
  	public function getUrl($filePath, $width, $height, $mode = "outbound", $quality = null, $fileName = null)
  	{
  		//get original file 
  		$normalizePath = FileHelper::normalizePath(Yii::getAlias($filePath));
  		//get cache url
  		$cacheUrl = Yii::getAlias($this->cachePath);
  		//generate file
  		$resizedFilePath = self::generateImage($normalizePath, $width, $height, $mode, $quality, $fileName);
  		//get resized file
  		$normalizeResizedFilePath = FileHelper::normalizePath($resizedFilePath);
  		$resizedFileName = pathinfo($normalizeResizedFilePath, PATHINFO_BASENAME);
  		//get url
  		$sFileUrl = Url::to('@web/' . $cacheUrl . '/' . substr($resizedFileName, 0, 2) . '/' . $resizedFileName, $this->absoluteUrl);
  		//return path
  		return $sFileUrl;
  	}
  
  	/**
  	 * Clear cache directory.
  	 *
  	 * @return bool
  	 */
  	public function clearCache()
  	{
  		$cachePath = Yii::getAlias('@webroot/' . $this->cachePath);
  		//remove dir
  		FileHelper::removeDirectory($cachePath);
  		//creat dir
  		return FileHelper::createDirectory($cachePath, 0755);
  	}
  
  }