Elgg method to get the file icon address

  • 2020-03-31 20:28:46
  • OfStack

The process is as follows:
First, the entity is saved using this method (the system itself) :
Let's say I have an Activity class that inherits from ElggObject and creates an instance of its Activity,
 
// Now see if we have a file icon 
if ((isset($_FILES['icon'])) && (substr_count($_FILES['icon']['type'],'image/'))) { 
$prefix = "activity/".$activity->guid; 
$filehandler = new ElggFile(); 
$filehandler->owner_guid = $activity->owner_guid; 
$filehandler->setFilename($prefix . ".jpg"); 
$filehandler->open("write"); 
$filehandler->write(get_uploaded_file('icon')); 
$filehandler->close(); 
$thumbtiny = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),25,25, true); 
$thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),40,40, true); 
$thumbmedium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),100,100, true); 
$thumblarge = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),200,200, false); 
if ($thumbtiny) { 
$thumb = new ElggFile(); 
$thumb->owner_guid = $activity->owner_guid; 
$thumb->setMimeType('image/jpeg'); 
$thumb->setFilename($prefix."tiny.jpg"); 
$thumb->open("write"); 
$thumb->write($thumbtiny); 
$thumb->close(); 
$thumb->setFilename($prefix."small.jpg"); 
$thumb->open("write"); 
$thumb->write($thumbsmall); 
$thumb->close(); 
$thumb->setFilename($prefix."medium.jpg"); 
$thumb->open("write"); 
$thumb->write($thumbmedium); 
$thumb->close(); 
$thumb->setFilename($prefix."large.jpg"); 
$thumb->open("write"); 
$thumb->write($thumblarge); 
$thumb->close(); 
} 
} 

After this process, the file is saved to a directory structure consisting of a string of user names, such as ABC, under a/b/c/, and then a file name consisting of the guid+size+.jpg of the image.
Get the SRC address through the entity -> GetIcon (); Method to get. GetIcon is a method in entities.php. This method then calls the get_entity_icon_url method, and there is a line in the get_entity_icon_url method:
$url = trigger_plugin_hook (' entity: icon: url, $entity - > GetType (), array (' entity '= > $entity, 'viewtype = > $viewtype, 'size' = > Size), $url);
This triggers a hook, and the hood needs to be registered in the plug-in's start.php. To register, write:
Register_plugin_hook (' entity: icon: url ', 'object', 'activity_activityicon_hook');
The first parameter is the hook type, the second is the entity type, which is the type of activity, and the third is the hook function name.
Then write the activity_activityicon_hook method in start.php:
 
 
function activity_activityicon_hook($hook, $entity_type, $returnvalue, $params) { 
global $CONFIG; 
if ((!$returnvalue) && ($hook == 'entity:icon:url') && ($params['entity'] instanceof Activity)) { 
$entity = $params['entity']; 
$type = $entity->type; 
$viewtype = $params['viewtype']; 
$size = $params['size']; 
if ($icontime = $entity->icontime) { 
$icontime = "{$icontime}"; 
} else { 
$icontime = "default"; 
} 
$filehandler = new ElggFile(); 
$filehandler->owner_guid = $entity->owner_guid; 
$filehandler->setFilename("activity/" . $entity->guid . $size . ".jpg"); 
if ($filehandler->exists()) { 
$url = $CONFIG->url . "pg/activityicon/{$entity->guid}/$size/$icontime.jpg"; 
return $url; 
} 
} 
} 

This method returns a url, which is the address of SRC. After the url is returned to get_entity_icon_url, it will continue processing according to the image size and return the final url. So you get the SRC address.

Related articles: