Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Imagecreatefrompng() function in php

  1. May 28, 2007 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I was experimenting with the imagecreatefrompng() function in php. There was an example here:

    I tried to modify it to use an image called full.png in my img folder.

    Code (Text):


    function LoadPNG($imgname)
        $im = @imagecreatefrompng($imgname); /* Attempt to open */
        if (!$im) { /* See if it failed */
            $im  = imagecreatetruecolor(150, 30); /* Create a blank image */
            $bgc = imagecolorallocate($im, 255, 255, 255);
            $tc  = imagecolorallocate($im, 0, 0, 0);
            imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
            /* Output an errmsg */
            imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
        return $im;
    header("Content-Type: image/png");
    $img = LoadPNG("img/full.png");

    I'm not sure what to do with the HTML. I tried the simplest idea:
    Code (Text):

    This is a test.<BR/>
    <img src="aaaimage.php" />
    But that does nothing. I just get a broken image tag. I am not really clear on what the function returns, they say it is an

    "image identifier" but I don't know what that means.

    Any advice? Thanks.:smile:
  2. jcsd
  3. May 28, 2007 #2


    User Avatar
    Science Advisor
    Homework Helper

    Here is a lot of commentary on this function.
    Apparently there is only one error return.
    Scroll down a bit. The page looks blank, but isn't.

  4. May 29, 2007 #3

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    thanks. I've been through the commentary but still haven't solved my problem. I think I might try the jpeg version of this function and see if I have any more luck. I was hoping if I was doing something stupid in my code it would be glaringly apparent and I could correct it, but it sounds like this is just going to be a complicated function to work with.
    Last edited: May 29, 2007
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook