Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8394319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:01:40+00:00 2026-06-09T20:01:40+00:00

I’m trying to create a dynamic image generator that also caches the images. Everything

  • 0

I’m trying to create a dynamic image generator that also caches the images.

Everything works perfectly, except that for some reason when I save an images and try to display it at the same time, it shows a broken image icon (like: http://cl.ly/image/3K1f0R0n1R0U).

Code sample:

// some string parsing junk happens up here, but removing that didn't change 
// what I've been having a problem with

header("Content-type: image/png;");

if(file_exists($fullFileName)){ // serve the file if it already exists
    ob_clean();
    flush();
    readfile($fullFileName);
    exit;
} else { // create the file if it doesn't exist
    $my_img = imagecreate(200, 80);
    $background = imagecolorallocatealpha($my_img, 0, 0, 0, 127);
    $line_colour = imagecolorallocatealpha($my_img, $color["r"], $color["g"], $color["b"], $color["a"]);

    imagesetthickness($my_img, 1);
    imageline($my_img, 30, 45, 165, 45, $line_colour);

    //////////////////////////////////////////
    // the line that's causing my troubles: //
    //////////////////////////////////////////
    imagepng($my_img, $fullFileName);


    imagecolordeallocate($line_color);
    imagecolordeallocate($background);
    imagedestroy($my_img);
}

So, the images are created just fine, save just fine, and display exactly like I want when I refresh the page (because it grabs the saved file).

If I switch that darn line above to:

imagepng($my_img);

Then the image shows up fine, but it doesn’t save (since I’m not telling it to).

I thought that this topic:

PHP echoing jpeg image fails, but writing same image to file works great. Why?

Sounded similar, but removing the trailing white space still didn’t change what’s happening. Other threads mentioned it could be a problem with other content being sent before the headers, but there is nothing like that in this file.

Any ideas on what could be going wrong? No errors are being logged, and I’m out of ideas.

Thanks!

-Zach

EDIT:

It was explained that I was destroying my image, which I missed. I ended up using this:

header("Content-type: image/png");

if(!file_exists($fullFileName)) {
    $my_img = imagecreate($dashWidth + $dashSpace, $height);
    $background = imagecolorallocatealpha($my_img, 0, 0, 0, 127);
    $line_colour = imagecolorallocatealpha($my_img, $color["r"], $color["g"], $color["b"], $color["a"]);

    imagesetthickness($my_img, 1);
    imageline($my_img, 0, $height-1, $dashWidth-1, $height-1, $line_colour);

    imagepng($my_img, $fullFileName);
    imagecolordeallocate($my_img, $line_color);
    imagecolordeallocate($my_img, $background);
}

ob_clean();
flush();
readfile($fullFileName);
exit;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:01:42+00:00Added an answer on June 9, 2026 at 8:01 pm

    you mean

    header("Content-type: image/png"); // without ;
    

    and also, you are destroying your image, read again the file

    readfile($fullFileName);
    

    because imagepng($my_img, $fullFileName); only saves it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.