Viewed   132 times

Why is this code not working ?

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

        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);


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‚ `



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" />.

Saturday, August 27, 2022

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (

  -r, --register-only
        do not remove files, only register the packages as not installed

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
$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 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:

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";
Tuesday, October 11, 2022

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

Thursday, September 29, 2022

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :