不要不为什么图像质量不是100%

时间:2019-08-21 19:20:20

标签: php

下面的代码将图像文件上传到目录。我花了一些时间对此进行研究。似乎正在将图像质量更改为70-80%。我不知道这是怎么回事。我想将图像质量保持在100%。

我尝试替换:

  

ImageJpeg($ resized_img,“ $ path_thumbs / $ rand_name。$ file_ext”);

收件人:

  

ImageJpeg($ resized_img,“ $ path_thumbs / $ rand_name。$ file_ext” 100);

那似乎不起作用!


这是我的代码:

if(isset($_POST['submit']))
{

    //make sure this directory is writable!
    $path_thumbs = "backgrounds/";

    //the new width of the resized image, in pixels.
    $img_thumb_width = 1920; // 

    $extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
    //List of allowed extensions if extlimit = yes
    // $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
    $limitedext = array(".jpg");

    //the image -> variables
    $file_type = $_FILES['vImage']['type'];
    $file_name = $_FILES['vImage']['name'];
    $file_size = $_FILES['vImage']['size'];
    $file_tmp = $_FILES['vImage']['tmp_name'];

    // No upload size limit
    ini_set('memory_limit', '-1');

    //check if you have selected a file.
    if(!is_uploaded_file($file_tmp)){
        echo "Please select a file to upload! <a href=\"$_SERVER[PHP_SELF]\">Try again</a>";
        exit(); //exit the script and don't process the rest of it!
    }
    //check the file's extension
    $ext = strrchr($file_name,'.');
    $ext = strtolower($ext);
    //uh-oh! the file extension is not allowed!
    if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
        echo "Wrong file extension. Make sure the file is a JPG file! <br>--<a href=\"$_SERVER[PHP_SELF]\">Try again..</a>";
        exit();
    }
    //so, whats the file's extension?
    $getExt = explode ('.', $file_name);
    $file_ext = $getExt[count($getExt)-1];

    //create a random file name
    // $rand_name = md5(time());
    // $rand_name= rand(0,999999999);
    $rand_name = 'default';
    //the new width variable
    $ThumbWidth = $img_thumb_width;

    //////////////////////////
    // CREATE THE THUMBNAIL //
    //////////////////////////

    //keep image type
    if($file_size){
        if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
            $new_img = imagecreatefromjpeg($file_tmp);
        }elseif($file_type == "image/x-png" || $file_type == "image/png"){
            $new_img = imagecreatefrompng($file_tmp);
        }elseif($file_type == "image/gif"){
            $new_img = imagecreatefromgif($file_tmp);
        }
        //list the width and height and keep the height ratio.
        list($width, $height) = getimagesize($file_tmp);
        //calculate the image ratio
        $imgratio=$width/$height;
        if ($imgratio>1){
            $newwidth = $ThumbWidth;
            $newheight = $ThumbWidth/$imgratio;
        }else{
                $newheight = $ThumbWidth;
                $newwidth = $ThumbWidth*$imgratio;
        }
        //function for resize image.
        if (function_exists(imagecreatetruecolor)){
        $resized_img = imagecreatetruecolor($newwidth,$newheight);
        }else{
                die("Error: Please make sure you have GD library ver 2+");
        }
        //the resizing is going on here!
        imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        //finally, save the image
        ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
        ImageDestroy ($resized_img);
        ImageDestroy ($new_img);


    }

    //ok copy the finished file to the thumbnail directory
    move_uploaded_file ("$path_big/$rand_name.$file_ext", $file_tmp);

我们将不胜感激:)

1 个答案:

答案 0 :(得分:1)

您的问题似乎是:imagecopyresized,它将复制并缩放图像,并使用一种相当原始的算法,该算法往往会产生更多的像素化结果。

  • imagecopyresampled将进行复制和缩放以及图像处理,它使用平滑和像素插值算法,通常产生的效果要好得多,然后图像复制的大小会有所降低,而只需占用很少的cpu。

  • imagecopy将复制但不会缩放图像。

尝试imagecopyresampled或仅尝试imagecopy并比较结果。

here

获取此信息