82 $this->events = $events;
83 $this->entities = $entities;
84 $this->uploads = $uploads;
85 $this->images = $images;
86 $this->mimetype = $mimetype;
107 if (empty($coords)) {
108 $auto_coords = $this->detectCroppingCoordinates(
$input_name);
109 if (!empty($auto_coords)) {
110 $coords = $auto_coords;
114 $tmp = new \ElggTempFile();
115 $tmp->setFilename(uniqid() .
$input->getClientOriginalName());
119 copy(
$input->getPathname(), $tmp->getFilenameOnFilestore());
121 $tmp->mimetype = $this->mimetype->getMimeType($tmp->getFilenameOnFilestore());
122 $tmp->simpletype = $this->mimetype->getSimpleType($tmp->mimetype);
124 $result = $this->saveIcon($entity, $tmp,
$type, $coords);
147 $tmp = new \ElggTempFile();
148 $tmp->setFilename(uniqid() . basename(
$filename));
154 $tmp->mimetype = $this->mimetype->getMimeType($tmp->getFilenameOnFilestore());
155 $tmp->simpletype = $this->mimetype->getSimpleType($tmp->mimetype);
157 $result = $this->saveIcon($entity, $tmp,
$type, $coords);
177 throw new InvalidArgumentException(__METHOD__ .
' expects an instance of ElggFile with an existing file on filestore');
180 $tmp = new \ElggTempFile();
187 $tmp->mimetype = $this->mimetype->getMimeType($tmp->getFilenameOnFilestore(), $file->
getMimeType() ?:
'');
188 $tmp->simpletype = $this->mimetype->getSimpleType($tmp->mimetype);
190 $result = $this->saveIcon($entity, $tmp,
$type, $coords);
211 $this->
getLogger()->error(
'Icon type passed to ' . __METHOD__ .
' can not be empty');
215 $entity_type = $entity->
getType();
217 $file = $this->events->triggerResults(
"entity:{$type}:prepare", $entity_type, [
223 $this->
getLogger()->error(
'Source file passed to ' . __METHOD__ .
' can not be resolved to a valid image');
234 $created = $this->events->triggerResults(
"entity:{$type}:save", $entity_type, [
244 if ($created !==
true) {
246 $this->deleteIcon($entity,
$type,
true);
249 $store = $this->generateIcon($entity, $file,
$type, $coords,
'master');
252 $this->deleteIcon($entity,
$type);
259 $coords = array_merge($sizes[
'master'], $coords);
261 $icon = $this->getIcon($entity,
'master',
$type,
false);
263 $this->images->normalizeResizeParameters(
$icon->getFilenameOnFilestore(), $coords);
273 if (
$type ==
'icon') {
274 $entity->icontime = time();
275 if ($x1 || $y1 || $x2 || $y2) {
282 if ($x1 || $y1 || $x2 || $y2) {
283 $entity->{
"{$type}_coords"} = serialize([
292 $this->events->triggerResults(
"entity:{$type}:saved",
$entity->getType(), [
313 $temp_file = new \ElggTempFile();
314 $temp_file->setFilename(uniqid() . basename(
$filename));
318 $rotated = $this->images->fixOrientation($temp_file->getFilenameOnFilestore());
324 $temp_file->delete();
341 $this->
getLogger()->error(
'Trying to generate an icon from a non-existing file');
353 $this->
getLogger()->warning(
"The provided icon size '{$icon_size}' doesn't exist for icon type '{$type}'");
357 foreach ($sizes as
$size => $opts) {
358 if (!empty($icon_size) && ($icon_size !==
$size)) {
378 $icon->open(
'write');
383 if (is_array($opts) && empty($opts)) {
389 $destination =
$icon->getFilenameOnFilestore();
391 $resize_params = array_merge($opts, $coords);
394 $image_service->setLogger($this->
getLogger());
397 $this->
getLogger()->error(
"Failed to create {$size} icon from 398 {$file->getFilenameOnFilestore()} with coords [{$x1}, {$y1}],[{$x2}, {$y2}]");
431 $entity_type = $entity->
getType();
433 $default_icon = new \ElggIcon();
434 $default_icon->owner_guid = $entity->guid;
435 $default_icon->setFilename(
"icons/{$type}/{$size}.jpg");
437 $icon = $this->events->triggerResults(
"entity:{$type}:file", $entity_type,
$params, $default_icon);
439 throw new UnexpectedValueException(
"'entity:{$type}:file', {$entity_type} event must return an instance of \ElggIcon");
442 if (
$size !==
'master' && $this->hasWebPSupport()) {
443 if (pathinfo(
$icon->getFilename(), PATHINFO_EXTENSION) ===
'jpg') {
448 if (
$icon->exists() || !$generate) {
452 if (
$size ===
'master') {
458 $master_icon = $this->getIcon($entity,
'master',
$type,
false);
459 if (!$master_icon->exists()) {
463 if (
$type ===
'icon') {
471 $coords = $entity->{
"{$type}_coords"};
472 $coords = empty($coords) ? [] : unserialize($coords);
490 $delete = $this->events->triggerResults(
"entity:{$type}:delete", $entity->
getType(), [
499 $supported_extensions = [
502 if ($this->images->hasWebPSupport()) {
503 $supported_extensions[] =
'webp';
507 foreach ($sizes as
$size) {
508 if ($size ===
'master' && $retain_master) {
512 $icon = $this->getIcon($entity, $size,
$type,
false);
516 $current_extension = pathinfo(
$icon->getFilename(), PATHINFO_EXTENSION);
519 if ($current_extension === $extension) {
525 $parts = explode(
'.',
$icon->getFilename());
535 if (
$type ==
'icon') {
536 unset($entity->icontime);
542 unset($entity->{
"{$type}_coords"});
573 $entity_type = $entity->
getType();
575 $url = $this->events->triggerResults(
"entity:{$type}:url", $entity_type,
$params, null);
579 $default_use_cookie = (bool)
elgg_get_config(
'session_bound_entity_icons');
582 $url = $this->getFallbackIconUrl($entity,
$params);
605 $entity_type = $entity->
getType();
608 $exts = [
'svg',
'gif',
'png',
'jpg'];
610 foreach ($exts as $ext) {
611 foreach ([$entity_subtype,
'default'] as
$subtype) {
612 if ($ext ==
'svg' &&
elgg_view_exists(
"{$type}/{$entity_type}/{$subtype}.svg",
'default')) {
616 if (
elgg_view_exists(
"{$type}/{$entity_type}/{$subtype}/{$size}.{$ext}",
'default')) {
638 if (
$icon->exists()) {
639 return $icon->getModifiedTime();
654 return $icon->exists() &&
$icon->getSize() > 0;
667 public function getSizes(
string $entity_type = null,
string $entity_subtype = null,
$type =
'icon'): array {
670 if (
$type ==
'icon') {
671 $sizes = $this->config->icon_sizes;
676 'entity_type' => $entity_type,
677 'entity_subtype' => $entity_subtype,
680 $sizes = $this->events->triggerResults(
"entity:{$type}:sizes", $entity_type,
$params, $sizes);
683 if (!is_array($sizes)) {
684 $msg =
"The icon size configuration for image type '{$type}'";
685 $msg .=
' must be an associative array of image size names and their properties';
699 if (!isset($sizes[
'master'][
'crop'])) {
700 $sizes[
'master'][
'crop'] =
false;
724 $auto_coords = array_filter($auto_coords,
function(
$value) {
728 if (
count($auto_coords) !== 4) {
733 array_walk($auto_coords,
function (&
$value) {
738 if ($auto_coords[
'x2'] <= $auto_coords[
'x1'] || $auto_coords[
'y2'] <= $auto_coords[
'y1']) {
751 return in_array(
'image/webp', $this->request->getAcceptableContentTypes()) && $this->images->hasWebPSupport();
getSubtype()
Get the entity subtype.
deleteIcon(\ElggEntity $entity, $type= 'icon', $retain_master=false)
Removes all icon files and metadata for the passed type of icon.
Exception thrown if an argument is not of the expected type.
$params
Saves global plugin settings.
saveIconFromUploadedFile(\ElggEntity $entity, $input_name, $type= 'icon', array $coords=[])
Saves icons using an uploaded file as the source.
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
saveIconFromElggFile(\ElggEntity $entity,\ElggFile $file, $type= 'icon', array $coords=[])
Saves icons using a file located in the data store as the source.
if($icon===false) if($icon!== '') $icon_size
saveIconFromLocalFile(\ElggEntity $entity, $filename, $type= 'icon', array $coords=[])
Saves icons using a local file as the source.
exists()
Returns if the file exists.
getSizes(string $entity_type=null, string $entity_subtype=null, $type= 'icon')
Returns a configuration array of icon sizes.
getSimpleType()
Get the simple type of the file.
getFallbackIconUrl(\ElggEntity $entity, array $params=[])
Returns default/fallback icon.
hasIcon(\ElggEntity $entity, $size, $type= 'icon')
Returns if the entity has an icon of the passed type.
prepareIcon($filename)
Prepares an icon.
trait TimeUsing
Adds methods for setting the current time (for testing)
elgg_strtolower()
Wrapper function for mb_strtolower().
__construct(Config $config, EventsService $events, EntityTable $entities, UploadService $uploads, ImageService $images, MimeTypeService $mimetype, HttpRequest $request)
Constructor.
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.
getFilenameOnFilestore()
Return the filename of this file as it is/will be stored on the filestore, which may be different to ...
trait Loggable
Enables adding a logger.
if(function_exists('apache_get_version')) $icon
Exception thrown if a value does not match with a set of values.
generateIcon(\ElggEntity $entity,\ElggFile $file, $type= 'icon', $coords=[], $icon_size= '')
Generate an icon for the given entity.
getMimeType()
Get the mime type of the file.
Exception that represents error in the program logic.
getIcon(\ElggEntity $entity, $size, $type= 'icon', $generate=true)
Returns entity icon as an ElggIcon object The icon file may or may not exist on filestore.
getIconURL(\ElggEntity $entity, string|array $params=[])
Get the URL for this entity's icon.
hasWebPSupport()
Checks if browser has WebP support and if the webserver is able to generate.
Image manipulation service.
getIconLastChange(\ElggEntity $entity, $size, $type= 'icon')
Returns the timestamp of when the icon was changed.
getLogger()
Returns logger.
and give any other recipients of the Program a copy of this License along with the Program You may charge a fee for the physical act of transferring a copy
getType()
Returns the entity type.
saveIcon(\ElggEntity $entity,\ElggFile $file, $type= 'icon', array $coords=[])
Saves icons using a created temporary file.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
_elgg_services()
Get the global service provider.
detectCroppingCoordinates(string $input_name)
Automagicly detect cropping coordinates.
elgg_normalize_url(string $url)
elgg_get_simplecache_url(string $view, string $subview= '')
Get the URL for the cached view.
Public service related to MIME type detection.
elgg_view_exists(string $view, string $viewtype= '', bool $recurse=true)
Returns whether the specified view exists.
Entity table database service.
File upload handling service.