The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

How to echo images from slim framework

abhinavother's Avatar

abhinavother

28 Aug, 2015 03:36 PM

I am changing the way i deliver images . Instead of directly from folders, I am trying to create routes for them.

I intent to use it as <img src="/assets/200/400/test/png" /> But i keep getting 404 error.

$app->get('/assets/:height/:width/:id/:type', function() use ($app) {
    header('Content-type: image/jpeg');
    $dir = dirname(__DIR__)."/Resources/Images/";
    $image = new Imagick($dir.$id.'.svg');
    /*
        Code to determine browser
        Code to create images based on height, width, and change its format {svg to png} as required
    */
        $app->response->header('Content-Type', 'content-type: image/'.$type );

     //  echo $image;
    // $res->body($image);
});

I already tried
http://help.slimframework.com/discussions/questions/93-how-get-imag...
http://help.slimframework.com/discussions/questions/674-best-practi...
http://help.slimframework.com/discussions/questions/359-file-download
http://stackoverflow.com/questions/20439144/php-how-to-use-a-rest-c...

and few other. I also found https://github.com/tuupola/slim-image-resize but I need to use SVG Conversions and few other stuffs which it does not have.

Any help would on how to forward/echo/send images or other non-text assets to the browserbe

  1. 1 Posted by nirajpradhan13 on 30 Aug, 2015 06:00 AM

    nirajpradhan13's Avatar

    $image = 'image.png';
    $imageData = base64_encode(file_get_contents($image));
    $src = 'data: '.mime_content_type($image).';base64,'.$imageData;
    And finally
    $res->body($src);

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac