19 const JPEG_QUALITY = 75;
38 $this->imagine = $imagine;
39 $this->config = $config;
66 public function resize($source, $destination = null, array
$params = []) {
68 if (!isset($destination)) {
69 $destination = $source;
73 $image = $this->imagine->open($source);
75 $width =
$image->getSize()->getWidth();
76 $height =
$image->getSize()->getHeight();
78 $resize_params = $this->normalizeResizeParameters($width, $height,
$params);
88 if ($x2 > $x1 && $y2 > $y1) {
89 $crop_start =
new Point($x1, $y1);
90 $crop_size =
new Box($x2 - $x1, $y2 - $y1);
91 $image->crop($crop_start, $crop_size);
94 $target_size =
new Box($max_width, $max_height);
95 $thumbnail =
$image->resize($target_size);
97 $thumbnail->save($destination, [
99 'format' => $this->getFileFormat($source,
$params),
131 if (!$max_height || !$max_width) {
132 throw new \LogicException(
"Resize width and height parameters are required");
143 $cropping_mode = $x1 || $y1 || $x2 || $y2;
145 if ($cropping_mode) {
146 $crop_width = $x2 - $x1;
147 $crop_height = $y2 - $y1;
148 if ($crop_width <= 0 || $crop_height <= 0 || $crop_width > $width || $crop_height > $height) {
149 throw new \LogicException(
"Coordinates [$x1, $y1], [$x2, $y2] are invalid for image cropping");
153 $crop_width = $width;
154 $crop_height = $height;
161 if ($cropping_mode ==
true && $crop_width != $crop_height) {
162 throw new \LogicException(
"Coordinates [$x1, $y1], [$x2, $y2] are invalid for a squared image cropping");
166 $max_width = $max_height = min($max_width, $max_height);
169 $crop_width = $crop_height = min($crop_width, $crop_height);
171 if (!$cropping_mode) {
173 $x1 = floor(($width - $crop_width) / 2);
174 $y1 = floor(($height - $crop_height) / 2);
178 if ($crop_height / $max_height > $crop_width / $max_width) {
179 $max_width = floor($max_height * $crop_width / $crop_height);
181 $max_height = floor($max_width * $crop_height / $crop_width);
185 if (!$upscale && ($crop_height < $max_height || $crop_width < $max_width)) {
187 $max_height = $crop_height;
188 $max_width = $crop_width;
196 'x2' => $x1 + $crop_width,
197 'y2' => $y1 + $crop_height,
199 'upscale' => $upscale,
214 $accepted_formats = [
215 'image/jpeg' =>
'jpeg',
216 'image/pjpeg' =>
'jpeg',
217 'image/png' =>
'png',
218 'image/x-png' =>
'png',
219 'image/gif' =>
'gif',
220 'image/vnd.wap.wbmp' =>
'wbmp',
221 'image/x‑xbitmap' =>
'xbm',
222 'image/x‑xbm' =>
'xbm',
227 if (in_array(
$format, $accepted_formats)) {
232 $mime = $mime_detector->getType(
$filename);
Access to configuration values.
Image manipulation service.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
getFileFormat($filename, $params)
Determine the image file format, this is needed for correct resizing.
__construct(ImagineInterface $imagine, Config $config)
Constructor.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Detect the MIME type of a file.
resize($source, $destination=null, array $params=[])
Crop and resize an image.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
normalizeResizeParameters($width, $height, array $params=[])
Calculate the parameters for resizing an image.