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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205<?php
namespace Directus\Filesystem;
class Thumbnail
{
/**
* Thumbnail default format
*
* @var string
*/
private static $defaultFormat = 'jpeg';
private static $imageFormatsSupported = [
'jpg',
'jpeg',
'gif',
'png',
];
/**
* List of non-image supported extension/formats
*
* @var array
*/
private static $nonImageFormatsSupported = [
'svg',
'pdf',
'psd',
'tif',
'tiff',
];
public static function generateThumbnail($targetContent, $format, $thumbnailSize, $cropEnabled)
{
if (static::isNonImageFormatSupported($format)) {
$format = static::defaultFormat();
$targetContent = static::createImageFromNonImage($targetContent, $format);
}
if (!in_array(strtolower($format), static::$imageFormatsSupported)) {
return false;
}
if (!$targetContent) {
return false;
}
$img = imagecreatefromstring($targetContent);
if ($img === false) {
return false;
}
$w = imagesx($img);
$h = imagesy($img);
$x1 = 0; // used for crops
$y1 = 0; // used for crops
$aspectRatio = $w / $h;
if ($cropEnabled) {
// crop to center of image
if ($aspectRatio <= 1) {
$newW = $thumbnailSize;
$newH = $h * ($thumbnailSize / $w);
$y1 = -1 * (($newH - $thumbnailSize) / 2);
} else {
$newH = $thumbnailSize;
$newW = $w * ($thumbnailSize / $h);
$x1 = -1 * (($newW - $thumbnailSize) / 2);
}
} else {
// portrait (or square) mode, maximize height
if ($aspectRatio <= 1) {
$newH = $thumbnailSize;
$newW = $thumbnailSize * $aspectRatio;
}
// landscape mode, maximize width
if ($aspectRatio > 1) {
$newW = $thumbnailSize;
$newH = $thumbnailSize / $aspectRatio;
}
}
if ($cropEnabled) {
$imgResized = imagecreatetruecolor($thumbnailSize, $thumbnailSize);
} else {
$imgResized = imagecreatetruecolor($newW, $newH);
}
// Preserve transperancy for gifs and pngs
if ($format == 'gif' || $format == 'png') {
imagealphablending($imgResized, false);
imagesavealpha($imgResized, true);
$transparent = imagecolorallocatealpha($imgResized, 255, 255, 255, 127);
imagefilledrectangle($imgResized, 0, 0, $newW, $newH, $transparent);
}
imagecopyresampled($imgResized, $img, $x1, $y1, 0, 0, $newW, $newH, $w, $h);
imagedestroy($img);
return $imgResized;
}
/**
* Create a image from a non image file content. (Ex. PDF, PSD or TIFF)
*
* @param $content
* @param string $format
*
* @return bool|string
*/
public static function createImageFromNonImage($content, $format = 'jpeg')
{
if (!extension_loaded('imagick')) {
return false;
}
$image = new \Imagick();
$image->readImageBlob($content);
$image->setIteratorIndex(0);
$image->setImageFormat($format);
return $image->getImageBlob();
}
public static function writeImage($extension, $path, $img, $quality)
{
ob_start();
// force $path to be NULL to dump writeImage on the stream
$path = NULL;
switch (strtolower($extension)) {
case 'jpg':
case 'jpeg':
imagejpeg($img, $path, $quality);
break;
case 'gif':
imagegif($img, $path);
break;
case 'png':
imagepng($img, $path);
break;
case 'pdf':
case 'psd':
case 'tif':
case 'tiff':
imagejpeg($img, $path, $quality);
break;
}
return ob_get_clean();
}
/**
* Gets the default thumbnail format
*
* @return string
*/
public static function defaultFormat()
{
return static::$defaultFormat;
}
/**
* Gets supported formats
*
* @return array
*/
public static function getFormatsSupported()
{
return array_merge(static::getImageFormatSupported(), static::getNonImageFormatSupported());
}
/**
* Gets image supported formats
*
* @return array
*/
public static function getImageFormatSupported()
{
return Thumbnail::$imageFormatsSupported;
}
/**
* Gets non-image supported formats
*
* @return array
*/
public static function getNonImageFormatSupported()
{
return static::$nonImageFormatsSupported;
}
/**
* If a given format/extension is a non-image supported to generate thumbnail
*
* @param $format
*
* @return bool
*/
public static function isNonImageFormatSupported($format)
{
return in_array(strtolower($format), static::$nonImageFormatsSupported);
}
}