...

View Full Version : Resolved GD lib with png



Phil Jackson
10-28-2009, 03:00 PM
Hi all. Not much of a GD man but i can understand and change to suit. My problem being this code i've wrote when uploading .png images.



<?php
if(isset($_FILES) && count($_FILES)!=0)
{
if(!empty($_FILES['fileToUpload']['tmp_name']))
{
if(preg_match("#^image/((?:gif)|(?:jpg)|(?:jpeg)|(?:png))$#is", $_FILES['fileToUpload']['type'], $match))
{
$extention = $match[1];
if(!empty($_POST['fileToUploadName']))
{
$filename = $_POST['fileToUploadName'].".".$extention;
}
else
{
$filename = $_FILES['fileToUpload']['name'].".".$extention;
}

$image_location = $filename;
if(!file_exists($image_location))
{
$uploadedfile = $_FILES['fileToUpload']['tmp_name'];
if($extention=="gif")
{
$src = imagecreatefromgif($uploadedfile);
}
elseif($extention=="png")
{
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromjpeg($uploadedfile);
}

list($width,$height)=getimagesize($uploadedfile);

$newwidth=200;
$newheight=134;
$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

if($extention=="gif")
{
if(imagegif($tmp,$filename,100))
{
imagedestroy($src);
imagedestroy($tmp);
echo "upload complete";
}
else
{
echo "upload failed";
}
}
elseif($extention=="png")
{
if(imagepng($tmp,$filename,100))
{
imagedestroy($src);
imagedestroy($tmp);
echo "upload complete";
}
else
{
echo "upload failed";
}
}
else
{
if(imagejpeg($tmp,$filename,100))
{
imagedestroy($src);
imagedestroy($tmp);
echo "upload complete";
}
else
{
echo "upload failed";
}
}
}
else
{
echo "file name already exists";
}
}
else
{
echo "please select a valid image file to upload";
}
}
else
{
echo "Please select a file to upload";
}
}
?>




returns error:


Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib failed to initialize compressor -- stream error in C:\wamp\www\ACTfileUploader\uploader.php on line 58

Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in C:\wamp\www\ACTfileUploader\uploader.php on line 58
upload complete

mlse
10-28-2009, 03:16 PM
This is the problem:



imagepng($tmp,$filename,100)

Where you have 100, use an integer from 0 to 9 instead.

Look at the "quality" argument of imagepng in the PHP manual: http://uk.php.net/manual/en/function.imagepng.php

Phil Jackson
10-28-2009, 03:23 PM
good man

gabrielesquivel
03-02-2010, 07:16 PM
Thanks :thumbsup:

Gabriel
-----------------------
www.gaboesquivel.com (http://www.gaboesquivel.com)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum