58 $input = $this->uploads->getFile($input_name);
65 $auto_coords = $this->detectCroppingCoordinates($input_name);
66 if (!empty($auto_coords)) {
67 $coords = $auto_coords;
71 $tmp = new \ElggTempFile();
72 $tmp->setFilename(uniqid() .
$input->getClientOriginalName());
76 copy(
$input->getPathname(), $tmp->getFilenameOnFilestore());
78 $tmp->mimetype = $this->mimetype->getMimeType($tmp->getFilenameOnFilestore());
79 $tmp->simpletype = $this->mimetype->getSimpleType($tmp->mimetype);
81 $result = $this->saveIcon($entity, $tmp,
$type, $coords);
100 if (!file_exists($filename) || !is_readable($filename)) {
104 $tmp = new \ElggTempFile();
105 $tmp->setFilename(uniqid() . basename($filename));
109 copy($filename, $tmp->getFilenameOnFilestore());
111 $tmp->mimetype = $this->mimetype->getMimeType($tmp->getFilenameOnFilestore());
112 $tmp->simpletype = $this->mimetype->getSimpleType($tmp->mimetype);
114 $result = $this->saveIcon($entity, $tmp,
$type, $coords);
134 throw new InvalidArgumentException(__METHOD__ .
' expects an instance of ElggFile with an existing file on filestore');
137 $tmp = new \ElggTempFile();
144 $tmp->mimetype = $this->mimetype->getMimeType($tmp->getFilenameOnFilestore(), $file->
getMimeType() ?:
'');
145 $tmp->simpletype = $this->mimetype->getSimpleType($tmp->mimetype);
147 $result = $this->saveIcon($entity, $tmp,
$type, $coords);
166 $this->
getLogger()->error(
'Icon type passed to ' . __METHOD__ .
' can not be empty');
170 $entity_type = $entity->
getType();
172 $file = $this->events->triggerResults(
"entity:{$type}:prepare", $entity_type, [
178 $this->
getLogger()->error(
'Source file passed to ' . __METHOD__ .
' can not be resolved to a valid image');
182 $entity->lockIconThumbnailGeneration(
$type);
191 $created = $this->events->triggerResults(
"entity:{$type}:save", $entity_type, [
201 if ($created !==
true) {
203 $this->deleteIcon($entity,
$type,
true);
206 $store = $this->generateIcon($entity, $file,
$type, $coords,
'master');
209 $this->deleteIcon($entity,
$type);
215 $coords = array_merge($sizes[
'master'], $coords);
217 $icon = $this->getIcon($entity,
'master',
$type,
false);
220 $this->images->normalizeResizeParameters(
$icon->getFilenameOnFilestore(), $coords);
235 if ($x1 || $y1 || $x2 || $y2) {
236 $entity->saveIconCoordinates($coords,
$type);
239 $this->events->triggerResults(
"entity:{$type}:saved", $entity->
getType(), [
247 $entity->unlockIconThumbnailGeneration(
$type);
261 $temp_file = new \ElggTempFile();
262 $temp_file->setFilename(uniqid() . basename($filename));
264 copy($filename, $temp_file->getFilenameOnFilestore());
266 $rotated = $this->images->fixOrientation($temp_file->getFilenameOnFilestore());
272 $temp_file->delete();
288 $this->
getLogger()->error(
'Trying to generate an icon from a non-existing file');
300 $this->
getLogger()->warning(
"The provided icon size '{$icon_size}' doesn't exist for icon type '{$type}'");
304 foreach ($sizes as
$size => $opts) {
305 if (!empty($icon_size) && ($icon_size !==
$size)) {
325 $icon->open(
'write');
330 if (is_array($opts) && empty($opts)) {
336 $destination =
$icon->getFilenameOnFilestore();
338 $resize_params = array_merge($opts, $coords);
341 $image_service->setLogger($this->
getLogger());
344 $this->
getLogger()->error(
"Failed to create {$size} icon from 345 {$file->getFilenameOnFilestore()} with coords [{$x1}, {$y1}],[{$x2}, {$y2}]");
347 if (
$size !==
'master') {
383 $entity_type = $entity->
getType();
385 $default_icon = new \ElggIcon();
386 $default_icon->owner_guid = $entity->guid;
387 $default_icon->setFilename(
"icons/{$type}/{$size}.jpg");
389 $icon = $this->events->triggerResults(
"entity:{$type}:file", $entity_type,
$params, $default_icon);
391 throw new UnexpectedValueException(
"'entity:{$type}:file', {$entity_type} event must return an instance of \ElggIcon");
394 if ($size !==
'master' && $this->hasWebPSupport()) {
395 if (pathinfo(
$icon->getFilename(), PATHINFO_EXTENSION) ===
'jpg') {
400 if (
$icon->exists() || !$generate) {
404 if ($size ===
'master') {
409 if ($entity->isIconThumbnailGenerationLocked(
$type)) {
414 $master_icon = $this->getIcon($entity,
'master',
$type,
false);
415 if (!$master_icon->exists()) {
419 $coords = $entity->getIconCoordinates(
$type);
421 $this->generateIcon($entity, $master_icon,
$type, $coords, $size);
436 $delete = $this->events->triggerResults(
"entity:{$type}:delete", $entity->
getType(), [
438 'retain_master' => $retain_master,
446 $supported_extensions = [
449 if ($this->images->hasWebPSupport()) {
450 $supported_extensions[] =
'webp';
454 foreach ($sizes as
$size) {
455 if ($size ===
'master' && $retain_master) {
459 $icon = $this->getIcon($entity, $size,
$type,
false);
463 $current_extension = pathinfo(
$icon->getFilename(), PATHINFO_EXTENSION);
466 if ($current_extension === $extension) {
472 $parts = explode(
'.',
$icon->getFilename());
482 $entity->removeIconCoordinates(
$type);
512 $entity_type = $entity->
getType();
514 $url = $this->events->triggerResults(
"entity:{$type}:url", $entity_type,
$params, null);
518 $default_use_cookie = (bool)
elgg_get_config(
'session_bound_entity_icons');
521 $url = $this->getFallbackIconUrl($entity,
$params);
544 $entity_type = $entity->
getType();
547 $exts = [
'svg',
'gif',
'png',
'jpg'];
549 foreach ($exts as $ext) {
550 foreach ([$entity_subtype,
'default'] as
$subtype) {
551 if ($ext ==
'svg' &&
elgg_view_exists(
"{$type}/{$entity_type}/{$subtype}.svg",
'default')) {
555 if (
elgg_view_exists(
"{$type}/{$entity_type}/{$subtype}/{$size}.{$ext}",
'default')) {
578 $icon = $this->getIcon($entity, $size,
$type);
579 if (
$icon->exists()) {
580 return $icon->getModifiedTime();
596 $icon = $this->getIcon($entity, $size,
$type);
597 return $icon->exists() &&
$icon->getSize() > 0;
610 public function getSizes(
string $entity_type = null,
string $entity_subtype = null,
$type =
'icon'): array {
613 if (
$type ==
'icon') {
614 $sizes = $this->config->icon_sizes;
619 'entity_type' => $entity_type,
620 'entity_subtype' => $entity_subtype,
623 $sizes = $this->events->triggerResults(
"entity:{$type}:sizes", $entity_type,
$params, $sizes);
626 if (!is_array($sizes)) {
627 $msg =
"The icon size configuration for image type '{$type}'";
628 $msg .=
' must be an associative array of image size names and their properties';
642 if (!isset($sizes[
'master'][
'crop'])) {
643 $sizes[
'master'][
'crop'] =
false;
666 $auto_coords = array_filter($auto_coords,
function(
$value) {
670 if (
count($auto_coords) !== 4) {
675 array_walk($auto_coords,
function (&
$value) {
680 if ($auto_coords[
'x2'] <= $auto_coords[
'x1'] || $auto_coords[
'y2'] <= $auto_coords[
'y1']) {
693 return in_array(
'image/webp', $this->request->getAcceptableContentTypes()) && $this->images->hasWebPSupport();
getSubtype()
Get the entity subtype.
Exception thrown if an argument is not of the expected type.
$params
Saves global plugin settings.
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
getIcon(\ElggEntity $entity, string $size, string $type= 'icon', bool $generate=true)
Returns entity icon as an ElggIcon object The icon file may or may not exist on filestore.
invalidateCache()
Invalidate cache for entity.
if($icon===false) if($icon!== '') $icon_size
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
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.
trait TimeUsing
Adds methods for setting the current time (for testing)
elgg_strtolower()
Wrapper function for mb_strtolower().
saveIconFromLocalFile(\ElggEntity $entity, string $filename, string $type= 'icon', array $coords=[])
Saves icons using a local file as the source.
Generic interface which allows catching of all exceptions thrown in Elgg.
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 ...
saveIconFromElggFile(\ElggEntity $entity,\ElggFile $file, string $type= 'icon', array $coords=[])
Saves icons using a file located in the data store as the source.
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.
saveIconFromUploadedFile(\ElggEntity $entity, string $input_name, string $type= 'icon', array $coords=[])
Saves icons using an uploaded file as the source.
generateIcon(\ElggEntity $entity,\ElggFile $file, string $type= 'icon', array $coords=[], string $icon_size= '')
Generate an icon for the given entity.
getMimeType()
Get the mime type of the file.
saveIcon(\ElggEntity $entity,\ElggFile $file, string $type= 'icon', array $coords=[])
Saves icons using a created temporary file.
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.
deleteIcon(\ElggEntity $entity, string $type= 'icon', bool $retain_master=false)
Removes all icon files and metadata for the passed type of icon.
prepareIcon(string $filename)
Prepares an icon.
__construct(protected Config $config, protected EventsService $events, protected EntityTable $entities, protected UploadService $uploads, protected ImageService $images, protected MimeTypeService $mimetype, protected HttpRequest $request)
Constructor.
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.
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_get_simplecache_url(string $view)
Get the URL for the cached view.
elgg_normalize_url(string $url)
hasIcon(\ElggEntity $entity, string $size, string $type= 'icon')
Returns if the entity has an icon of the passed type.
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.
getIconLastChange(\ElggEntity $entity, string $size, string $type= 'icon')
Returns the timestamp of when the icon was changed.
File upload handling service.