You wrote
The data block is not valid image data.
"image data" - but in your code you call fgets() to read that data. This is wrong, since the image is binary data, not a text file, so you do not want it to read it line by line ( docs ):
fgets - gets the string from file pointer
This means that fgets() stop reading from the file as soon as it detects that it considers the end-of-line marker, which usually means stopping earlier and reading less than $length , since there is a fairly low chance that such a byte is not in binary sequence.
So fgets() wrong method to use, and this is the main problem. Instead, you should choose a less clever fread() (which doesn't know about lines, etc. And just reads what you said). Finally, you should fclose() process when done. And, of course, you should always check for errors starting with fopen() :
if ($handle = fopen($binaryFile, 'rb')) { if (fseek($handle, $offset) === 0) { $imageData = fread($handle, $length); if ($imageData === false) { // error handling - failed to read the data } } else { // error handling - seek failed } fclose($handle); } else { // error handling - can't open file }
Therefore, always use the right tool for the task, and if you do not know what the given method / function does , there is always a good documentation to look.
source share