elgg本身有一套模板系统,可以加载图标。但是如果自己写了新的模板,怎样获得img的src地址呢?过程如下:
首先,实体保存的时候用这个方法(系统本身的):
比如有一个Activity类,继承自ElggObject,创建了一个它的实例 activity,
1 // Now see if we have a file icon
2
3 if ((isset($_FILES['icon'])) && (substr_count($_FILES['icon']['type'],'image/'))) {
4 $prefix = "activity/".$activity->guid;
5 $filehandler = new ElggFile();
6 $filehandler->owner_guid = $activity->owner_guid;
7 $filehandler->setFilename($prefix . ".jpg");
8 $filehandler->open("write");
9 $filehandler->write(get_uploaded_file('icon'));
10 $filehandler->close();
11 $thumbtiny = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),25,25, true);
12 $thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),40,40, true);
13 $thumbmedium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),100,100, true);
14 $thumblarge = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),200,200, false);
15
16 if ($thumbtiny) {
17 $thumb = new ElggFile();
18 $thumb->owner_guid = $activity->owner_guid;
19 $thumb->setMimeType('image/jpeg');
20 $thumb->setFilename($prefix."tiny.jpg");
21 $thumb->open("write");
22 $thumb->write($thumbtiny);
23 $thumb->close();
24 $thumb->setFilename($prefix."small.jpg");
25 $thumb->open("write");
26 $thumb->write($thumbsmall);
27 $thumb->close();
28
29 $thumb->setFilename($prefix."medium.jpg");
30 $thumb->open("write");
31 $thumb->write($thumbmedium);
32 $thumb->close();
33
34 $thumb->setFilename($prefix."large.jpg");
35 $thumb->open("write");
36 $thumb->write($thumblarge);
37 $thumb->close();
38 }
39 }
这个过程后,文件将被保存至一个由用户名字符串组成的一个目录结构下,比如用户名是abc,则被保存在了a/b/c/下,然后由图片的guid+size+.jpg组成一个文件名。
获取src地址的时候,通过实体->getIcon();方法来获取。getIcon是entities.php中的方法。然后这个方法会调用get_entity_icon_url方法,在get_entity_icon_url方法中有一行:
$url = trigger_plugin_hook('entity:icon:url', $entity->getType(), array('entity' => $entity, 'viewtype' => $viewtype, 'size' => $size), $url);
它会触发一个钩子(hook),这个hood需要在插件的start.php中注册。注册时这样写:
register_plugin_hook('entity:icon:url', 'object', 'activity_activityicon_hook');
第一个参数是钩子类型,第二个是实体类型,也就是activity的类型,第三个是钩子函数名。
然后在start.php中写出activity_activityicon_hook方法:
1/**
2 * 获取图标
3 * This hooks into the getIcon API and provides nice user icons for users where possible.
4 *
5 * @param string $hook 钩子名
6 * @param string $entity_type 实体类型
7 * @param string $returnvalue 图片url地址
8 * @param unknow $params 参数表列
9 * @return string $url 图片url地址
10 */
11
12 function activity_activityicon_hook($hook, $entity_type, $returnvalue, $params) {
13 global $CONFIG;
14
15 if ((!$returnvalue) && ($hook == 'entity:icon:url') && ($params['entity'] instanceof Activity)) {
16 $entity = $params['entity'];
17 $type = $entity->type;
18 $viewtype = $params['viewtype'];
19 $size = $params['size'];
20
21 if ($icontime = $entity->icontime) {
22 $icontime = "{$icontime}";
23 } else {
24 $icontime = "default";
25 }
26
27 $filehandler = new ElggFile();
28 $filehandler->owner_guid = $entity->owner_guid;
29 $filehandler->setFilename("activity/" . $entity->guid . $size . ".jpg");
30
31 if ($filehandler->exists()) {
32 $url = $CONFIG->url . "pg/activityicon/{$entity->guid}/$size/$icontime.jpg";
33 return $url;
34 }
35 }
36 }
这个方法会返回一个url,这个url就是src的地址。url返回到get_entity_icon_url后,会根据图片尺寸继续加工,返回最终url。这样就获取到了src地址。
浙公网安备 33010602011771号