douzhangjian1505
douzhangjian1505
2018-04-17 07:28
浏览 44
已采纳

使用SLIM Framework v3显示SVG

I am trying to show images using Slim micro framework v3 without result.

Firstly of all, I am new in Slim so I don't know what is the best pratice to show images using that framework.

So, for now I am trying with this:

$app->get('/images/resources/{resource}', function (Request $request, Response $response, array $args) {
    $resource = "./images/resources/".$args['resource'];
    $this->logger->info("requested imaged: <".$resource.">");
    if(file_exists($resource))
        $this->logger->info("image found in <".$resource.">");
    else
    {
        $this->logger->info("image NOT found in <".$resource.">");
        return;
    }
    $image = file_get_contents($resource);
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $response->withHeader('Content-Type', 'content-type: ' . $finfo->buffer($image));
    echo $image;
});

This works not completely, for example, the loggers report me that all images exist, but in the HTML page I see only PNGs and any SVGs:

enter image description here

The HTML code relative to the screenshot is:

<div class="row text-center" id="downloadApps">
    <div class="col-3"><a href="https://www.microsoft.com/store/apps/mybeautifulapp=badge"><img
                    class="downloadButton"
                    src="https://assets.windowsphone.com/13484911-a6ab-4170-8b7e-795c1e8b4165/English_get_L_InvariantCulture_Default.png"
                    alt="Get"/></a></div>
    <div class="col-3"><img class="downloadButton" src="/images/resources/google_play_store.png"
                            alt="Get"/></div>
    <div class="col-3"><img class="downloadButton" src="/images/resources/ios_app_store.svg"
                            alt="Get"/></div>
    <div class="col-3"><img class="downloadButton" src="/images/resources/mac_app_store.svg"
                            alt="Get"/></div>
</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqu8828
    douqu8828 2018-04-17 07:34
    已采纳

    I implemented this functionality for my project and it's working fine.

    Please check the code below,

    $app->get('/image/p/{data:\w+}', function($request, $response, $args) {
    $data = $args['data'];
    $image = @file_get_contents("http://localhost/main/media/image/p/$data");
    if($image === FALSE) {
        $handler = $this->notFoundHandler;
        return $handler($request, $response);    
    }
    
    $response->write($image);
    return $response->withHeader('Content-Type', FILEINFO_MIME_TYPE);
    });
    
    点赞 评论

相关推荐