The message "The image cannot be displayed because it contains errors" is produced by the browser. The header of the response it received from the server says that the body of the response should be interpreted as a JPEG image (image/jpeg).
The format of the JPEG files (as most binary formats) starts with a header that contains a "signature" (a sequence of bytes used to identify the file as JPEG), image information (width, height), content length etc.
If the script produces a warning or a notice during execution, this extra content is received by the browser before the bytes of the image file (they are generated by the very last line of code), the browser cannot not find the JPEG signature and reports the error above (and it is right).
How to debug it
Remove/comment the header() line or replace it temporarily with:
header('Content-type: text/plain');
This change will stop telling the browser to interpret the content as an image; it will display it as is and you can inspect it and see the PHP warnings and notices, if there is any.
After you find and solve the issues and the code does not produce error messages anymore put the original header line back and it should work.
Other possible cause: the BOM
If the change explained above does not reveal any message produced by the code and the browser still complains about errors in the received content then make sure the PHP file is NOT saved as UTF-8 with BOM. UTF-8 is fine, the BOM is the problem.
The BOM (byte order mark) is a sequence of bytes that is written at the very beginning of a file to let the programs that read it know how the characters are encoded in that file. It is vital for the UTF-16 encoding but it is completely useless for UTF-8. However, many text editors put it in files and some of them have the option to skip it.
The BOM encodes a special whitespace character that is not visible and does not occupy space. It cannot be detected by visual inspection of the PHP script or of its output. Also, there is no way to handle it from the PHP code because, if present, it is stored inthe PHP file before the opening tag <?php and the PHP interpreter sends it to the browser along with anything else that is outside the PHP open and close tags.
How to solve it
Check the settings of your code editor and uncheck the option that tells it to use the BOM on file saving (or check the one that says "skip BOM").
If there is no setting regarding the BOM the change the encoding to ISO-8859-1 (also known as ISO-latin1). This encoding does not use BOM and it is identical with UTF-8 for all ASCII characters (digits, lowercase and uppercase letters without accents or diacritical marks, punctuation, parenthesis, mathematical operators, straight quotes and other special characters that are used by the programming languages).
Changing the encoding will not affect the code exposed it the question in any way.
headerline and seeing what you get? You should see a bunch of binary data, but you might see a PHP error or two at the start... - Niet the Dark Absolob_*functions to capture any previous output? - Justinas