The process is as follows:
First, the entity is saved using this method (the system itself):
For example, there is an activity class that inherits from Elggobject and creates an instance of its activity,
Copy Code code as follows:
Now 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 (' icon ') (Get_uploaded_file);
$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 will be saved to a directory structure consisting of a user name string, for example, if the user name is ABC, it is saved under a/b/c/, then the guid+size+.jpg of the picture consists of a filename.
When obtaining the SRC address, the entity->geticon () is used to obtain the. GetIcon is the method in entities.php. This method then invokes the Get_entity_icon_url method, which has one row in the Get_entity_icon_url method:
$url = Trigger_plugin_hook (' Entity:icon:url ', $entity->gettype (), Array (' Entity ' => $entity, ' ViewType ' => $ ViewType, ' size ' => $size), $url);
It triggers a hook, and this hood needs to be registered in the start.php of the plugin. When registering, write this:
Register_plugin_hook (' Entity:icon:url ', ' object ', ' Activity_activityicon_hook ');
The first parameter is the hook type, the second is the entity type, the type of activity, and the third is the name of the hook function.
Then write the Activity_activityicon_hook method in start.php:
Copy Code code as follows:
/**
* Get icon
* This hooks to the GetIcon API and provides nice user icons for users where possible.
*
* @param string $hook hook name
* @param string $entity _type entity type
* @param string $returnvalue picture URL address
* @param unknow $params parameter table columns
* @return string $url picture URL address
*/
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 returns to Get_entity_icon_url, it continues processing according to the picture size, returning the final URL. This gets the SRC address.