elgg 獲取文件圖標(biāo)地址的方法
更新時(shí)間:2010年03月20日 23:43:36 作者:
elgg本身有一套模板系統(tǒng),可以加載圖標(biāo)。但是如果自己寫了新的模板,怎樣獲得img的src地址呢?
過程如下:
首先,實(shí)體保存的時(shí)候用這個(gè)方法(系統(tǒng)本身的):
比如有一個(gè)Activity類,繼承自ElggObject,創(chuàng)建了一個(gè)它的實(shí)例 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();
}
}
這個(gè)過程后,文件將被保存至一個(gè)由用戶名字符串組成的一個(gè)目錄結(jié)構(gòu)下,比如用戶名是abc,則被保存在了a/b/c/下,然后由圖片的guid+size+.jpg組成一個(gè)文件名。
獲取src地址的時(shí)候,通過實(shí)體->getIcon();方法來獲取。getIcon是entities.php中的方法。然后這個(gè)方法會調(diào)用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);
它會觸發(fā)一個(gè)鉤子(hook),這個(gè)hood需要在插件的start.php中注冊。注冊時(shí)這樣寫:
register_plugin_hook('entity:icon:url', 'object', 'activity_activityicon_hook');
第一個(gè)參數(shù)是鉤子類型,第二個(gè)是實(shí)體類型,也就是activity的類型,第三個(gè)是鉤子函數(shù)名。
然后在start.php中寫出activity_activityicon_hook方法:
/**
* 獲取圖標(biāo)
* This hooks into the getIcon API and provides nice user icons for users where possible.
*
* @param string $hook 鉤子名
* @param string $entity_type 實(shí)體類型
* @param string $returnvalue 圖片url地址
* @param unknow $params 參數(shù)表列
* @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;
}
}
}
這個(gè)方法會返回一個(gè)url,這個(gè)url就是src的地址。url返回到get_entity_icon_url后,會根據(jù)圖片尺寸繼續(xù)加工,返回最終url。這樣就獲取到了src地址。
首先,實(shí)體保存的時(shí)候用這個(gè)方法(系統(tǒng)本身的):
比如有一個(gè)Activity類,繼承自ElggObject,創(chuàng)建了一個(gè)它的實(shí)例 activity,
復(fù)制代碼 代碼如下:
// 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();
}
}
這個(gè)過程后,文件將被保存至一個(gè)由用戶名字符串組成的一個(gè)目錄結(jié)構(gòu)下,比如用戶名是abc,則被保存在了a/b/c/下,然后由圖片的guid+size+.jpg組成一個(gè)文件名。
獲取src地址的時(shí)候,通過實(shí)體->getIcon();方法來獲取。getIcon是entities.php中的方法。然后這個(gè)方法會調(diào)用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);
它會觸發(fā)一個(gè)鉤子(hook),這個(gè)hood需要在插件的start.php中注冊。注冊時(shí)這樣寫:
register_plugin_hook('entity:icon:url', 'object', 'activity_activityicon_hook');
第一個(gè)參數(shù)是鉤子類型,第二個(gè)是實(shí)體類型,也就是activity的類型,第三個(gè)是鉤子函數(shù)名。
然后在start.php中寫出activity_activityicon_hook方法:
復(fù)制代碼 代碼如下:
/**
* 獲取圖標(biāo)
* This hooks into the getIcon API and provides nice user icons for users where possible.
*
* @param string $hook 鉤子名
* @param string $entity_type 實(shí)體類型
* @param string $returnvalue 圖片url地址
* @param unknow $params 參數(shù)表列
* @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;
}
}
}
這個(gè)方法會返回一個(gè)url,這個(gè)url就是src的地址。url返回到get_entity_icon_url后,會根據(jù)圖片尺寸繼續(xù)加工,返回最終url。這樣就獲取到了src地址。
相關(guān)文章
PHP利用正則表達(dá)式將相對路徑轉(zhuǎn)成絕對路徑的方法示例
這篇文章主要介紹了PHP利用正則表達(dá)式將相對路徑轉(zhuǎn)成絕對路徑的方法,文中給出了詳細(xì)的示例代碼,大家可以整合成一個(gè)方法,在需要的地方調(diào)用,非常的不錯(cuò)。需要的朋友們下面來一起看看吧。2017-02-02php cache類代碼(php數(shù)據(jù)緩存類)
php的執(zhí)行效率很高,速度很快,但是連接數(shù)據(jù)庫、查詢數(shù)據(jù)庫等還是比較耗時(shí)的。2010-04-04PHP 數(shù)據(jù)結(jié)構(gòu) 算法 三元組 Triplet
PHP 數(shù)據(jù)結(jié)構(gòu) 算法 三元組 Triplet,學(xué)習(xí)php的朋友可以參考下。2011-07-07使用VisualStudio開發(fā)php的圖文設(shè)置方法
早先在asp橫行的年代,php和asp一樣,大都都是html中夾雜代碼,說實(shí)話,這時(shí)候IDE的確用處不是很大,倒是類似于dw之類的設(shè)計(jì)器甚為上手。2010-08-08