如何将图像图标添加到Elgg实体

时间:2016-05-07 15:35:48

标签: php file elgg

Elgg Version 2.0

如何创建带图像图标的Elgg实体,就像创建Elgg组时的做法一样?

我已尝试实施以下内容,但未上传任何图片:

操作文件:

<?php
// get the form inputs
$title = get_input('title');
$body = get_input('body');
$tags = string_to_tag_array(get_input('tags'));

// create a new my_blog object
$blog = new ElggObject();
$blog->subtype = "nganya";
$blog->title = $title;
$blog->description = $body;

// for now make all my_blog posts public
$blog->access_id = ACCESS_PUBLIC;

// owner is logged in user
$blog->owner_guid = elgg_get_logged_in_user_guid();

// save tags as metadata
$blog->tags = $tags;

// save to database and get id of the new my_blog
$blog_guid = $blog->save();

// if the my_blog was saved, we want to display the new post
// otherwise, we want to register an error and forward back to the form
if ($blog_guid) {
    system_message("Your nganya post was saved >>>>>>>>>> " . $blog_guid);
    forward($blog->getURL());
} else {
    register_error("The nganya post could not be saved");
   forward(REFERER); // REFERER is a global variable that defines the previous page
}

$has_uploaded_icon = (!empty($_FILES['icon']['type']) && substr_count($_FILES['icon']['type'], 'image/'));

if ($has_uploaded_icon) {

    $icon_sizes = elgg_get_config('icon_sizes');

    $prefix = "nganya/" . $blog->guid;

    $filehandler = new ElggFile();
    $filehandler->owner_guid = $blog_guid;
    $filehandler->setFilename($prefix . ".jpg");
    $filehandler->open("write");
    $filehandler->write(get_uploaded_file('icon'));
    $filehandler->close();
    $filename = $filehandler->getFilenameOnFilestore();

    $sizes = array('tiny', 'small', 'medium', 'large', 'master');

    $thumbs = array();
    foreach ($sizes as $size) {
        $thumbs[$size] = get_resized_image_from_existing_file(
            $filename,
            $icon_sizes[$size]['w'],
            $icon_sizes[$size]['h'],
            $icon_sizes[$size]['square']
            );
    }

    if ($thumbs['tiny']) { // just checking if resize successful
        $thumb = new ElggFile();
        $thumb->owner_guid = $blog->owner_guid;
        $thumb->setMimeType('image/jpeg');

        foreach ($sizes as $size) {
            $thumb->setFilename("{$prefix}{$size}.jpg");
            $thumb->open("write");
            $thumb->write($thumbs[$size]);
            $thumb->close();
        }

        $blog->icontime = time();
    }
}

表格:

<div>
    <label><?php echo elgg_echo("groups:icon"); ?></label><br />
    <?php echo elgg_view("input/file", array("name" => "icon")); ?>
</div>

<div>
    <label for="title"><?= elgg_echo("title"); ?></label><br />
    <?= elgg_view('input/text', ['name' => 'title', 'id' => 'title']); ?>
</div>

<div>
    <label for="body"><?= elgg_echo("body"); ?></label><br />
    <?= elgg_view('input/longtext', ['name' => 'body', 'id' => 'body']); ?>
</div>

<div>
    <label for="tags"><?= elgg_echo("tags"); ?></label><br />
    <?= elgg_view('input/tags', ['name' => 'tags', 'id' => 'tags']); ?>
</div>

<div>
    <?= elgg_view('input/submit', ['value' => elgg_echo('save')]); ?>
</div>

1 个答案:

答案 0 :(得分:1)

操作代码看起来正确。在Elgg 2.2中,这将更容易,但与此同时:

  1. 您需要为其添加插件挂钩处理程序 'entity:icon:url',<entity_type>挂钩并替换URL if $params['entity']是您要添加的子类型
  2. 更新您的页面处理程序以提供上述网址的文件。