How to get correct location url from ElggFile to show in image tags

Hello! Once again I seek your wisdom. I have successfully uploaded images to my data folder. Now I am trying to view the images, but having problems with geting the right url from my file entity. The location is stored correct, but only the last part of the url. example: onegai_media/date.image.png

My whole url to the image looks like this: http://localhost/sites/elgg/onegai_media/date.image.jpg This is obviously not the right location T-T

I dont need any other sizes of the image in the begining, thats why i have no thumbnail.php file, like the file plugin. Can someone post a simple example on how to get the right location of a stored file ready for display, please?? I have browsed through the code of the file plugin, cant find my failure and am now a little bit confused...

  • look at the thumbnail.php file (which is the way elgg show imgs) and try to modify it

  • I have, the result is the following: ����JFIF��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 90 ��C ��C ��HX"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%� and so on.

    All they do in the thumbnail.php file is finding out, which size should be displayed. After that they create a new ElggFile and grab the picture like largethumb. This is stored in a $contents attribute and this is how my $contents attribute looks like... This is how i create it:

    $readfile = new ElggFile();
    $readfile->owner_guid = $media->owner_guid;
    $readfile->setFilename($media->largethumb);
    $contents = $readfile->grabFile();

    I cant call the standard ElggObject functions on that attribute, have tried getURL() and got an error

     

  • ahhhhh you forgot the header... your php find an img and return it to the browser, but the browser interprets it as plain text, so you need to specify the content type of the response... The page that return all that nasty stuff need to return only an img? if that is the case then add these line before to echo the img:

    header("Content-type: $mime"); // MIME type

    header('Expires: ' . date('r',time() + 864000)); //cache expiration in 10days

    header("Pragma: public", true);

    header("Cache-Control: public", true);

    header("Content-Length: " . strlen($contents));

    the variable $mime should be computed dinamically since you could have different file type, everyway if you're going to use only img, you can set it statically (not recommended!). $contents is the var you echo

    for example the thumbnail.php use:

    $file = get_entity($file_guid);

    $mime = $file->getMimeType();

  • Hello, thank you for the explanation. But it still looks like before. Here is what my code looks like:

    $readfile = new ElggFile();
        $readfile->owner_guid = $media->owner_guid;
        $readfile->setFilename($media->largethumb);
        $contents = $readfile->grabFile();
        $mime = $media->mimetype;
       
        header("Content-type: $mime"); // MIME type
        header('Expires: ' . date('r',time() + 864000)); //cache expiration in 10days
        header("Pragma: public", true);
        header("Cache-Control: public", true);
        header("Content-Length: " . strlen($contents));
       

        $image_url = $contents;
        $image_url = elgg_format_url($image_url);

        echo <<<HTML
            <div class="file-photo">
                <img class="elgg-photo" src="$image_url" /></a>
            </div>
    HTML;

     

    The MimeType is stored in Metadata, because i got an error when calling the function on my file. Fatal error: Call to undefined method ElggObject::getMimeType() Might this be the reason? It seems that my $media is not an ElggFile Object

  • well, the fatal error is due to the engine that has not been called, so add this line at the beginning

    require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");

    then there is a problem with the code...the var $content contains an image file not the url to that file! the code i provided to you (that is no more than the thumbnail.php code...) works if you echo the variable $content, so you should move the html part containing the div and img elements somewhere else and the code up to the header functions (plus the line 'echo $contents') is a code to get img files. When you visit in the browser that php page is something like to visit http://mysite.it/photo_123.jpg, so if you call that page http://myelgg.com/pg/get_generic_file.php?guid=123, you should write in another php file this:

    <div class="file-photo">
                <img class="elgg-photo" src="http://myelgg.com/pg/get_generic_file.php?guid=123" /></a>
    </div>
  • Okay i will try this, thank you.

  • Hello again. I have tried to create a new view and copy as much as i could from the file plugin. Now i dont get any errors, but i also get no image. This is how my new view looks like:

    <?php
    $image_url = elgg_get_site_url() . "mod/onegia/thumbnail.php?file_guid={$vars['entity']->getGUID()}&size=large";
    $image_url = elgg_format_url($image_url);

    if ($vars['full_view']) {
        echo <<<HTML
            <div class="file-photo">
                <img class="elgg-photo" src="$image_url" /></a>
            </div>
    HTML;
    }

    This is how my thumbnail.php looks like:

    <?php
    /**
     * Elgg file thumbnail
     *
     * @package ElggFile
     */

    // Get engine
    require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");

    // Get file GUID
    $file_guid = (int) get_input('file_guid', 0);

    // Get file thumbnail size
    $size = get_input('size', 'small');
    $file = get_entity($file_guid);

    $thumbfile = $file->largethumb;
    // Grab the file
    $readfile = new ElggFile();
    $readfile->owner_guid = $file->owner_guid;
    $readfile->setFilename($thumbfile);
    $mime = $file->getMimeType();
    $contents = $readfile->grabFile();

    // caching images for 10 days
    header("Content-type: $mime");
    header('Expires: ' . date('r',time() + 864000));
    header("Pragma: public", true);
    header("Cache-Control: public", true);
    header("Content-Length: " . strlen($contents));

    echo $contents;

    and this is how i call the view and display the content:

    if ($full){

        if (elgg_view_exists("image/image_view")) {
            $image_view = elgg_view("image/image_view", $vars);
        }
        echo <<<HTML

    <div class="file elgg-content">
    $body
    $image_view
    </div>
    HTML;

     

    Its like my $contents object is empty or i am displaying it wrong. Need your help once again Tango...

  • If i move the content of the thumbnail.php to my new created view and 'echo $contents' i get the result from before (����JFIF��...), so the $contents object is not empty. Displaying it seems to be what i am doing wrong...

  • I have just tried to display this simple example from php.net:

    $im = imagecreatetruecolor(120, 20);
    $text_color = imagecolorallocate($im, 233, 14, 91);
    imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

    // Set the content type header - in this case image/jpeg
    header('Content-Type: image/jpeg');

    // Output the image
    imagejpeg($im);

    // Free up memory
    imagedestroy($im);

     

    It should display a red string on a black background, but it does the same like before (����JFIF�). Maybe i have forgot a major setup in the start.php???

  • well, what you've done seems to be ok... i suppose that the view image/image_view is this

    <?php
    $image_url = elgg_get_site_url() . "mod/onegia/thumbnail.php?file_guid={$vars['entity']->getGUID()}&size=large";
    $image_url = elgg_format_url($image_url);

    if ($vars['full_view']) {
        echo <<<HTML
            <div class="file-photo">
                <img class="elgg-photo" src="$image_url" /></a>
            </div>
    HTML;
    }

    the header functions are placed in the right place! please could you tell me what happen when you use your code? the output (html) should be:

    <div class="file elgg-content">
    $body
            <div class="file-photo">
                <img class="elgg-photo" src="http://....mod/onegia/thumbnail.php?file_guid=xx&size=large"/>&nbsp;                

            </div>

    </div>

    Is what you get? what do you mean for 'I get no image'? you see a not found img (the strange default img when the img is not loaded)? or nothing?

    If you're using chrome use the console (Ctrl+Shift+J) and look at the 'Network' tab to check page loading...or use other debugger...

    Everyway try to visit the upgrade.php page to rebuild elgg cache, and force the refresh (generally Ctrl+Shift+R) so you'll overwrite browser cache.

    Everyway for all the trials you can visit directly the img url, instead to use views and img elements..

    PS: are you using ajax to get the img? in this case you must specify what response do you expect...