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);
147 $this->assertValidImageDimensions(
$filename);
151 if (!isset(
$metadata[
'ifd0.Orientation'])) {
156 $autorotate =
new Autorotate();
162 }
catch (\Exception $ex) {
163 $this->getLogger()->notice($ex);
185 $this->assertValidImageDimensions(
$source);
189 $width =
$image->getSize()->getWidth();
190 $height =
$image->getSize()->getHeight();
194 if (!$max_height || !$max_width) {
206 $cropping_mode = $x1 || $y1 || $x2 || $y2;
208 if ($cropping_mode) {
209 $crop_width = $x2 - $x1;
210 $crop_height = $y2 - $y1;
211 if ($crop_width <= 0 || $crop_height <= 0 || $crop_width > $width || $crop_height > $height) {
212 throw new RangeException(
"Coordinates [$x1, $y1], [$x2, $y2] are invalid for image cropping");
216 $crop_width = $width;
217 $crop_height = $height;
225 $max_width = min($max_width, $max_height);
226 $max_height = $max_width;
229 $crop_width = min($crop_width, $crop_height);
230 $crop_height = $crop_width;
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)) {
295 $this->getLogger()->warning($e);
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);
331 if (!is_array(
$info)) {
336 if ($height > $this->config->image_resize_max_height) {
341 if ($width > $this->config->image_resize_max_width) {
342 throw new RangeException(
'Image width too large to resize');
345 if (($width * $height) > $this->config->image_resize_max_resolution) {
346 throw new RangeException(
'Image resolution too large to resize');
$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.
assertValidImageDimensions(string $path)
Assert that the source image has valid dimensions.
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.