elgg 获取文件图标地址的方法


Posted in PHP onMarch 20, 2010

过程如下:
首先,实体保存的时候用这个方法(系统本身的):
比如有一个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地址。
PHP 相关文章推荐
第十二节 类的自动加载 [12]
Oct 09 PHP
社区(php&&mysql)五
Oct 09 PHP
phpmyadmin的#1251问题
Nov 25 PHP
php设计模式 Delegation(委托模式)
Jun 26 PHP
php cli换行示例
Apr 22 PHP
CodeIgniter中实现泛域名解析
Jul 19 PHP
php判断并删除空目录及空子目录的方法
Feb 11 PHP
PHP 验证登陆类分享
Mar 13 PHP
codeigniter中实现一次性加载多个view的方法
Mar 20 PHP
PHP SPL标准库之SplFixedArray使用实例
May 12 PHP
浅析Laravel5中队列的配置及使用
Aug 04 PHP
php实现用户登陆简单实例
Apr 04 PHP
PHP 解决utf-8和gb2312编码转换问题
Mar 18 #PHP
ecshop 订单确认中显示省市地址信息的方法
Mar 15 #PHP
php smarty函数扩展
Mar 15 #PHP
php Smarty date_format [格式化时间日期]
Mar 15 #PHP
libmysql.dll与php.ini是否真的要拷贝到c:\windows目录下呢
Mar 15 #PHP
php下获取客户端ip地址的函数
Mar 15 #PHP
PHP 模拟$_PUT实现代码
Mar 15 #PHP
You might like
获取远程文件大小的php函数
2010/01/11 PHP
PHP 多维数组的排序问题 根据二维数组中某个项排序
2011/11/09 PHP
PHP中加速、缓存扩展的区别和作用详解(eAccelerator、memcached、xcache、APC )
2016/07/09 PHP
Javascript弹出窗口的各种方法总结
2013/11/11 Javascript
js函数调用的方式
2014/05/06 Javascript
JavaScript中的null和undefined区别介绍
2015/01/01 Javascript
基于豆瓣API+Angular开发的web App
2015/01/02 Javascript
javascript中Array()数组函数详解
2015/08/23 Javascript
jquery validate和jquery form 插件组合实现验证表单后AJAX提交
2015/08/26 Javascript
JavaScript+html5 canvas绘制缤纷多彩的三角形效果完整实例
2016/01/26 Javascript
jquery结合html实现中英文页面切换
2016/11/29 Javascript
vue input 输入校验字母数字组合且长度小于30的实现代码
2018/05/16 Javascript
微信小程序之批量上传并压缩图片的实例代码
2018/07/05 Javascript
JavaScript实现点击出现图片并统计点击次数功能示例
2018/07/23 Javascript
微信小程序实现发送验证码按钮效果
2018/12/20 Javascript
layui table 表格上添加日期控件的两种方法
2019/09/28 Javascript
Vue使用虚拟dom进行渲染view的方法
2019/12/26 Javascript
Vue+Element实现网页版个人简历系统(推荐)
2019/12/31 Javascript
详解ES6实现类的私有变量的几种写法
2021/02/10 Javascript
[01:07:19]DOTA2-DPC中国联赛 正赛 CDEC vs XG BO3 第一场 1月19日
2021/03/11 DOTA
python的描述符(descriptor)、装饰器(property)造成的一个无限递归问题分享
2014/07/09 Python
Python的Django中django-userena组件的简单使用教程
2015/05/30 Python
在Ubuntu系统下安装使用Python的GUI工具wxPython
2016/02/18 Python
python实现稀疏矩阵示例代码
2017/06/09 Python
Pandas库之DataFrame使用的学习笔记
2019/06/21 Python
对Python3之方法的覆盖与super函数详解
2019/06/26 Python
python中for循环把字符串或者字典添加到列表的方法
2019/07/20 Python
浅谈对pytroch中torch.autograd.backward的思考
2019/12/27 Python
在pycharm中为项目导入anacodna环境的操作方法
2020/02/12 Python
解决Django提交表单报错:CSRF token missing or incorrect的问题
2020/03/13 Python
jupyter notebook tensorflow打印device信息实例
2020/04/20 Python
python 实用工具状态机transitions
2020/11/21 Python
英国领先的家庭时尚品牌:Peacocks
2018/01/11 全球购物
霸王洗发水广告词
2014/03/14 职场文书
大型演出策划方案
2014/05/28 职场文书
全民创业工作总结
2015/08/13 职场文书