时间:2021-07-01 10:21:17 帮助过:31人阅读
// 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();
}
}
这个过程后,文件将被保存至一个由用户名字符串组成的一个目录结构下,比如用户名是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方法:
代码如下:
/**
* 获取图标
* This hooks into the getIcon API and provides nice user icons for users where possible.
*
* @param string $hook 钩子名
* @param string $entity_type 实体类型
* @param string $returnvalue 图片url地址
* @param unknow $params 参数表列
* @return string $url 图片url地址
*/
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;
}
}
}
这个方法会返回一个url,这个url就是src的地址。url返回到get_entity_icon_url后,会根据图片尺寸继续加工,返回最终url。这样就获取到了src地址。