0

我有一个图片库,用户可以上传图片,上传图片后,PHP 脚本会创建两个副本 - 一个用于缩略图,另一个用于以较大尺寸显示在图库中。

该应用程序工作正常,除了,上传一些PNG图片时,声明

 if($image=@imagecreatefromstring($filedata))

返回假。

下面是脚本。请帮忙。

    <!---------------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";
                }
        }
    }
?>
4

1 回答 1

3

检查imagejpeg上的手册,它需要一个可选的第三个参数,即质量(0 - 100):

if (imagejpeg($thumb,$thumbpath, 92) && imagejpeg($large,$largepath, 96))
于 2013-03-12T14:03:52.383 回答