8 use Elgg\Traits\Loggable;
9 use Imagine\Filter\Basic\Autorotate;
10 use Imagine\Image\Box;
11 use Imagine\Image\ImagineInterface;
12 use Imagine\Image\Point;
24 const JPEG_QUALITY = 75;
25 const WEBP_QUALITY = 75;
40 switch (
$config->image_processor) {
42 if (extension_loaded(
'imagick')) {
43 $this->imagine = new \Imagine\Imagick\Imagine();
50 $this->imagine = new \Imagine\Gd\Imagine();
85 $destination = $destination ??
$source;
88 $resize_params = $this->normalizeResizeParameters(
$source,
$params);
100 if ($x2 > $x1 && $y2 > $y1) {
101 $crop_start =
new Point($x1, $y1);
102 $crop_size =
new Box($x2 - $x1, $y2 - $y1);
103 $image->crop($crop_start, $crop_size);
106 $target_size =
new Box($max_width, $max_height);
107 $image->resize($target_size);
111 $thumbnail = $this->imagine->create(
$image->getSize(),
$image->palette()->color($background_color));
112 $thumbnail->paste(
$image,
new Point(0, 0));
114 if (pathinfo($destination, PATHINFO_EXTENSION) ===
'webp') {
125 $thumbnail->save($destination,
$options);
129 }
catch (\Exception $ex) {
130 $this->getLogger()->error($ex);
149 if (!isset(
$metadata[
'ifd0.Orientation'])) {
154 $autorotate =
new Autorotate();
160 }
catch (\Exception $ex) {
161 $this->getLogger()->notice($ex);
186 $width =
$image->getSize()->getWidth();
187 $height =
$image->getSize()->getHeight();
191 if (!$max_height || !$max_width) {
203 $cropping_mode = $x1 || $y1 || $x2 || $y2;
205 if ($cropping_mode) {
206 $crop_width = $x2 - $x1;
207 $crop_height = $y2 - $y1;
208 if ($crop_width <= 0 || $crop_height <= 0 || $crop_width > $width || $crop_height > $height) {
209 throw new RangeException(
"Coordinates [$x1, $y1], [$x2, $y2] are invalid for image cropping");
213 $crop_width = $width;
214 $crop_height = $height;
222 $max_width = min($max_width, $max_height);
223 $max_height = $max_width;
226 $crop_width = min($crop_width, $crop_height);
227 $crop_height = $crop_width;
229 if (!$cropping_mode) {
231 $x1 = floor(($width - $crop_width) / 2);
232 $y1 = floor(($height - $crop_height) / 2);
236 if ($crop_height / $max_height > $crop_width / $max_width) {
237 $max_width = floor($max_height * $crop_width / $crop_height);
239 $max_height = floor($max_width * $crop_height / $crop_width);
243 if (!$upscale && ($crop_height < $max_height || $crop_width < $max_width)) {
245 $max_height = $crop_height;
246 $max_width = $crop_width;
254 'x2' => $x1 + $crop_width,
255 'y2' => $y1 + $crop_height,
257 'upscale' => $upscale,
272 $accepted_formats = [
273 'image/jpeg' =>
'jpeg',
274 'image/pjpeg' =>
'jpeg',
275 'image/png' =>
'png',
276 'image/x-png' =>
'png',
277 'image/gif' =>
'gif',
278 'image/vnd.wap.wbmp' =>
'wbmp',
279 'image/x‑xbitmap' =>
'xbm',
280 'image/x‑xbm' =>
'xbm',
285 if (in_array(
$format, $accepted_formats)) {
292 $this->getLogger()->warning($e);
302 if ($this->config->webp_enabled === false) {
306 if ($this->imagine instanceof \Imagine\Imagick\Imagine) {
307 return !empty(\Imagick::queryformats(
'WEBP*'));
308 }
elseif ($this->imagine instanceof \Imagine\Gd\Imagine) {
309 return (
bool)
elgg_extract(
'WebP Support', gd_info(),
false);
$params
Saves global plugin settings.
Exception thrown if an argument is not of the expected type.
Exception thrown to indicate range errors during program execution.
Public service related to MIME type detection.
Image manipulation service.
normalizeResizeParameters(string $source, array $params=[])
Calculate the parameters for resizing an image.
__construct(protected Config $config, protected MimeTypeService $mimetype)
Constructor.
getFileFormat($filename, $params)
Determine the image file format, this is needed for correct resizing.
hasWebPSupport()
Checks if imagine has WebP support.
fixOrientation($filename)
If needed the image will be rotated based on orientation information.
resize(string $source, ?string $destination=null, array $params=[])
Crop and resize an image.
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
$config
Advanced site settings, debugging section.
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.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.