0

For my website, I've been trying to make it so people's characters (which are currently composed of several pictures that are moved by CSS) are merged into one image as to make my life easier. The chunk of code that currently doesn't work is as follows:

                    $template = $charRow['template'];
                    $gender = $charRow['gender'];
                    $shirt = $charRow['shirt'];
                    $pants = $charRow['pants'];
                    $hat = $charRow['hat'];

                    $templatePic = imagecreatefrompng("Templates/".$template);

                    if (!empty($shirt)) {
                        $shirtPic = imagecreatefrompng($shirt);
                        imagecopy($templatePic,$shirtPic,0,0,0,0,imagesx($templatePic),imagesy($templatePic));
                    }
                    if (!empty($pants)) {
                        $pantsPic = imagecreatefrompng($pants);
                        imagecopy($templatePic,$pantsPic,0,0,0,0,imagesx($templatePic),imagesy($templatePic));
                    }
                    if (!empty($hat)) {
                        $hatPic = imagecreatefrompng($hat);
                        imagecopy($templatePic,$hatPic,0,0,0,0,imagesx($templatePic),imagesy($templatePic));
                    }

                    imagePNG($templatePic, 'Images/'); //Problem line...

This is the error PHP is giving me:

Warning: imagepng() [function.imagepng]: Unable to open 'Images/' for writing: Is a directory in PathToParentFolderOfFollowingFile/testFile.php on line 139

What exactly does this error mean and how can it be fixed?

NOTE: $charRow is not the problem. The query to get that is just not being displayed to all of you.

Also...How do you make the transparency work? All the files I'm working with are PNG files WITH transparency.

  • Two things come to mind - either your php app doesn't have permissions to access the Images directory, or you don't have the correct path for the Images dir. – JW8 Nov 17 '11 at 17:51
  • 1
    similar question has been answered [here](http://stackoverflow.com/questions/4388780/imagepng-function-not-quite-working). – Muhammad Saifuddin Nov 17 '11 at 17:55

1 Answers1

2

That error appears because you need to supply a filename, not a directory. The imagePNG function does not know what file to save this image as.

Try

imagepng($templatePic, 'Images/anActualFileName.png');

I see the docs say 'path', which could be confusing, but in unix terms that includes a filename. If you just gave it a directory, what would it call the file?

JAL
  • 21,295
  • 1
  • 48
  • 66
  • It now works in every way BUT the fact it doesn't show transparency...How do I fix that? – bballer13sn Nov 17 '11 at 23:23
  • 1
    do a quick search on SO - you'll be surprised at what you find! ;) http://stackoverflow.com/questions/1705098/php-imagepng-and-transparency – JW8 Nov 17 '11 at 23:58
  • But what about the fact that my images already have transparent backgrounds? – bballer13sn Nov 18 '11 at 02:25