0

I have worked out how to use dynamic routing and JsonResponse() in a custom controller to output pure JSON, with no HTML at all. I would like to do the same with an image — for the page to output an image header and the image itself to the browser, with no HTML, as though the address were simply a file path to an image.

Is this possible?

closed as too broad by kiamlaluno Jul 13 at 5:01

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Can you give an example URL and output you expect ? – GiorgosK Mar 22 at 15:10
  • @GiorgosK — any URL, really, since I can control that in my router. Currently I'm using /<modulename>/image/<imagepath> — and the output I want is just the image. But I think I've solved it. – Spunto the Rat Boy Mar 26 at 15:30
1

Something like the following could help you.

Just Set $filepath and $filename with your desired values.

$filepath = "/path/to/my/image/chart.png";
$filename = "chart.png";

$response = new Response();
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $filename);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', 'image/png');
$response->setContent(file_get_contents($filepath));

return $response;
  • Thanks very much @Yusuf — this looks as though it would work, but unfortunately it doesn't because of the curcumstance which I forgot to mention in my badly-worded original question, and that is that the whole reason I'm doing this, rather than just outputting an img tag with the real path in the src attribute, is that the server where the images are kept is firewalled and only the web server can get through. So image requests from a user's browser, which your otherwise excellent solution ends up with, won't work. I'm going to post my working code as an answer, since it won't fit here. – Spunto the Rat Boy Mar 26 at 15:41
0

I suspect the reason my original approach failed was a typo, or something similarly trivial, and being such a Drupal noob I assumed an entirely different kind of error. @Yusuf's answer is neater by far than what I ended up with, but doesn't work for me for reasons I explain in a comment. Here is what I had to do — less elegant, but also more universal: $im is passed to the controller in the URL:

    $impath.='http://<IMAGE DOMAIN HERE>/media/'.$im;
    $ext = pathinfo( $im, PATHINFO_EXTENSION );

    switch( strtolower($ext) ){
        case 'jpeg':
        case 'jpg':
            $im = imagecreatefromjpeg( $impath );
            if ( $im ){
                header('Content-Type: image/jpeg');
                imagejpeg( $im );
                imagedestroy( $im );
            } else {
                echo '<p>'.basename(__FILE__).'#'.__LINE__.'<br />$impath = '.$impath.'<br />$ext = '.$ext.'</p>';
            }
            break;
        case 'png':
            $im = imagecreatefrompng( $impath );
            if ( $im ){
                header('Content-Type: image/png');
                imagepng( $im );
                imagedestroy( $im );
            } else {
                echo '<p>'.basename(__FILE__).'#'.__LINE__.'<br />$impath = '.$impath.'<br />$ext = '.$ext.'</p>';
            }
            break;
        case 'gif':
            $im = imagecreatefromgif( $impath );
            if ( $im ){
                header('Content-Type: image/gif');
                imagegif( $im );
                imagedestroy( $im );
            } else {
                echo '<p>'.basename(__FILE__).'#'.__LINE__.'<br />$impath = '.$impath.'<br />$ext = '.$ext.'</p>';
            }
            break;
        default:
            echo '<p>'.basename(__FILE__).'#'.__LINE__.'<br />$impath = '.$impath.'<br />$ext = '.$ext.'</p>';
            break;
    }

    exit;

I'm not sure what to do about accepting an answer — I suspect I shouldn't accept @Yusuf's since it didn't actually solve my problem and consequently I haven't seen it work.

Not the answer you're looking for? Browse other questions tagged or ask your own question.