我有一个图片库,用户可以上传图片,上传图片后,PHP脚本创建它的两个副本 - 一个用于缩略图,另一个用于在更大尺寸的图库中显示。
该应用程序工作正常,但上传一些PNG图片时,声明
if($image=@imagecreatefromstring($filedata))
返回false。
以下是脚本。请帮忙。
<!---------------Processing uploaded image----------------->
<?php
if (isset($_FILES['file1']))
{
if($fgmembersite->CheckLogin())
{
if($_FILES['file1']['error']>0)
{
echo "file upload error".$_FILES['file1']['error'];
}
else
{
$allowedtype=array('image/jpg','image/jpeg','image/pjpeg','image/gif','image/png');
$maxsize=10*1024*1024;
$filename= mysql_real_escape_string($_FILES['file1']['name']);
$tmp_name=$_FILES['file1']['tmp_name'];
$size=$_FILES['file1']['size'];
$type=$_FILES['file1']['type'];
$ext=$filename;
if (!in_array($type,$allowedtype))
{
die ('Invalid file type');
}
if ($size>$maxsize)
{
die ('error- file size must be less than'.($maxsize/1024/1024).'MB');
}
filedata=file_get_contents($tmp_name);
if($image=@imagecreatefromstring($filedata))
{
$width=imagesx($image);
$height=imagesy($image);
//creating images
$large=imagecreatetruecolor(445,380);
imagecopyresampled($large,$image,0,0,0,0,445,380,$width,$height);
$largepath = 'image/large/' . uniqid('img',true) . '.jpg' ; //assigning file location and path
$thumb=imagecreatetruecolor(54,54);
imagecopyresampled($thumb,$image,0,0,0,0,54,54,$width,$height);
$thumbpath = 'image/thumb/' . uniqid('thumb',true) . '.jpg' ;
if (imagejpeg($thumb,$thumbpath) && imagejpeg($large,$largepath))
{
$con = connect();
query("INSERT INTO gallery (caption,thumbpath,largepath) values ('$caption','$thumbpath','$largepath')");
}
header('location:'.$_SERVER["PHP_SELF"]);
}
else
echo "failed";
}
}
}
?>
答案 0 :(得分:3)
检查imagejpeg上的手册,它采用质量可选的第三个参数(0 - 100):
if (imagejpeg($thumb,$thumbpath, 92) && imagejpeg($large,$largepath, 96))