25 const JPEG_QUALITY = 75;
26 const WEBP_QUALITY = 75;
51 switch ($config->image_processor) {
53 if (extension_loaded(
'imagick')) {
54 $this->imagine = new \Imagine\Imagick\Imagine();
61 $this->imagine = new \Imagine\Gd\Imagine();
66 $this->mimetype = $mimetype;
99 $destination = $destination ??
$source;
102 $resize_params = $this->normalizeResizeParameters($source,
$params);
104 $image = $this->imagine->open($source);
114 if ($x2 > $x1 && $y2 > $y1) {
115 $crop_start =
new Point($x1, $y1);
116 $crop_size =
new Box($x2 - $x1, $y2 - $y1);
117 $image->crop($crop_start, $crop_size);
120 $target_size =
new Box($max_width, $max_height);
121 $image->resize($target_size);
125 $thumbnail = $this->imagine->create(
$image->getSize(),
$image->palette()->color($background_color));
126 $thumbnail->paste(
$image,
new Point(0, 0));
128 if (pathinfo($destination, PATHINFO_EXTENSION) ===
'webp') {
134 'format' => $this->getFileFormat($source,
$params),
139 $thumbnail->save($destination,
$options);
163 if (!isset(
$metadata[
'ifd0.Orientation'])) {
168 $autorotate =
new Autorotate();
198 $image = $this->imagine->open($source);
200 $width =
$image->getSize()->getWidth();
201 $height =
$image->getSize()->getHeight();
205 if (!$max_height || !$max_width) {
217 $cropping_mode = $x1 || $y1 || $x2 || $y2;
219 if ($cropping_mode) {
220 $crop_width = $x2 - $x1;
221 $crop_height = $y2 - $y1;
222 if ($crop_width <= 0 || $crop_height <= 0 || $crop_width > $width || $crop_height > $height) {
223 throw new RangeException(
"Coordinates [$x1, $y1], [$x2, $y2] are invalid for image cropping");
227 $crop_width = $width;
228 $crop_height = $height;
236 $max_width = min($max_width, $max_height);
237 $max_height = $max_width;
240 $crop_width = min($crop_width, $crop_height);
241 $crop_height = $crop_width;
243 if (!$cropping_mode) {
245 $x1 = floor(($width - $crop_width) / 2);
246 $y1 = floor(($height - $crop_height) / 2);
250 if ($crop_height / $max_height > $crop_width / $max_width) {
251 $max_width = floor($max_height * $crop_width / $crop_height);
253 $max_height = floor($max_width * $crop_height / $crop_width);
257 if (!$upscale && ($crop_height < $max_height || $crop_width < $max_width)) {
259 $max_height = $crop_height;
260 $max_width = $crop_width;
268 'x2' => $x1 + $crop_width,
269 'y2' => $y1 + $crop_height,
271 'upscale' => $upscale,
286 $accepted_formats = [
287 'image/jpeg' =>
'jpeg',
288 'image/pjpeg' =>
'jpeg',
289 'image/png' =>
'png',
290 'image/x-png' =>
'png',
291 'image/gif' =>
'gif',
292 'image/vnd.wap.wbmp' =>
'wbmp',
293 'image/x‑xbitmap' =>
'xbm',
294 'image/x‑xbm' =>
'xbm',
299 if (in_array(
$format, $accepted_formats)) {
316 if ($this->config->webp_enabled ===
false) {
320 if ($this->imagine instanceof \Imagine\Imagick\Imagine) {
321 return !empty(\Imagick::queryformats(
'WEBP*'));
322 }
elseif ($this->imagine instanceof \Imagine\Gd\Imagine) {
323 return (
bool)
elgg_extract(
'WebP Support', gd_info(),
false);
__construct(Config $config, MimeTypeService $mimetype)
Constructor.
Exception thrown to indicate range errors during program execution.
normalizeResizeParameters(string $source, array $params=[])
Calculate the parameters for resizing an image.
Exception thrown if an argument is not of the expected type.
$params
Saves global plugin settings.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
$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.
resize(string $source, string $destination=null, array $params=[])
Crop and resize an image.
Image manipulation service.
getFileFormat($filename, $params)
Determine the image file format, this is needed for correct resizing.
getLogger()
Returns logger.
hasWebPSupport()
Checks if imagine has WebP support.
Public service related to MIME type detection.