手机
当前位置:查字典教程网 >编程开发 >php教程 >elgg 获取文件图标地址的方法
elgg 获取文件图标地址的方法
摘要:过程如下:首先,实体保存的时候用这个方法(系统本身的):比如有一个Activity类,继承自ElggObject,创建了一个它的实例acti...

过程如下:

首先,实体保存的时候用这个方法(系统本身的):

比如有一个Activity类,继承自ElggObject,创建了一个它的实例 activity,

复制代码 代码如下:

// 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地址。

【elgg 获取文件图标地址的方法】相关文章:

php实现修改新闻时删除图片的方法

Yii获取当前url和域名的方法

PHP文件操作方法汇总

php将图片文件转换成二进制输出的方法

PHP获取文件行数的方法

php将12小时制转换成24小时制的方法

php对文件进行hash运算的方法

php生成圆角图片的方法

php生成图片缩略图的方法

php实现将上传word文件转为html的方法

精品推荐
分类导航