Home  >  Article  >  Backend Development  >  elgg How to get the file icon address_PHP tutorial

elgg How to get the file icon address_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:39:441355browse

The process is as follows:
First, use this method (system itself) when saving the entity:
For example, there is an Activity class, which inherits from ElggObject and creates an instance activity of it,

Copy code The 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->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 Save it to a directory structure composed of username strings. For example, if the username is abc, it will be saved under a/b/c/, and then a file name will be composed of the guid+size+.jpg of the picture.
When obtaining the src address, obtain it through the entity->getIcon(); method. getIcon is a method in entities.php. Then this method will call the get_entity_icon_url method. 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);
It will trigger a hook, which needs to be registered in the plug-in's start.php. Write this when registering:
register_plugin_hook('entity:icon:url', 'object', 'activity_activityicon_hook');
The first parameter is the hook type, and the second is the entity type, which is the type of activity , the third one is the hook function name.
Then write the activity_activityicon_hook method in start.php:
Copy the code The code is as follows:

/**
* Get Icon
* This hooks into 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 Image url address
* @param unknow $params Parameter table column
* @return string $url Image 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 will return a url, which is the address of src. After the url returns to get_entity_icon_url, it will continue to be processed according to the image size and the final url will be returned. In this way, the src address is obtained.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321496.htmlTechArticleThe process is as follows: First, use this method (system itself) when saving entities: For example, there is an Activity class, Inherited from ElggObject, created an instance activity of it, copy the code...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn