11 use Elgg\Traits\Loggable;
12 use Elgg\Traits\TimeUsing;
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);
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);
133 if (!$file->exists()) {
134 throw new InvalidArgumentException(__METHOD__ .
' expects an instance of ElggFile with an existing file on filestore');
137 $tmp = new \ElggTempFile();
138 $tmp->setFilename(uniqid() . basename($file->getFilenameOnFilestore()));
142 copy($file->getFilenameOnFilestore(), $tmp->getFilenameOnFilestore());
144 $tmp->mimetype = $this->mimetype->getMimeType($tmp->getFilenameOnFilestore(), $file->getMimeType() ?:
'');
145 $tmp->simpletype = $this->mimetype->getSimpleType($tmp->mimetype);
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, [
177 if (!$file instanceof \
ElggFile || !$file->
exists() || $file->getSimpleType() !==
'image') {
178 $this->getLogger()->error(
'Source file passed to ' . __METHOD__ .
' can not be resolved to a valid image');
184 $this->prepareIcon($file->getFilenameOnFilestore());
191 $created = $this->events->triggerResults(
"entity:{$type}:save", $entity_type, [
201 if ($created !==
true) {
206 $store = $this->generateIcon(
$entity, $file,
$type, $coords,
'master');
215 $coords = array_merge($sizes[
'master'], $coords);
220 $this->images->normalizeResizeParameters(
$icon->getFilenameOnFilestore(), $coords);
221 }
catch (ExceptionInterface $e) {
235 if ($x1 || $y1 || $x2 || $y2) {
239 $this->events->triggerResults(
"entity:{$type}:saved",
$entity->getType(), [
264 copy(
$filename, $temp_file->getFilenameOnFilestore());
266 $rotated = $this->images->fixOrientation($temp_file->getFilenameOnFilestore());
269 copy($temp_file->getFilenameOnFilestore(),
$filename);
272 $temp_file->delete();
287 if (!$file->exists()) {
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) {
325 $icon->open(
'write');
330 if (is_array($opts) && empty($opts)) {
331 copy($file->getFilenameOnFilestore(),
$icon->getFilenameOnFilestore());
335 $source = $file->getFilenameOnFilestore();
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') {
396 $icon->setFilename(substr(
$icon->getFilename(), 0, -3) .
'webp');
400 if (
$icon->exists() || !$generate) {
404 if (
$size ===
'master') {
414 $master_icon = $this->getIcon(
$entity,
'master',
$type,
false);
415 if (!$master_icon->exists()) {
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) {
463 $current_extension = pathinfo(
$icon->getFilename(), PATHINFO_EXTENSION);
472 $parts = explode(
'.',
$icon->getFilename());
477 $icon->setFilename(implode(
'.', $parts));
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');
544 $entity_type =
$entity->getType();
545 $entity_subtype =
$entity->getSubtype();
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')) {
579 if (
$icon->exists()) {
580 return $icon->getModifiedTime();
597 return $icon->exists() &&
$icon->getSize() > 0;
610 public function getSizes(?
string $entity_type =
null, ?
string $entity_subtype =
null,
string $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';
629 throw new InvalidArgumentException($msg);
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();
getLogger()
Returns logger.
$params
Saves global plugin settings.
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
if($icon===false) if($icon !=='') $icon_size
elgg_get_simplecache_url(string $view)
Get the URL for the cached view.
setFilename(string $filename)
Set the filename of this file.
exists()
Returns if the file exists.
This class represents a physical file (by default in the system temp directory).
Entity table database service.
getIconLastChange(\ElggEntity $entity, string $size, string $type='icon')
Returns the timestamp of when the icon was changed.
detectCroppingCoordinates(string $input_name)
Automagicly detect cropping coordinates.
__construct(protected Config $config, protected EventsService $events, protected EntityTable $entities, protected UploadService $uploads, protected ImageService $images, protected MimeTypeService $mimetype, protected HttpRequest $request)
Constructor.
saveIconFromLocalFile(\ElggEntity $entity, string $filename, string $type='icon', array $coords=[])
Saves icons using a local file as the source.
getSizes(?string $entity_type=null, ?string $entity_subtype=null, string $type='icon')
Returns a configuration array of icon sizes.
prepareIcon(string $filename)
Prepares an icon.
hasIcon(\ElggEntity $entity, string $size, string $type='icon')
Returns if the entity has an icon of the passed type.
getFallbackIconUrl(\ElggEntity $entity, array $params=[])
Returns default/fallback icon.
hasWebPSupport()
Checks if browser has WebP support and if the webserver is able to generate.
generateIcon(\ElggEntity $entity, \ElggFile $file, string $type='icon', array $coords=[], string $icon_size='')
Generate an icon for the given entity.
getIconURL(\ElggEntity $entity, string|array $params=[])
Get the URL for this entity's icon.
saveIcon(\ElggEntity $entity, \ElggFile $file, string $type='icon', array $coords=[])
Saves icons using a created temporary file.
deleteIcon(\ElggEntity $entity, string $type='icon', bool $retain_master=false)
Removes all icon files and metadata for the passed type of icon.
saveIconFromElggFile(\ElggEntity $entity, \ElggFile $file, string $type='icon', array $coords=[])
Saves icons using a file located in the data store as the source.
saveIconFromUploadedFile(\ElggEntity $entity, string $input_name, string $type='icon', array $coords=[])
Saves icons using an uploaded file as the source.
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.
Exception thrown if an argument is not of the expected type.
Exception thrown if a value does not match with a set of values.
Public service related to MIME type detection.
Image manipulation service.
File upload handling service.
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
$config
Advanced site settings, debugging section.
_elgg_services()
Get the global service provider.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
if(function_exists('apache_get_version')) $icon
Generic interface which allows catching of all exceptions thrown in Elgg.
elgg_view_exists(string $view, string $viewtype='', bool $recurse=true)
Returns whether the specified view exists.
elgg_strtolower()
Wrapper function for mb_strtolower().
elgg_normalize_url(string $url)
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.