imagecreatefrompng() fatal errors

Posted on 2006-05-08
Last Modified: 2012-05-05
I can't say that I've worked extensively with the GD library, but I've been toying with some of those functions tonight, and unfortunately the imagecreatefrompng() function throws back a fatal warning:

Warning: imagecreatefrompng(): '/home/gmyers/public_html/test.png' is not a valid PNG file in /home/gmyers/public_html/tester.php on line 2

I'm pretty sure it is *valid*; I just created it in Photoshop CS. Although I'm not sure in this case what PHP's definition is valid is. I also tested the function getimagesize() on the same image, which worked successfully; it gave me the correct width × height. The image has a transparent background, probably has a lot of colors, and may have some alpha blending (I used the polygon lasso tool in Photoshop to cut out the background; I believe that may cause the edges to have partial transparency.) I know that those properties may be causing the problem with PHP, and if that's the case, is there some way to preserve transparency/alpha channels? If that's not the case, what did I screw up?

Code I'm using:

$old_image = imagecreatefrompng('/home/gmyers/public_html/test.png');
$new_image = imagecreatetruecolor(400, 150);

imagecopy($new_image, $old_image, 0, 0, 130, 0, 100, 100);

header('Content-type: image/png');
Question by:soapergem
    LVL 40

    Accepted Solution

    Can you give a URL to the image?
    LVL 6

    Author Comment

    Heh...don't I feel silly now. It turns out that when I uploaded the image, my FTP client encountered an error and only partially uploaded it. That would be the problem! I've re-uploaded, and it works fine now. Aside from that, I'm a generous guy, and I think I would have been beating myself over the head for quite awhile longer had you not said that comment, so I'm going to give you all the points. Thanks!
    LVL 40

    Expert Comment


    The next thing I would have done is downloaded the image and then tried running the script myself.

    But as soon as the URL failed ...

    Thanks anyway.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now