Asked  7 Months ago    Answers:  5   Viewed   25 times

Why is this code not working ?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>

        <title></title>
    </head>
    <body>
        <?php
        header('Content-type: image/png');
        $myImage = imagecreate(200, 100);
        $myGray = imagecolorallocate($myImage, 204, 204, 204);
        $myBlack = imagecolorallocate($myImage, 0, 0, 0);
        imageline($myImage, 15, 35, 120, 60, $myBlack);

        imagepng($myImage);
        imagedestroy($myImage);
        ?>
    </body>
</html>

I always get error The image cannot be displayed because it contains errors.. I've already enabled php_gd2.dll and memory_limit in php.ini is also 128M. If i remove header('Content-type: image/png'); i don't get the error but i don't see the image either. All i see is this :-

‰PNG ??? IHDR???È???d???ùHíH???PLTEÌÌÌ???Ó33d???MIDATH‰c£Àx?§” Nf*k²Ã)Ãø?§”•5}À)ÅS†ÚšpJUà”a§²¦œ2ÔŽw<špJ‚Q0 †;?? uTBúŸ????IEND®B‚ `

 Answers

49

You must not output anything before header(). Just start your document with <?php (as the first file characters), followed by the code for displaying the image. Skip the HTML tags. Do not even write a single blankline before header().

If you want to display an image inside the html document of yours, you must do it in two files. One, call it for example image.php, containing only the PHP code including the header. The second file, call it show.php or show.html, includes the HTML code you like, including <img src="image.php" alt="Your generated image" />.

Wednesday, March 31, 2021
 
Silfverstrom
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
23
$im = @imagecreatetruecolor(120, 20)
      or die('Cannot Initialize new GD image stream');

You first hide the real error and TRY to display something...

which you can't display because you don't look for it,

and expose the image no matter if it really got generated.

Then you go on stackoverflow and hope someone can guess the error you might have simply suppressed using @ operator.

Make sure there is nothing before <?php and if you have, remove ?> at the end.

To make sure you have GD installed try this in a new php file:

<?php
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";
}
Wednesday, March 31, 2021
 
astaykov
answered 7 Months ago
90

If it is true that it works by uncommenting the require() for myclass.class.php, then the most likely cause is this file contains blank lines (whitespace) before the <?php or after the ?>. This would add Ascii characters to the output of the image, or insert a php error message (Headers could not be sent) on your header() statement and thus mess up your file. However, as I mentioned in my comment, if your sole purpose is to output the picture you could use readfile() instead of creating an image instance. Hope that helps, Stefan

Wednesday, March 31, 2021
 
Pradip
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share