23 const JPEG_QUALITY = 75;
24 const WEBP_QUALITY = 75;
49 switch ($config->image_processor) {
51 if (extension_loaded(
'imagick')) {
52 $this->imagine = new \Imagine\Imagick\Imagine();
57 $this->imagine = new \Imagine\Gd\Imagine();
62 $this->mimetype = $mimetype;
95 if (!isset($destination)) {
102 $width =
$image->getSize()->getWidth();
103 $height =
$image->getSize()->getHeight();
105 $resize_params = $this->normalizeResizeParameters($width, $height,
$params);
115 if ($x2 > $x1 && $y2 > $y1) {
116 $crop_start =
new Point($x1, $y1);
117 $crop_size =
new Box($x2 - $x1, $y2 - $y1);
118 $image->crop($crop_start, $crop_size);
121 $target_size =
new Box($max_width, $max_height);
122 $thumbnail =
$image->resize($target_size);
124 if (pathinfo($destination, PATHINFO_EXTENSION) ===
'webp') {
135 $thumbnail->save($destination,
$options);
159 if (!isset(
$metadata[
'ifd0.Orientation'])) {
164 $autorotate =
new Autorotate();
196 if (!$max_height || !$max_width) {
197 throw new \LogicException(
"Resize width and height parameters are required");
208 $cropping_mode = $x1 || $y1 || $x2 || $y2;
210 if ($cropping_mode) {
211 $crop_width = $x2 - $x1;
212 $crop_height = $y2 - $y1;
213 if ($crop_width <= 0 || $crop_height <= 0 || $crop_width > $width || $crop_height > $height) {
214 throw new \LogicException(
"Coordinates [$x1, $y1], [$x2, $y2] are invalid for image cropping");
218 $crop_width = $width;
219 $crop_height = $height;
227 $max_width = $max_height = min($max_width, $max_height);
230 $crop_width = $crop_height = min($crop_width, $crop_height);
232 if (!$cropping_mode) {
234 $x1 = floor(($width - $crop_width) / 2);
235 $y1 = floor(($height - $crop_height) / 2);
239 if ($crop_height / $max_height > $crop_width / $max_width) {
240 $max_width = floor($max_height * $crop_width / $crop_height);
242 $max_height = floor($max_width * $crop_height / $crop_width);
246 if (!$upscale && ($crop_height < $max_height || $crop_width < $max_width)) {
248 $max_height = $crop_height;
249 $max_width = $crop_width;
257 'x2' => $x1 + $crop_width,
258 'y2' => $y1 + $crop_height,
260 'upscale' => $upscale,
275 $accepted_formats = [
276 'image/jpeg' =>
'jpeg',
277 'image/pjpeg' =>
'jpeg',
278 'image/png' =>
'png',
279 'image/x-png' =>
'png',
280 'image/gif' =>
'gif',
281 'image/vnd.wap.wbmp' =>
'wbmp',
282 'image/x‑xbitmap' =>
'xbm',
283 'image/x‑xbm' =>
'xbm',
288 if (in_array(
$format, $accepted_formats)) {
305 if ($this->config->webp_enabled ===
false) {
309 if ($this->imagine instanceof \Imagine\Imagick\Imagine) {
310 return !empty(\Imagick::queryformats(
'WEBP*'));
311 }
elseif ($this->imagine instanceof \Imagine\Gd\Imagine) {
312 return (
bool)
elgg_extract(
'WebP Support', gd_info(),
false);
__construct(Config $config, MimeTypeService $mimetype)
Constructor.
Base exception of invalid argument exceptions in the Elgg system.
$params
Saves global plugin settings.
$config
Advanced site settings, debugging section.
fixOrientation($filename)
If needed the image will be rotated based on orientation information.
trait Loggable
Enables adding a logger.
Image manipulation service.
getFileFormat($filename, $params)
Determine the image file format, this is needed for correct resizing.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
getLogger()
Returns logger.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
hasWebPSupport()
Checks if imagine has WebP support.
resize($source, $destination=null, array $params=[])
Crop and resize an image.
Public service related to MIME type detection.
normalizeResizeParameters($width, $height, array $params=[])
Calculate the parameters for resizing an image.