I have a script I made which works a vast majority of the time. The script allows for multiple users to gather and upload images which changes a main image. Very occassionally (two times in 3 or 4 days of testing), the script will stop working because when GD2 tries to load the main image it gets "Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: IDAT: CRC error in..." Users are no longer able to change the image by uploading their own, and the main image is unrenderable when you try to view it. I know CRC stands for Cyclic Redundancy Check and looked up specifications on PNG's format and it looks like there is a chuck which is a CRC to verify the images data.

Anyway, I did some googling to see if there was a known issue and fix for the problem and I always run into dead ends. Either my googling skills need to be sharpened or this is an unavoidable and catastrophic error. Since I doubt the latter is true, can anyone shed some light on this issue or give me some advice?

The best advice I have gotten (it was my own suggestion) so far is to not save the main image as a PNG - but I wanted to check in first.

    (almost) all files have a CRC, it is there to detect when a files has been corupted, rather like a parity check. Trouble is, you still may want to open the file even if it has become corrupted but the operating system refuses - the data up unto the error would stil be useable.

    Merging multiple images, interactive art, sounds cool. PNG is probably the best image format for this since it allows for transparency which jpeg does not. My guess is that if you broke the process down into individual merge and save the error would be less likely - if only we could elliminate all errors eh.

      This is precisely why I chose to use PNG - alpha layer and truecolor. Using GD I can always merge a non-alpha enabled image into a bitfield that supports transparency, so that actually isn't much of a problem. I have all of the image merging split into steps.

      I actually have never programmed in PHP before; my main language is ANSI C and I have dabbed in x86 Assembly. I found PHP to be so easy to work with - no type casting is needed: the language takes care of that and the feature is build in. With PHP I also only have to write code for one session and the parser takes care of the rest.

      Normally I program in lower level languages, so I would like to ask if PHP takes care of handles to files on its own. For example, if two people access the site at the same time and two sessions try to write to the image at the same time, will they both write data? Or will one session wait for the other to stop using the file before accessing it? I guess this is more of a GD question, because it would be library specific.

      If the answer to the first question is yes, then maybe two people are posting images simultaneously and the two instances of the script write to the main image at the same time and corrupt it? This would have to happen in a small window of time, which would explain why my script works most of the time.

      PS, thank you for your swift reply.

        Unfortunately, php does not automatically lock files so you will have to do that explicitly when writing, and check for locks before trying to open for writing.
        The function to use is flock and pay particular attention to the warnings regarding operating system etc.

          Thank you. I think that may have been the problem. I will look into the use of flock(). :-D

            Great, I think I fixed it, but it is a hard error to produce without traffic. My beta testers (friends) got sick of it (I think) and therefore I need traffic from elsewhere.

            The idea of the script is to emulate the game "Katamari Damacy" from Namco. If you have not played the game, you might not get this at all. Anyway, the URL is http://normlegaia.1111mb.com/about.php. If you come across any errors in the script, please notify me. I will check back. If this isn't the place to show off your work, ignore this please, and wipe the URL from your memory.

            The idea was mine and all of the code is mine except some of the Captcha image code (based on code from Simon Jarvis at http://www.white-hat-web-design.co.uk/articles/php-captcha.php). However, I changed the font, randomized the colors, changed the noise and added lines on top of the letters to thwart edge detection. Still, credit to Simon.

              Write a Reply...