Vaidotas
Vaidotas

Reputation: 19

The image cannot be displayed because it contains errors (Need help!!!)

When I try this code, I get this error: "The image cannot be displayed because it contains errors" and I don't know how to fix it.

header('Content-type: image/jpeg');
$email= '[email protected]';
$email_length=strlen($email);
$font_size= 4;
$image_height= imagefontheight($font_size);
$image_width = imagefontwidth($font_size)*$email_length;
$image=imagecreate($image_width, $image_height);
imagecolorallocate($image,255,255,255);
$font_color= imagecolorallocate($image,0,0,0);
imagestring($image,$font_size, 0, 0, $email, $font_color);
imagejpeg($image);

Upvotes: 1

Views: 12721

Answers (3)

atilauy
atilauy

Reputation: 41

Check that there are no spaces before the php opening tag, and if you use a closing tag, check there are no spaces or any other characters after it.

I've had this same problem more than once and this was the cause.

Upvotes: 1

axiac
axiac

Reputation: 72226

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.

Upvotes: 2

Szymon Roziewski
Szymon Roziewski

Reputation: 1129

Make sure the library GD is installed

if (extension_loaded('gd') && function_exists('gd_info')) {
    echo "PHP GD library is installed on your web server";
}
else {
    echo "PHP GD library is NOT installed on your web server";
}

Upvotes: 0

Related Questions