1
|
|
|
<?php |
2
|
|
|
namespace library\images |
3
|
|
|
{ |
4
|
|
|
|
5
|
|
|
use library\images\methods\BoxCrop; |
6
|
|
|
use library\images\methods\Resize; |
7
|
|
|
use library\images\methods\SmartCrop; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ImageResizer |
11
|
|
|
* @package library\images |
12
|
|
|
*/ |
13
|
|
|
class ImageResizer |
14
|
|
|
{ |
15
|
|
|
protected $imageSet; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ImageResizer constructor. |
19
|
|
|
* |
20
|
|
|
* @param $imageSet |
21
|
|
|
*/ |
22
|
|
|
public function __construct($imageSet) |
23
|
|
|
{ |
24
|
|
|
$this->imageSet = $imageSet; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param $imagePath |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
|
|
public function applyImageSetToImage($imagePath) |
34
|
|
|
{ |
35
|
|
|
$returnFileNames = array(); |
36
|
|
|
$filename = ''; |
37
|
|
|
if (file_exists($imagePath)) { |
38
|
|
|
foreach ($this->imageSet as $set) { |
39
|
|
|
if ($set->method == 'resize') { |
40
|
|
|
$filename = $this->resize($imagePath, $set->width, $set->height); |
41
|
|
|
} elseif ($set->method == 'smartcrop') { |
42
|
|
|
$filename = $this->smartcrop($imagePath, $set->width, $set->height); |
43
|
|
|
} elseif ($set->method == 'boxcrop') { |
44
|
|
|
$filename = $this->boxcrop($imagePath, $set->width, $set->height); |
45
|
|
|
} |
46
|
|
|
$returnFileNames[$set->slug] = $filename; |
47
|
|
|
} |
48
|
|
|
return $returnFileNames; |
49
|
|
|
} else { |
50
|
|
|
throw new \Exception('Image doesnt exist: ' . $imagePath); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $imagePath |
56
|
|
|
* @param string $width |
57
|
|
|
* @param string $height |
58
|
|
|
* @return string |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
public function resize($imagePath='', $width='',$height='') |
62
|
|
|
{ |
63
|
|
|
$modifier = '-r' . $width . 'x' . $height; |
64
|
|
|
$destination = $this->modifyName($imagePath, $modifier); |
65
|
|
|
if (file_exists($imagePath)) { |
66
|
|
|
$image = new Image(); |
67
|
|
|
$image->LoadImage($imagePath); |
68
|
|
|
$resize = new Resize(); |
69
|
|
|
$resize->SetWidth($width); |
70
|
|
|
$resize->SetHeight($height); |
71
|
|
|
$resizedImageResource = $resize->Execute($image->GetImageResource()); |
72
|
|
|
$resizedImage = new Image(); |
73
|
|
|
$resizedImage->LoadImage($resizedImageResource); |
74
|
|
|
$resizedImage->SaveImage($destination, $resizedImage->GetImageMimeType($imagePath), 80); |
75
|
|
|
return basename($destination); |
76
|
|
|
} else { |
77
|
|
|
throw new \Exception('Image doesnt exist: ' . $imagePath); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $imagePath |
83
|
|
|
* @param string $width |
84
|
|
|
* @param string $height |
85
|
|
|
* @return string |
86
|
|
|
* @throws \Exception |
87
|
|
|
*/ |
88
|
|
|
public function smartcrop($imagePath='', $width='',$height='') |
89
|
|
|
{ |
90
|
|
|
$modifier = '-s' . $width . 'x' . $height; |
91
|
|
|
$destination = $this->modifyName($imagePath, $modifier); |
92
|
|
|
if (file_exists($imagePath)) { |
93
|
|
|
$image = new Image(); |
94
|
|
|
$image->LoadImage($imagePath); |
95
|
|
|
$resize = new SmartCrop(); |
96
|
|
|
$resize->SetWidth($width); |
97
|
|
|
$resize->SetHeight($height); |
98
|
|
|
$resizedImageResource = $resize->Execute($image->GetImageResource()); |
99
|
|
|
$resizedImage = new Image(); |
100
|
|
|
$resizedImage->LoadImage($resizedImageResource); |
101
|
|
|
$resizedImage->SaveImage($destination, $resizedImage->GetImageMimeType($imagePath), 80); |
102
|
|
|
return basename($destination); |
103
|
|
|
} else { |
104
|
|
|
throw new \Exception('Image doesnt exist: ' . $imagePath); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $imagePath |
110
|
|
|
* @param string $width |
111
|
|
|
* @param string $height |
112
|
|
|
* @return string |
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
|
|
public function boxcrop($imagePath='', $width='',$height='') |
116
|
|
|
{ |
117
|
|
|
$modifier = '-b' . $width . 'x' . $height; |
118
|
|
|
$destination = $this->modifyName($imagePath, $modifier); |
119
|
|
|
if (file_exists($imagePath)) { |
120
|
|
|
$image = new Image(); |
121
|
|
|
$image->LoadImage($imagePath); |
122
|
|
|
$resize = new BoxCrop(); |
123
|
|
|
$resize->SetWidth($width); |
124
|
|
|
$resize->SetHeight($height); |
125
|
|
|
$resizedImageResource = $resize->Execute($image->GetImageResource()); |
126
|
|
|
$resizedImage = new Image(); |
127
|
|
|
$resizedImage->LoadImage($resizedImageResource); |
128
|
|
|
$resizedImage->SaveImage($destination, $resizedImage->GetImageMimeType($imagePath), 80); |
129
|
|
|
return basename($destination); |
130
|
|
|
} else { |
131
|
|
|
throw new \Exception('Image doesnt exist: ' . $imagePath); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $imagePath |
137
|
|
|
* @param string $modifier |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
private function modifyName($imagePath, $modifier='') |
142
|
|
|
{ |
143
|
|
|
$filename = basename($imagePath); |
144
|
|
|
$path = dirname($imagePath); |
145
|
|
|
$fileParts = explode('.', $filename); |
146
|
|
|
if (count($fileParts) > 1) { |
147
|
|
|
$extension = end($fileParts); |
148
|
|
|
array_pop($fileParts); |
149
|
|
|
$fileNameWithoutExtension = implode('-', $fileParts); |
150
|
|
|
$fileNameWithoutExtension = slugify($fileNameWithoutExtension); |
151
|
|
|
$filename = $fileNameWithoutExtension . $modifier . '.' . $extension; |
152
|
|
|
} else { |
153
|
|
|
$filename = slugify($filename); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (file_exists($path . '/' . $filename)) { |
157
|
|
|
$fileParts = explode('.', $filename); |
158
|
|
|
if (count($fileParts) > 1) { |
159
|
|
|
$extension = end($fileParts); |
160
|
|
|
array_pop($fileParts); |
161
|
|
|
$fileNameWithoutExtension = implode('-', $fileParts); |
162
|
|
|
$fileNameWithoutExtension .= '-copy'; |
163
|
|
|
$filename = $fileNameWithoutExtension . '.' . $extension; |
164
|
|
|
} else { |
165
|
|
|
$filename .= '-copy'; |
166
|
|
|
} |
167
|
|
|
return $this->modifyName($path . '/' . $filename); |
168
|
|
|
} |
169
|
|
|
return $path . '/' . $filename; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |