d8c1a2e0
Yarik
Big commit artbox
|
1
|
<?php
|
9532b442
Yarik
"current" functio...
|
2
3
4
5
6
|
namespace common\components\artboximage;
use Yii;
use yii\base\Object;
|
9532b442
Yarik
"current" functio...
|
7
8
9
10
11
|
use yii\helpers\Html;
class ArtboxImageHelper extends Object
{
|
c70f24ea
Yarik
For Leha commit.
|
12
13
14
|
/**
* @var ArtboxImage $imageDriver
*/
|
9532b442
Yarik
"current" functio...
|
15
16
|
private static $imageDriver;
|
c70f24ea
Yarik
For Leha commit.
|
17
18
19
|
/**
* @var array $presets
*/
|
9532b442
Yarik
"current" functio...
|
20
21
|
private static $presets;
|
c70f24ea
Yarik
For Leha commit.
|
22
23
24
25
26
|
/**
* Get image manipulation driver
*
* @return \common\components\artboximage\ArtboxImage
*/
|
9532b442
Yarik
"current" functio...
|
27
28
|
public static function getDriver()
{
|
c70f24ea
Yarik
For Leha commit.
|
29
30
|
if (empty( self::$imageDriver )) {
self::$imageDriver = Yii::$app->get('artboximage');
|
9532b442
Yarik
"current" functio...
|
31
32
|
}
return self::$imageDriver;
|
d8c1a2e0
Yarik
Big commit artbox
|
33
|
}
|
9532b442
Yarik
"current" functio...
|
34
|
|
c70f24ea
Yarik
For Leha commit.
|
35
36
37
38
39
40
41
|
/**
* Get named preset from driver preset list.
*
* @param string $preset
*
* @return array|null
*/
|
9532b442
Yarik
"current" functio...
|
42
43
|
public static function getPreset($preset)
{
|
c70f24ea
Yarik
For Leha commit.
|
44
|
if (empty( self::$presets )) {
|
9532b442
Yarik
"current" functio...
|
45
46
|
self::$presets = self::getDriver()->presets;
}
|
c70f24ea
Yarik
For Leha commit.
|
47
|
return empty( self::$presets[ $preset ] ) ? null : self::$presets[ $preset ];
|
d8c1a2e0
Yarik
Big commit artbox
|
48
|
}
|
9532b442
Yarik
"current" functio...
|
49
|
|
c70f24ea
Yarik
For Leha commit.
|
50
51
52
53
54
55
56
57
58
59
|
/**
* Get image HTML for image
*
* @param string $file
* @param array|string $preset
* @param array $imgOptions
*
* @see Html::img()
* @return string
*/
|
9532b442
Yarik
"current" functio...
|
60
61
|
public static function getImage($file, $preset, $imgOptions = [])
{
|
c70f24ea
Yarik
For Leha commit.
|
62
|
$preset_alias = is_array($preset) ? array_keys($preset)[ 0 ] : null;
|
9532b442
Yarik
"current" functio...
|
63
|
return Html::img(self::getImageSrc($file, $preset, $preset_alias), $imgOptions);
|
d8c1a2e0
Yarik
Big commit artbox
|
64
|
}
|
9532b442
Yarik
"current" functio...
|
65
|
|
c70f24ea
Yarik
For Leha commit.
|
66
67
68
69
70
71
72
73
74
75
|
/**
* Get src for image
*
* @param string $file
* @param string $preset
* @param null|string $preset_alias
*
* @return bool|string
*/
public static function getImageSrc($file, $preset, $preset_alias = null)
|
9532b442
Yarik
"current" functio...
|
76
|
{
|
c70f24ea
Yarik
For Leha commit.
|
77
|
if (is_string($preset)) {
|
9532b442
Yarik
"current" functio...
|
78
79
80
|
$preset_alias = $preset;
$preset = self::getPreset($preset);
}
|
c70f24ea
Yarik
For Leha commit.
|
81
|
if (empty( $preset ) || empty( $preset_alias )) {
|
9532b442
Yarik
"current" functio...
|
82
83
84
85
|
return $file;
}
$filePath = self::getPathFromUrl($file);
|
c70f24ea
Yarik
For Leha commit.
|
86
87
88
89
90
91
|
if (!file_exists($filePath) || !preg_match(
'#^(.*)\.(' . self::getExtensionsRegexp() . ')$#',
$file,
$matches
)
) {
|
9532b442
Yarik
"current" functio...
|
92
93
94
|
return $file;
}
return self::getPresetUrl($filePath, $preset, $preset_alias);
|
d8c1a2e0
Yarik
Big commit artbox
|
95
|
}
|
9532b442
Yarik
"current" functio...
|
96
|
|
c70f24ea
Yarik
For Leha commit.
|
97
98
99
100
101
102
103
|
/**
* Replace web path with file path
*
* @param string $url
*
* @return string
*/
|
9532b442
Yarik
"current" functio...
|
104
105
106
|
private static function getPathFromUrl($url)
{
return substr_replace($url, self::getDriver()->rootPath, 0, strlen(self::getDriver()->rootUrl));
|
d8c1a2e0
Yarik
Big commit artbox
|
107
|
}
|
9532b442
Yarik
"current" functio...
|
108
|
|
c70f24ea
Yarik
For Leha commit.
|
109
110
111
112
113
114
115
|
/**
* Replace file path with web path
*
* @param string $path
*
* @return string
*/
|
9532b442
Yarik
"current" functio...
|
116
117
118
|
private static function getUrlFromPath($path)
{
return substr_replace($path, self::getDriver()->rootUrl, 0, strlen(self::getDriver()->rootPath));
|
d8c1a2e0
Yarik
Big commit artbox
|
119
|
}
|
9532b442
Yarik
"current" functio...
|
120
|
|
c70f24ea
Yarik
For Leha commit.
|
121
122
123
124
125
126
127
128
129
|
/**
* Get formatted file url or create it if not exist
*
* @param string $filePath
* @param array $preset
* @param string $preset_alias
*
* @return bool|string
*/
|
9532b442
Yarik
"current" functio...
|
130
131
132
133
134
135
|
private static function getPresetUrl($filePath, $preset, $preset_alias)
{
$pathinfo = pathinfo($filePath);
$presetPath = $pathinfo[ 'dirname' ] . '/styles/' . strtolower($preset_alias);
$presetFilePath = $presetPath . '/' . $pathinfo[ 'basename' ];
$presetUrl = self::getUrlFromPath($presetFilePath);
|
c70f24ea
Yarik
For Leha commit.
|
136
|
if (file_exists($presetFilePath)) {
|
9532b442
Yarik
"current" functio...
|
137
138
|
return $presetUrl;
}
|
c70f24ea
Yarik
For Leha commit.
|
139
|
if (!file_exists($presetPath)) {
|
9532b442
Yarik
"current" functio...
|
140
141
142
|
@mkdir($presetPath, 0777, true);
}
$output = self::createPresetImage($filePath, $preset, $preset_alias);
|
c70f24ea
Yarik
For Leha commit.
|
143
|
if (!empty( $output )) {
|
9532b442
Yarik
"current" functio...
|
144
145
146
147
148
149
|
$f = fopen($presetFilePath, 'w');
fwrite($f, $output);
fclose($f);
return $presetUrl;
}
return false;
|
d8c1a2e0
Yarik
Big commit artbox
|
150
|
}
|
9532b442
Yarik
"current" functio...
|
151
|
|
c70f24ea
Yarik
For Leha commit.
|
152
153
154
155
156
157
158
159
160
161
162
163
|
/**
* Create formatted image.
* Available manipulations:
* * resize
* * flip
*
* @param string $filePath
* @param array $preset
* @param string $preset_alias
*
* @return string
*/
|
9532b442
Yarik
"current" functio...
|
164
165
166
167
|
private static function createPresetImage($filePath, $preset, $preset_alias)
{
$image = self::getDriver()
->load($filePath);
|
c70f24ea
Yarik
For Leha commit.
|
168
169
|
foreach ($preset as $action => $data) {
switch ($action) {
|
9532b442
Yarik
"current" functio...
|
170
|
case 'resize':
|
c70f24ea
Yarik
For Leha commit.
|
171
172
173
|
$width = empty( $data[ 'width' ] ) ? null : $data[ 'width' ];
$height = empty( $data[ 'height' ] ) ? null : $data[ 'height' ];
$master = empty( $data[ 'master' ] ) ? null : $data[ 'master' ];
|
9532b442
Yarik
"current" functio...
|
174
175
176
177
178
179
180
181
|
$image->resize($width, $height, $master);
break;
case 'flip':
$image->flip(@$data[ 'direction' ]);
break;
default:
break;
}
|
d8c1a2e0
Yarik
Big commit artbox
|
182
|
}
|
9532b442
Yarik
"current" functio...
|
183
|
return $image->render();
|
d8c1a2e0
Yarik
Big commit artbox
|
184
|
}
|
9532b442
Yarik
"current" functio...
|
185
186
187
|
/**
* Get extensions regexp
|
c70f24ea
Yarik
For Leha commit.
|
188
|
*
|
9532b442
Yarik
"current" functio...
|
189
190
191
192
193
194
195
|
* @return string regexp
*/
private static function getExtensionsRegexp()
{
$keys = array_keys(self::getDriver()->extensions);
return '(?i)' . join('|', $keys);
}
|
9532b442
Yarik
"current" functio...
|
196
|
}
|