PHP Image Generation - ERR_CONTENT_LENGTH_MISMATCH
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I am creating images dynamically with PHP, and when serving the images the image loads half way and then stops loading and the image is replaced by nothing (as if it was an unavaiable image), and on console I see this error:

net::ERR_CONTENT_LENGTH_MISMATCH

1) I added set_time_limit(60) to my PHP script, but it doesn't seem to do anything.

2) These are the headers that console is showing:
https://pastebin.com/S7HLLXLR

3) I have a cache system. Whenever the image is generated for the first time (fresh), this error occurs. Whenever the image is served from cache, this error does not occur.

The code logic is as follows:

If( cache not exists){
    createCache();
}

renderImageFromCacheFolder()`

This means that in both cases (wherever cache is fresh or not), the same output function is used.

4) This is my PHP code for renderImageFromCacheFolder:
https://pastebin.com/x7q02ukL

Please help me solve this bug! Thank you

Do you have your server access and error log that is generated when you request your url ? It may help to determine the root cause of your issue.
kerncy 1 year ago
Thanks Kerncy! I have just sent you a private message. Please check it.
georgefountain 1 year ago
Hey georgefountion, I think it a problem with your header('Content-Length: ' . filesize($filePath)) .More specifically the file path $filePath
Codeword 1 year ago
For experimentation just set Content-Length manually to a large value and see what happens header('Content-Length: 100000000000')
Codeword 1 year ago
More specifically set the content-length manually to the file size of the image, and if it works there is a problem with your filesize
Codeword 1 year ago
Thanks @CodeWord. I just tried adding $filePath = self::getImagesCacheFolderPath(). $fileName . ".". $fileExtension; echo $filePath; exit(); ..and the filepath is correct. It points to a real image.
georgefountain 1 year ago
I have updated my previous comment,Have you tried setting content-length manually to the file size of image?
Codeword 1 year ago
After this try to set zlib.output_compression =Off
Codeword 1 year ago
I think the error is beacuse the as zlib compression is on so the filesize of image outputed by browser is not same as the actual filesize as calculated by filesize function and so the Mismatch error.
Codeword 1 year ago
But then, how can you explain that the code returns no error when giving a cached image and when generating one it gives an error?
georgefountain 1 year ago
The mismatch error only occurs when requesting a non-cached image.
georgefountain 1 year ago
As you said, "code returns no error when giving a cached image and when generating one it gives an error". I think this may be explained like this when the first time the images loads, due to zlib compression file sent to the browser mismatch the actual file calculated by filesize function.But when the image is in cached then this piece of code runs if($notModified){ header("HTTP/1.1 304 Not Modified"); exit; } runs and the code exits before it can send the header this time and so the error doesn't occur.
Codeword 1 year ago
Can you verify that? I have just sent you a private message.
georgefountain 1 year ago
awarded to Codeword
Tags
PHP

Crowdsource coding tasks.

2 Solutions


Hi,

For a trial, you can add the function "clearstatcache();" just before the call to "header('Content-Length: ' . filesize($filePath));"

From what I saw, the filesize uses a cached version of the data stored. If the file already exists it may use the invalid length as described here : https://www.w3schools.com/php/func_filesystem_filesize.asp

Hi Kerncy! Thanks for your help. Codeword privately suggested me the same solution you did just hours before you posted. I just added clearstatcache() before calling filesize(). Could you double check if everythign works? If so, I will award you a TIP, since you both had the same solution. Thanks!
georgefountain 1 year ago
PLease double check if all is fine with the links I previously provided
georgefountain 1 year ago
Hi, I checked the link you gave me by private message, and it's ok for me. I tested a few different colors, and PNG always loads well in Firefox, Chrome and IE.
kerncy 1 year ago
Thanks so much kerncy! Tip awarded :)
georgefountain 1 year ago
Thank you for the tip !
kerncy 1 year ago
Winning solution

Hey georgefountain, I have tested just now.it's working correctly now.
Thank you

Thanks Goergefountain.
Codeword 1 year ago
View Timeline