74 LoggerInterface $logger,
80 $this->hooks = $hooks;
82 $this->logger = $logger;
83 $this->entities = $entities;
84 $this->uploads = $uploads;
85 $this->images = $images;
105 $auto_coords = $this->detectCroppingCoordinates();
106 if (!empty($auto_coords)) {
111 $tmp = new \ElggTempFile();
112 $tmp->setFilename(uniqid() .
$input->getClientOriginalName());
116 copy(
$input->getPathname(), $tmp->getFilenameOnFilestore());
118 $tmp->mimetype = (
new MimeTypeDetector())->getType($tmp->getFilenameOnFilestore(),
$input->getClientMimeType());
143 $tmp = new \ElggTempFile();
144 $tmp->setFilename(uniqid() . basename(
$filename));
150 $tmp->mimetype = (
new MimeTypeDetector())->getType($tmp->getFilenameOnFilestore());
175 $tmp = new \ElggTempFile();
204 if (!strlen(
$type)) {
205 $this->logger->error(
'Icon type passed to ' . __METHOD__ .
' can not be empty');
209 $entity_type = $entity->
getType();
211 $file = $this->hooks->trigger(
"entity:$type:prepare", $entity_type, [
217 $this->logger->error(
'Source file passed to ' . __METHOD__ .
' can not be resolved to a valid image');
228 $created = $this->hooks->trigger(
"entity:$type:save", $entity_type, [
238 if ($created !==
true) {
240 $this->deleteIcon($entity,
$type,
true);
243 $store = $this->generateIcon($entity, $file,
$type,
$coords,
'master');
246 $this->deleteIcon($entity,
$type);
251 if (
$type ==
'icon') {
252 $entity->icontime = time();
253 if ($x1 || $y1 || $x2 || $y2) {
260 if ($x1 || $y1 || $x2 || $y2) {
261 $entity->{
"{$type}_coords"} = serialize([
270 $this->hooks->trigger(
"entity:$type:saved",
$entity->getType(), [
291 $temp_file = new \ElggTempFile();
292 $temp_file->setFilename(uniqid() . basename(
$filename));
296 $rotated = $this->images->fixOrientation($temp_file->getFilenameOnFilestore());
302 $temp_file->delete();
319 $this->logger->error(
'Trying to generate an icon from a non-existing file');
331 $this->logger->warning(
"The provided icon size '{$icon_size}' doesn't exist for icon type '{$type}'");
335 foreach ($sizes as
$size => $opts) {
336 if (!empty($icon_size) && ($icon_size !==
$size)) {
356 $icon->open(
'write');
361 if (is_array($opts) && empty($opts)) {
367 $destination =
$icon->getFilenameOnFilestore();
369 $resize_params = array_merge($opts,
$coords);
372 $image_service->setLogger($this->logger);
375 $this->logger->error(
"Failed to create {$size} icon from 376 {$file->getFilenameOnFilestore()} with coords [{$x1}, {$y1}],[{$x2}, {$y2}]");
409 $entity_type = $entity->
getType();
412 $default_icon->owner_guid = $entity->guid;
413 $default_icon->setFilename(
"icons/$type/$size.jpg");
415 $icon = $this->hooks->trigger(
"entity:$type:file", $entity_type,
$params, $default_icon);
420 if (
$icon->exists() || !$generate) {
424 if (
$size ===
'master') {
430 $master_icon = $this->getIcon($entity,
'master',
$type,
false);
431 if (!$master_icon->exists()) {
435 if (
$type ===
'icon') {
443 $coords = $entity->{
"{$type}_coords"};
462 $delete = $this->hooks->trigger(
"entity:$type:delete", $entity->
getType(), [
473 foreach ($sizes as
$size) {
474 if ($size ===
'master' && $retain_master) {
478 $icon = $this->getIcon($entity, $size,
$type,
false);
482 if (
$type ==
'icon') {
483 unset($entity->icontime);
489 unset($entity->{
"{$type}_coords"});
519 $entity_type = $entity->
getType();
521 $url = $this->hooks->trigger(
"entity:$type:url", $entity_type,
$params, null);
525 $default_use_cookie = (bool)
elgg_get_config(
'session_bound_entity_icons',
false);
528 $url = $this->getFallbackIconUrl($entity,
$params);
549 $entity_type = $entity->
getType();
552 $exts = [
'svg',
'gif',
'png',
'jpg'];
554 foreach ($exts as $ext) {
555 foreach ([$entity_subtype,
'default'] as
$subtype) {
581 if (
$icon->exists()) {
582 return $icon->getModifiedTime();
595 return $this->getIcon($entity,
$size,
$type)->exists();
607 public function getSizes($entity_type = null, $entity_subtype = null,
$type =
'icon') {
612 if (
$type ==
'icon') {
613 $sizes = $this->config->icon_sizes;
617 'entity_type' => $entity_type,
618 'entity_subtype' => $entity_subtype,
621 $sizes = $this->hooks->trigger(
"entity:$type:sizes", $entity_type,
$params, $sizes);
624 if (!is_array($sizes)) {
626 "must be an associative array of image size names and their properties");
639 if (!isset($sizes[
'master'][
'crop'])) {
640 $sizes[
'master'][
'crop'] =
false;
654 $response =
new Response();
656 $response->prepare($this->request);
658 if ($allow_removing_headers) {
660 header_remove(
'Cache-Control');
661 header_remove(
'Pragma');
662 header_remove(
'Expires');
665 $path = implode(
'/', $this->request->getUrlSegments());
666 if (!preg_match(
'~serve-icon/(\d+)/(.*+)$~',
$path, $m)) {
667 return $response->setStatusCode(400)->setContent(
'Malformatted request URL');
674 return $response->setStatusCode(404)->setContent(
'Item does not exist');
678 if (!$thumbnail->exists()) {
679 return $response->setStatusCode(404)->setContent(
'Icon does not exist');
682 $if_none_match = $this->request->headers->get(
'if_none_match');
683 if (!empty($if_none_match)) {
685 $this->request->headers->set(
'if_none_match', str_replace(
'-gzip',
'', $if_none_match));
688 $filenameonfilestore = $thumbnail->getFilenameOnFilestore();
689 $last_updated = filemtime($filenameonfilestore);
690 $etag =
'"' . $last_updated .
'"';
692 $response->setPrivate()
697 if ($response->isNotModified($this->request)) {
704 $response =
new BinaryFileResponse($filenameonfilestore, 200,
$headers,
false,
'inline');
705 $response->prepare($this->request);
707 $response->setPrivate()
731 $auto_coords = array_filter($auto_coords,
function(
$value) {
735 if (count($auto_coords) !== 4) {
740 array_walk($auto_coords,
function (&
$value) {
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
getSubtype()
Get the entity subtype.
if(!array_key_exists($filename, $text_files)) $file
$params
Saves global plugin settings.
if($icon===false) if(isset($icon)&&$icon!==true) $icon_size
$request
Page handler for autocomplete endpoint.
elgg_get_simplecache_url($view, $subview= '')
Get the URL for the cached view.
getFallbackIconUrl(ElggEntity $entity, array $params=[])
Returns default/fallback icon.
trait Loggable
Enables adding a logger.
exists()
Returns if the file exists.
getSizes($entity_type=null, $entity_subtype=null, $type= 'icon')
Returns a configuration array of icon sizes.
getSimpleType()
Get the simple type of the file.
getCurrentTime($modifier= '')
Get the (cloned) time.
hasIcon(\ElggEntity $entity, $size, $type= 'icon')
Returns if the entity has an icon of the passed type.
$guid
Removes an admin notice.
prepareIcon($filename)
Prepares an icon.
elgg_strtolower()
Wrapper function for mb_strtolower().
saveIconFromElggFile(ElggEntity $entity, ElggFile $file, $type= 'icon', array $coords=[])
Saves icons using a file located in the data store as the source.
$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 ...
handleServeIconRequest($allow_removing_headers=true)
Handle request to /serve-icon handler.
detectCroppingCoordinates()
Automagicly detect cropping coordinates.
getIconURL(ElggEntity $entity, $params=[])
Get the URL for this entity's icon.
deleteIcon(ElggEntity $entity, $type= 'icon', $retain_master=false)
Removes all icon files and metadata for the passed type of icon.
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.
getMimeType()
Get the mime type of the file.
Image manipulation service.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
generateIcon(ElggEntity $entity, ElggFile $file, $type= 'icon', $coords=[], $icon_size= '')
Generate an icon for the given entity.
getIconLastChange(ElggEntity $entity, $size, $type= 'icon')
Returns the timestamp of when the icon was changed.
__construct(Config $config, PluginHooksService $hooks, HttpRequest $request, LoggerInterface $logger, EntityTable $entities, UploadService $uploads, ImageService $images)
Constructor.
Detect the MIME type of a file.
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.
_elgg_services()
Get the global service provider.
saveIconFromUploadedFile(ElggEntity $entity, $input_name, $type= 'icon', array $coords=[])
Saves icons using an uploaded file as the source.
saveIconFromLocalFile(ElggEntity $entity, $filename, $type= 'icon', array $coords=[])
Saves icons using a local file as the source.
trait TimeUsing
Adds methods for setting the current time (for testing)
if(!$owner||!$owner->canEdit()) if(!$owner->hasIcon('master')) $coords
elgg_get_file_simple_type($mime_type)
Returns the category of a file from its MIME type.
elgg_get_config($name, $default=null)
Get an Elgg configuration value.
saveIcon(ElggEntity $entity, ElggFile $file, $type= 'icon', array $coords=[])
Saves icons using a created temporary file.