-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageService.php
executable file
·152 lines (135 loc) · 4.14 KB
/
ImageService.php
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
<?php
namespace PYSys\Tools;
use Nette\InvalidArgumentException;
use Nette\Object;
use Nette\Utils\Image;
/**
* Class ImageService
* @package PYSys\Tools
*/
class ImageService extends Object
{
/** @var string */
protected $imagePath;
/** @var string */
protected $cachePath;
/** @var array */
protected $imageSizes;
/** @var string */
protected $cacheUrl;
/**
* @param string $imagePath
* @param string $cachePath
* @param array $imageSizes
* @param string $cacheUrl
*/
public function __construct($imagePath, $cachePath, $imageSizes, $cacheUrl) {
$this->imagePath = $imagePath;
$this->cachePath = $cachePath;
$this->imageSizes = $imageSizes;
$this->cacheUrl = $cacheUrl;
}
/**
* @param int $id
* @param string $filename
* @param string $type
* @return string
*/
public function getThumbnailPath($id, $filename, $type) {
return $this->cachePath . $this->getThumbnail($id, $filename, $type);
}
/**
* @param int $id
* @param string $filename
* @param string $type
* @return string
*/
public function getThumbnailUrl($id, $filename, $type) {
return $this->cacheUrl . $this->getThumbnail($id, $filename, $type);
}
/**
* @param int $id
* @param string $filename
* @param string $type
* @return string
* @throws \Nette\InvalidArgumentException
*/
protected function getThumbnail($id, $filename, $type) {
if(!array_key_exists($type,$this->imageSizes)) {
throw new InvalidArgumentException("Neznámý typ obrázku (Není nastaven v configu)");
}
$params = $this->imageSizes[$type];
$thumbnail = $this->getCachePath($id, $filename, $params["width"], $params["height"], $params["flag"]);
if(!file_exists($this->cachePath . $thumbnail)) {
$this->generate($id, $filename, $params["width"], $params["height"], $params["flag"]);
}
return $thumbnail;
}
/**
* @param $id
* @param $filename
* @param $width
* @param $height
* @param $flag
* @throws \Nette\InvalidArgumentException
*/
protected function generate($id, $filename, $width, $height, $flag) {
if(!file_exists($this->imagePath . $filename)) {
throw new InvalidArgumentException("Obrázek neexistuje");
}
$image = Image::fromFile($this->imagePath . $filename);
switch ($flag) {
case 'fit': $image->resize($width,$height,Image::FIT); break;
case 'exact': $image->resize($width,$height,Image::EXACT); break;
case 'stretch': $image->resize($width,$height,Image::STRETCH); break;
case 'shrink_only': $image->resize($width,$height,Image::SHRINK_ONLY); break;
case 'fill': $image->resize($width,$height,Image::FILL); break;
case 'centered':
$centered_image = $image->resize($width,$height,Image::SHRINK_ONLY);
$image = Image::fromBlank($width,$height, Image::rgb(255,255,255));
$image->place($centered_image,($width - $centered_image->getWidth())/2,($height - $centered_image->getHeight())/2);
break;
}
$thumbnail = $this->getCachePath($id, $filename, $width, $height, $flag);
list($d1, $d2) = explode('/', $thumbnail);
@mkdir( $this->cachePath . implode("/",array($d1, $d2)) . "/" , 0777, true);
$image->save($this->cachePath . $thumbnail);
}
/**
* @param int $id
* @param string $filename
* @param int|null $width
* @param int|null $height
* @param int|null $flag
* @return string
*/
protected function getCachePath($id, $filename, $width=null, $height=null, $flag=null) {
$cacheName = $this->getCacheName($id, $filename, $width, $height, $flag);
return substr($cacheName, 0, 2) . "/" . substr($cacheName, 2, 2) . "/" . $cacheName;
}
/**
* @param int $id
* @param string $filename
* @param int|null $width
* @param int|null $height
* @param int|null $flag
* @return string
*/
protected function getCacheName($id,$filename, $width=null, $height=null, $flag=null) {
$hash = md5( $id . $filename );
return substr($hash,0,12)
. '-' . $id
. (!empty($width) ? "-{$width}" : "")
. (!empty($height) ? "x{$height}" : "")
. (!empty($flag) ? "-{$flag}" : "")
. '.' . $this->getExtension($filename);
}
/**
* @param $filename
* @return string
*/
protected function getExtension($filename) {
$name_parts = explode(".",$filename);
return end($name_parts);
}
}