Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP文件上载脚本生成模糊图像_Php_File Upload - Fatal编程技术网

PHP文件上载脚本生成模糊图像

PHP文件上载脚本生成模糊图像,php,file-upload,Php,File Upload,我制作的文件上传脚本,上传时会上传模糊的图像!这是我目前的剧本,试着找出我做错了什么。脚本以.png格式上载图像,用户名是当前登录用户的实际用户名 请注意,原始图像是17x22,因此这并不是使其模糊的原因 <?php include('../class/resize.php'); //error_reporting(0); if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ $path = "../files/cloaks

我制作的文件上传脚本,上传时会上传模糊的图像!这是我目前的剧本,试着找出我做错了什么。脚本以.png格式上载图像,用户名是当前登录用户的实际用户名

请注意,原始图像是17x22,因此这并不是使其模糊的原因

<?php
include('../class/resize.php');
//error_reporting(0);
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$path = "../files/cloaks/"; //set your folder path
$filename = $_FILES['photoimg']['tmp_name']; //get the temporary uploaded image name
$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg","GIF","JPG","PNG", "JPEG"); //add the formats you want to upload

        $name = $_FILES['photoimg']['name']; //get the name of the image
        $size = $_FILES['photoimg']['size']; //get the size of the image
        if(strlen($name)) //check if the file is selected or cancelled after pressing the browse button. 
        {
            list($txt, $ext) = explode(".", $name); //extract the name and extension of the image
            if(in_array($ext,$valid_formats)) //if the file is valid go on.
            {
            if($size < 2098888) // check if the file size is more than 2 mb
            {
            $actual_image_name =  $_POST['fname']; //actual image name going to store in your folder
            $tmp = $_FILES['photoimg']['tmp_name']; 
            if(move_uploaded_file($tmp, $path.$actual_image_name)) //check the path if it is fine
                {   
                    move_uploaded_file($tmp, $path.$actual_image_name); //move the file to the folder
                    $dburl = ('../files/cloaks/'.$actual_image_name.'');
                    $image = new ZiResize();
                    $image->load($dburl);
                    $image->resize(22,17);
                    $image->save($path.$actual_image_name);
                    //display the image after successfully upload
                    echo "<img src='files/cloaks/".$actual_image_name."'  class='preview'> <input type='hidden' name='actual_image_name' id='actual_image_name' value='$actual_image_name' />";

                }
            else
                {
                echo "failed";
                }
            }
            else
            {
                echo "Error! Max image size is 2 MB!";                  
            }
            }
            else
            {
                echo "Error! Invalid image format!";    
            }
        }
        else
        {       
        echo "Error! No file selected!";
        }       
    exit;
    }
?>

将您的大小调整为22px乘以17px。当您将图像缩小到该大小时,它看起来总是模糊的。您还应该使用
imagecreatetruecolor
,因为它允许全彩色光谱。imagecreate的颜色有限,这也是图像看起来模糊的另一个原因

您将图像大小调整为22像素乘以17像素。当您将图像缩小到该大小时,它看起来总是模糊的。您还应该使用
imagecreatetruecolor
,因为它允许全彩色光谱。imagecreate的颜色有限,这是保存图像时图像可能看起来模糊的另一个原因

函数保存($filename,$image\u type=IMAGETYPE\u JPEG,$compression=75,$permissions=null)

您使用的是jpg格式,在php中,您可以设置结果图像的质量,如果该质量低于原始图像,则图像将看起来“模糊”,即使尚未调整大小

你可以:

  • 更改压缩值(
    $image->save($path.$actual\u image\u name,NULL,100);
  • 将图像格式更改为不支持“压缩”的其他格式
  • 由于不调整图像的大小,因此可以替换以下内容:

    $image->resize(22,17);
    $image->save($path.$actual_image_name);
    
    为此:

    $image->save($path.$actual_image_name, NULL, 100);
    

    保存图像时更改压缩参数

    函数保存($filename,$image\u type=IMAGETYPE\u JPEG,$compression=75,$permissions=null)

    您使用的是jpg格式,在php中,您可以设置结果图像的质量,如果该质量低于原始图像,则图像将看起来“模糊”,即使尚未调整大小

    你可以:

  • 更改压缩值(
    $image->save($path.$actual\u image\u name,NULL,100);
  • 将图像格式更改为不支持“压缩”的其他格式
  • 由于不调整图像的大小,因此可以替换以下内容:

    $image->resize(22,17);
    $image->save($path.$actual_image_name);
    
    为此:

    $image->save($path.$actual_image_name, NULL, 100);
    


    问题将出现在你的
    ZiResize
    类中。也许可以查看它的文档。你能展示一个示例屏幕截图吗?一个具体的示例屏幕截图吗?调整大小代码?这是做什么的?可能是重新调整大小使图像变得模糊
    $image->resize(22,17)
    您需要查看
    ZiResize
    类的文档,看看是否可以更改采样算法。我假设22和17是输出的尺寸-任何大小调整到如此小的图像都会模糊。不,图像是像素化的图片,它只需要图像就可以获得这些像素。问题将出在您的
    ZiResize
    类。也许可以查看它的文档。你能显示一个示例屏幕截图吗?一个具体的示例屏幕截图?调整大小代码?这是做什么的?可能是重新调整大小,使图像变得模糊
    $image->resize(22,17)
    您需要查看
    ZiResize
    类的文档,看看是否可以更改采样算法。我假设22和17是输出的尺寸-任何大小调整到如此小的图像都会模糊。不,图像是像素化的图片,它只需要图像就可以获得这些像素。不,原始图像是22px乘以17px。它没有被调整大小,如果有人能帮我移除调整大小的部分就可以了,因为我自己做这件事有困难。更新的答案,在你的调整大小功能中将
    imagecreatetruecolor
    替换为
    imagecreatetruecolor
    我必须对所有的“imagecreate”都这样做吗是的。但是对于一个简单的文件上传程序,没有任何图像加载、调整大小或操作需要在那里。这实际上帮助了我以更好的质量保存它,但是它没有解决问题,但是我现在正在我的代码中使用它!谢谢你的帮助!不,原始图像是22px乘17px。它没有被调整大小,如果有人能帮我移除调整大小的部分也没关系,因为我自己做的很麻烦。更新的答案,在你的调整大小功能中,用
    imagecreatetruecolor
    替换
    imagecreatetruecolor
    我必须对所有的“imagecreate”都这样做吗是的。但是对于一个简单的文件上传程序来说,所有的图像加载、调整大小或操作都不需要在那里。这实际上帮助了我一点,让它以更好的质量保存,但是它没有解决问题,但是我现在正在我的代码中使用它!谢谢你的帮助!我上传的是一个.png文件,不是jpg。是的,但您正在保存一个
    jpg
    文件。请尝试替换我编写的代码,如果没有,请告诉我。我正在上载一个.png文件,而不是jpg。是的,但您正在保存一个
    jpg
    文件。请尝试替换我编写的代码,如果没有,请告诉我。。