Click to See Complete Forum and Search --> : Showing image with imagePNG doesn´t work


Roxxor
10-23-2008, 04:09 PM
I am trying to show images in a folder with imagePNG. But I can´t get it working. Something seems to go wrong with ImageCreateFromPNG function. If I remove the header in pix.php, the images will appear but I will also get som warnings. If I use the header then the browser says it cannot output the image because it contains errors.

index.php
<?php
include "pix.php";
$dir = ".";

$dh = opendir($dir);

while(($file = readdir($dh)) != false)
{
if(preg_match("/.png/", $file) or preg_match("/.PNG/", $file))
{
$pix[] = $file;
}
}

closedir($dh);
?>
<html>
<head>
<title></title>
</head>

<body>
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<?php
foreach($pix as $key => $value)
echo "<td><img src=\"pix.php?pic=$value \" /></td>";

?>


</tr>
</table>
</body>
</html>

pix.php
<?php
$pic = $_REQUEST['pic'];
$src = ImageCreateFromPNG($pic);
$width = ImageSx($src);
$height = ImageSy($src);
$x = $width/2;
$y = $height/2;
$dst = ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
header('Content-Type: image/png'); // if I remove this line, the images are showing but I will also get some errors (see below)
ImagePNG($dst);
?>

These are the warning lines I get when I comment the header('Content... line:
Warning: imagesx(): supplied argument is not a valid Image resource in C:\wamp\www\galleri\pix.php on line 5

Warning: imagesy(): supplied argument is not a valid Image resource in C:\wamp\www\galleri\pix.php on line 6

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\galleri\pix.php on line 9

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\galleri\pix.php on line 10

Warning: imagepng(): supplied argument is not a valid Image resource in C:\wamp\www\galleri\pix.php on line 13

NogDog
10-23-2008, 08:43 PM
A few things:

I don't think you want to include pix.php in the index.php file, as you do not want to display an image at that point.

In the <img> tag, it looks like you have a space between the $value variable and the closing quote, which might be throwing off the file name that the image create function is trying to use. (You could take care of this in the pix.php by trim()-ing the $_REQUEST value.)

Lastly, you might want to put in an exit; right after the imagepng() call to ensure that nothing else gets output by the pix.php file after that.

Roxxor
11-04-2008, 10:02 AM
Thanks!

I solved it.

Another question:

Why does this work:
<?php
include "config.php";


$filename = $_REQUEST['pic'];
$fileID = $_REQUEST['id'];

if(!file_exists("pix/thumbnails/$fileID.png"))
{
$im = ImageCreateFromPNG("pix/$filename");
$width = ImageSx($im);
$height = ImageSy($im);

$x = 100;
$y = 100;

$dst = ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$im,0,0,0,0,$x,$y,$width,$height);

$textcolor = imagecolorallocate($im, 255, 255, 255);
imagestring($dst, 2, 30, 130, $filename, $textcolor);

header('Content-Type: image/png');

ImagePNG($dst, "pix/thumbnails/$fileID.png");
ImagePNG($dst);
imagedestroy($dst);
}
else
{
readfile("pix/thumbnails/$fileID.png");

}

?>

...while this does not (the difference is that in the below code I am using createThumb($im) to create the image and I think it should work. I don´t get any errors, the images just do not appear):
<?php
include "config.php";


$filename = $_REQUEST['pic'];
$fileID = $_REQUEST['id'];

if(!file_exists("pix/thumbnails/$fileID.png"))
{
$im = ImageCreateFromPNG("pix/$filename");
createThumb($im);
}
else
{
readfile("pix/thumbnails/$fileID.png");

}

function createThumb($im)
{
$width = ImageSx($im);
$height = ImageSy($im);

$x = 100;
$y = 100;

$dst = ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$im,0,0,0,0,$x,$y,$width,$height);

$textcolor = imagecolorallocate($im, 255, 255, 255);
imagestring($dst, 2, 30, 130, $filename, $textcolor);

header('Content-Type: image/png');

ImagePNG($dst, "pix/thumbnails/$fileID.png");
ImagePNG($dst);
imagedestroy($dst);
}
?>

?