Jump to content

Header("Content-Type: image/PNG") Showing broken image


Go to solution Solved by gizmola,

Recommended Posts

Hey everyone,

 

I am trying to load an image that I created from a string. It worked great on my staging site, but now that it's on my live site, it isn't working.

 

Both pages are on php8.5

<?php
	$kcal = 500;
   	require_once("fpdf.php");
    class PDF extends FPDF{
        // Simple table
        function BasicTable($header, $data, $kcal){
            // Header
            $first = 45;
            foreach($header as $col){
                $v = is_numeric($col) ? round($col) : $col;
                $this->Cell($first,7,$v,1,0,'C');
                $first = 8;
            }
            $this->Ln();

            // Data
            foreach($data as $row){
                $first = 45;
                foreach($row as $col){
                    $v = (is_numeric($col)) ? near25($col/$kcal/112) : $col;
                    $this->Cell($first,7,$v,1,0,'C');
                    $first = 8;
                }
                $this->Ln();
            } 
        }
    }

    $header = json_decode('["Category class)","1","1.5","2","2.5","3","3.5","4","4.5","5","5.5","6","6.5","7"]');
    $row = json_decode('[["Activity",130,150,170,190,210,230,250,270,290,310,330,350,370]]');

    // Data loading
    $pdf = new PDF();
    $pdf->SetFont('Arial','',6);
    $pdf->AddPage();
    $pdf->BasicTable($header,$row, $kcal);
    $output = $pdf->Output('S');

    $im = new imagick();
    $im->setResolution(300, 300);
    $im->readImageBlob($output);
    $im->setImageFormat('png');
    $im->setImageCompression(imagick::COMPRESSION_JPEG); 
    $im->setImageCompressionQuality(100);

    $im->borderImage("#ffffff", 20, 20);
    $im->trimImage(0.3);
    $im->setImagePage($im->getImageWidth(), $im->getImageHeight(), 0, 0);

    header("Content-Type: image/" . $im->getImageFormat());
    $im->getImageBlob();
    
    $im->clear();
    $im->destroy();
?>

When I run the script I get a broken image icon. My first thought was that there is an issue creating the image. However, if I switch the following, it shows the image is made properly.

 

<?php
	//header("Content-Type: image/" . $im->getImageFormat());
    //echo $im->getImageBlob();
    ?><img src="data:image/png;base64,<?php echo base64_encode($im->getImageBlob()); ?>" alt="" /><?
?>
    

Any ideas?

  • Solution

This is some confusing code -- you are creating a pdf purely to then create an image out of it.  Since you are throwing away the pdf, I have to think there's a more direct way to do this, either with imagemagick directly or using some existing library designed for making graphs and charts.  

With that said, if it works on one machine and not the other, and everything else is the same, then you have to consider the environment settings and what might be different (error levels, permissions, etc.).  A few comments:

  • It's best not to have a php close tag in your scripts. 
  • This and other recommended coding standards are documented here:  https://www.php-fig.org/per/coding-style/
  • I bring this up because whenever you are returning output, a closing tag with whitespace following can lead to to some strange and confusing behavior, and has long been one of the reasons people encounter the "output already sent" message.
  • There is also little to no reason to have code that is trying to clean up resources -- especially in a script like this, when php garbage collects everything when the script ends.

Having a script that defines a class only to use it in the same script in which it was defined is an anti pattern, you should try and avoid. 

PHP also has namespaces, composer as both a dependency manager and autoload builder.  You are using php 8.5, yet creating and writing code the way people did it 15 years ago.

On 1/29/2026 at 3:16 PM, gizmola said:

This is some confusing code -- you are creating a pdf purely to then create an image out of it.  Since you are throwing away the pdf, I have to think there's a more direct way to do this, either with imagemagick directly or using some existing library designed for making graphs and charts.  

I agree. I am open to simpler solutions if you have any. At the time, this was the only way I could figure out how to make it work.

On 1/29/2026 at 3:16 PM, gizmola said:

With that said, if it works on one machine and not the other, and everything else is the same, then you have to consider the environment settings and what might be different (error levels, permissions, etc.).  A few comments:

  • It's best not to have a php close tag in your scripts. 
  • This and other recommended coding standards are documented here:  https://www.php-fig.org/per/coding-style/
  • I bring this up because whenever you are returning output, a closing tag with whitespace following can lead to to some strange and confusing behavior, and has long been one of the reasons people encounter the "output already sent" message.
  • There is also little to no reason to have code that is trying to clean up resources -- especially in a script like this, when php garbage collects everything when the script ends.

This ended up fixing the issue. It was caused by white space being added after a ?> tag. It never occurred to me that I could leave those out altogether.

 

On 1/29/2026 at 3:16 PM, gizmola said:

Having a script that defines a class only to use it in the same script in which it was defined is an anti pattern, you should try and avoid. 

PHP also has namespaces, composer as both a dependency manager and autoload builder.  You are using php 8.5, yet creating and writing code the way people did it 15 years ago.

I learned PHP 20 years ago, maybe more. I only use it when I have specific problems I think it can solve. So it makes sense that I am out of date. Thanks for the resources; I am sure they will be a big help.

 

I also removed the class and replaced it with a function. in hindsight that makes more sense.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.