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 369951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:58:15+00:00 2026-05-12T13:58:15+00:00

What I’m trying to do is simple and should be very straightforward in my

  • 0

What I’m trying to do is simple and should be very straightforward in my mind. The results have been less than encouraging. What I’m trying to do: I have a web page that receives base64-encoded PNG image data. I can and have successfully taken that data through the proper steps to create an image on the filesystem. Call this “Script A”:

$imageData = $_POST['png'];
$imageStr = base64_decode($imageData);
$image = imagecreatefromstring($imageStr);
imagepng($image, $somePath);

This works fine. But I now have a new requirement, which is to later read this image, base64-encode it, and post it to Script A which will in turn write it back to the filesystem, exactly as above. Call this second script, the one that reads the data and posts it back to Script A, “Script B”. It’s when Script A attempts to call imagepng on the data received from Script B that I’m getting my error:

imagepng(): supplied argument is not a valid Image resource

Still with me? Impressive. So what this all means to me is that there is something wrong with the way Script B is reading the .png data from the filesystem, encoding it, and posting it to Script A. It does so as follows (w/o error handling here in the interest of brevity, but no errors are occuring) —

// Read in image file, base64-encode
$fd = fopen($somePath, 'rb');
$size = filesize($somePath);
$data = fread($fd, $size);
fclose($fd);
$encoded = base64_encode($data);

It then posts to Script A using cURL, w/ “$encoded” as a param.

Now, I’ve found countless examples of this methodology on the ‘Net. I’m pretty sure it should work. It doesn’t. I’ve also tried using file_get_contents. Also tried using ‘imagecreatefrompng’ to read the image, then using output buffer control to capture the image as a stream and base64-encoding that…no luck. In the end I think the approach above is the simplest and most common.

Apologies if this post is too long and/or too hard to follow. Lots of moving parts is all. Any feedback would be greatly appreciated.

Cheers


UPDATE: per Jon Skeet’s excellent suggestion to examine the image string as I step through the code using MD5, I’ve narrowed down the problem – once I write the image to the filesystem in Script B, then read it back in, it has changed. So:

$imageStr = base64_decode($imageData);
$image = imagecreatefromstring($imageStr);
imagepng($image, $somePath);

…seems to manipulate the data, because when I read it back in w/ plain old fread(), the MD5 signature has changed. I think it’s the imagepng function, perhaps it adds metadata? The php docs aren’t clear about this. If this is the case, I think instead of reading the image back in w/ fread(), I would need to do the opposite of what I’m doing when I write it, something like:

$image = imagecreatefrompng($somePath);

…then figure out a way to get the image’s content directly. I can’t figure out how to do so though: it seems to require a function like “imagepng($image)”, w/o a path, to output the image’s content…and that’s what I don’t want to do.

At this point I think the question is either:

  1. how can i undo (or reverse) what imagepng seems to do, or
  2. how can i output an image resource’s contents directly
  • 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-05-12T13:58:15+00:00Added an answer on May 12, 2026 at 1:58 pm

    I suggest you experiment by taking the MD5 hash of the data on the file system that Script B is reading from, again after reading it, and again in Script A after decoding it. That should show you which point is changing the data.

    Just as a thought – is the data definitely valid on the file system that Script B is reading from?

    EDIT: Okay, so as per your edit it looks like using imagepng is changing the contents. Do you actually need to use imagepng at all? Are you really doing anything with the image? It sounds like you’re basically just trying to send a file from script B to script A… so I would do that without ever treating the file as anything other than opaque binary data.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
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
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
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
this is what i have right now Drawing an RSS feed into the php,

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.