25 const JPEG_QUALITY = 75;
26 const WEBP_QUALITY = 75;
41 switch ($config->image_processor) {
43 if (extension_loaded(
'imagick')) {
44 $this->imagine = new \Imagine\Imagick\Imagine();
51 $this->imagine = new \Imagine\Gd\Imagine();
86 $destination = $destination ??
$source;
89 $resize_params = $this->normalizeResizeParameters($source,
$params);
91 $image = $this->imagine->open($source);
101 if ($x2 > $x1 && $y2 > $y1) {
102 $crop_start =
new Point($x1, $y1);
103 $crop_size =
new Box($x2 - $x1, $y2 - $y1);
104 $image->crop($crop_start, $crop_size);
107 $target_size =
new Box($max_width, $max_height);
108 $image->resize($target_size);
112 $thumbnail = $this->imagine->create(
$image->getSize(),
$image->palette()->color($background_color));
113 $thumbnail->paste(
$image,
new Point(0, 0));
115 if (pathinfo($destination, PATHINFO_EXTENSION) ===
'webp') {
121 'format' => $this->getFileFormat($source,
$params),
126 $thumbnail->save($destination,
$options);
130 }
catch (\Exception $ex) {
150 if (!isset(
$metadata[
'ifd0.Orientation'])) {
155 $autorotate =
new Autorotate();
161 }
catch (\Exception $ex) {
185 $image = $this->imagine->open($source);
187 $width =
$image->getSize()->getWidth();
188 $height =
$image->getSize()->getHeight();
192 if (!$max_height || !$max_width) {
204 $cropping_mode = $x1 || $y1 || $x2 || $y2;
206 if ($cropping_mode) {
207 $crop_width = $x2 - $x1;
208 $crop_height = $y2 - $y1;
209 if ($crop_width <= 0 || $crop_height <= 0 || $crop_width > $width || $crop_height > $height) {
210 throw new RangeException(
"Coordinates [$x1, $y1], [$x2, $y2] are invalid for image cropping");
214 $crop_width = $width;
215 $crop_height = $height;
223 $max_width = min($max_width, $max_height);
224 $max_height = $max_width;
227 $crop_width = min($crop_width, $crop_height);
228 $crop_height = $crop_width;
230 if (!$cropping_mode) {
232 $x1 = floor(($width - $crop_width) / 2);
233 $y1 = floor(($height - $crop_height) / 2);
237 if ($crop_height / $max_height > $crop_width / $max_width) {
238 $max_width = floor($max_height * $crop_width / $crop_height);
240 $max_height = floor($max_width * $crop_height / $crop_width);
244 if (!$upscale && ($crop_height < $max_height || $crop_width < $max_width)) {
246 $max_height = $crop_height;
247 $max_width = $crop_width;
255 'x2' => $x1 + $crop_width,
256 'y2' => $y1 + $crop_height,
258 'upscale' => $upscale,
273 $accepted_formats = [
274 'image/jpeg' =>
'jpeg',
275 'image/pjpeg' =>
'jpeg',
276 'image/png' =>
'png',
277 'image/x-png' =>
'png',
278 'image/gif' =>
'gif',
279 'image/vnd.wap.wbmp' =>
'wbmp',
280 'image/x‑xbitmap' =>
'xbm',
281 'image/x‑xbm' =>
'xbm',
286 if (in_array(
$format, $accepted_formats)) {
303 if ($this->config->webp_enabled ===
false) {
307 if ($this->imagine instanceof \Imagine\Imagick\Imagine) {
308 return !empty(\Imagick::queryformats(
'WEBP*'));
309 }
elseif ($this->imagine instanceof \Imagine\Gd\Imagine) {
310 return (
bool)
elgg_extract(
'WebP Support', gd_info(),
false);
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.
__construct(protected Config $config, protected MimeTypeService $mimetype)
Constructor.
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.
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
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.