Elggファイルアイコンアドレスの取得方法

4206 ワード

プロセスは次のとおりです.
まず、エンティティを保存するときにこの方法(システム自体):
たとえば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);
フックがトリガーされますプラグインのstart.phpに登録します.登録時に次のように書きます.
register_plugin_hook('entity:icon:url', 'object', 'activity_activityicon_hook');
1つ目のパラメータはフックタイプ、2つ目はエンティティタイプ、すなわちactivityタイプ、3つ目はフック関数名です.
そして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アドレスが取得されます.