Elgg method to get file icon address _php tips

Source: Internet
Author: User
Tags elgg
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.