How can elgg obtain the file icon address?

Source: Internet
Author: User
Tags elgg

The process is as follows:
First, this method is used when the object is saved (the system itself ):
For example, an Activity class inherits from ElggObject and creates an instance activity,
Copy codeThe Code is as follows:
// 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-& gt; getFilenameOnFilestore (), 100,100, true );
$ Thumblarge = get_resized_image_from_existing_file ($ filehandler-& gt; 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, the user name is abc, it is saved under a/B/c/. Then, a file name is composed of guid1_size0000.jpg of the image.
You can use the entity-> getIcon (); method to obtain the src address. GetIcon is the method in entities. php. Then this method will call the get_entity_icon_url method. There is a 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 will trigger a hook. This hood needs to be registered in start. php of the plug-in. Write as follows during registration:
Register_plugin_hook ('entity: icon: url', 'object', 'activity _ activityicon_hook ');
The first parameter is the hook type, the second is the entity type, that is, the activity type, and the third is the hook function name.
Then write the activity_activityicon_hook method in start. php:
Copy codeThe Code is as follows:
/**
* Retrieve icons
* This hooks into the getIcon API and provides nice user icons for users where possible.
*
* @ Param string $ hook name
* @ Param string $ entity_type object type
* @ Param string $ returnvalue url of the image
* @ Param unknow $ params parameter table column
* @ Return string $ url of the image
*/
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 src address. After the url is returned to get_entity_icon_url, the final url will be returned Based on the image size. In this way, the src address is obtained.

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.