搜尋
首頁php教程php手册elgg 获取文件图标地址的方法

elgg本身有一套模板系统,可以加载图标。但是如果自己写了新的模板,怎样获得img的src地址呢?

过程如下:
首先,实体保存的时候用这个方法(系统本身的):
比如有一个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地址。
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器