1

I want to download the PNG image that my site creates. I want to be able to force download the image. I attempted an HTTP request to a PHP file outside of Joomla, but that did not work. How can I download the PNG image that I create?

From the controller of my component

$req = new HTTP_Request2($pth);
$req->setMethod(HTTP_Request2::METHOD_POST)
    ->addPostParameter('download_image', $data["select_image"]);
            
try
{
    $response = $req->send();
    if (200 == $response->getStatus()) 
    {
        $resp = $response->getBody();
        echo $resp;
    }
    else
    {
        $app->enqueueMessage('Unexpected HTTP status: ' . $response->getReasonPhrase());
    }
} 
catch (HTTP_Request2_Exception $e) 
{
    $app->enqueueMessage('Error: ' . $e->getMessage());
}

Code from the outside file

//code to create PNG image with location $image_png;
if (file_exists($image_png))
{
    ob_start();
    header_remove();
    header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
    header('Content-Type: image/png');
    header('Content-Disposition: attachment; filename="'.basename($image_png).'"');
    header('Content-Length: ' . filesize($image_png));
    ob_clean();
    flush();
    readfile($image_png);
}
5
  • Can you explain any of the symptoms? How does it not work?
    – mickmackusa
    Jun 21 at 23:53
  • You are not sending the headers in the controller. Also, if the images are on the same filesystem as Joomla, you don't need to make a HTTP request.
    – Sharky
    Jun 22 at 6:10
  • I get a white screen when the code is executed. The HTTP request reaches the file, and I am sure something is echoed back. If I use $app->enqueueMessage() on the HTTP response, I get the binary code.
    – rancper
    Jun 22 at 16:59
  • @Sharky What do you mean when you say not sending the headers in the controller?
    – rancper
    Jun 22 at 17:47
  • You need to set the same headers in the controller as you have in the remote file. You can probably get them from $response.
    – Sharky
    Jun 25 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.