【问题标题】:Can't upload image with 100% quality无法上传 100% 质量的图片
【发布时间】:2020-11-29 02:57:03
【问题描述】:

我正在尝试通过 PHP 上传图片,但我无法上传 100% 质量的图片。

我实际使用的代码

class imaging
{

    // Variables
    private $img_input;
    private $img_output;
    private $img_src;
    private $format;
    private $quality = 100;
    private $x_input;
    private $y_input;
    private $x_output;
    private $y_output;
    private $resize;

    // Set image
    public function upload($orig, $name, $path)
    {
        move_uploaded_file($orig, __DIR__ . "/../uploads/" . $path . 'orig_' . $name);
    }

    public function set_img($img)
    {
        //$img = __DIR__ . '/../uploads/' . $img;

        $img = '../uploads/' . $img;

        // Find format
        $ext = strtoupper(pathinfo($img, PATHINFO_EXTENSION));

        // JPEG image
        if(is_file($img) && ($ext == "JPG" OR $ext == "JPEG"))
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromJPEG($img);
            $this->img_src = $img;

        }

        // PNG image
        elseif(is_file($img) && $ext == "PNG")
        {

            $this->format = $ext;
            $this->img_input = ImageCreateFromPNG($img);
            $this->img_src = $img;

        }

        // GIF image
        elseif(is_file($img) && $ext == "GIF")
        {

            $this->format = $ext;
            $this->img_input = ImageCreateFromGIF($img);
            $this->img_src = $img;

        }

        // Get dimensions
        $this->x_input = imagesx($this->img_input);
        $this->y_input = imagesy($this->img_input);
    }

    // Set maximum image size (pixels)
    public function set_size($size = 100)
    {
        // Wide
        if($this->x_input >= $this->y_input && $this->x_input > $size)
        {
            $this->x_output = $size;
            $this->y_output = ($this->x_output / $this->x_input) * $this->y_input;

            $this->resize = TRUE;
        }

        // Tall
        elseif($this->y_input > $this->x_input && $this->y_input > $size)
        {
            $this->y_output = $size;
            $this->x_output = ($this->y_output / $this->y_input) * $this->x_input;

            $this->resize = TRUE;
        }

        // Don't resize
        else { $this->resize = FALSE; }

    }

    // Set image quality (JPEG only)
    public function set_quality($quality)
    {

        if(is_int($quality))
        {

            $this->quality = $quality;

        }

    }

    // Save image
    public function save_img($path)
    {

        // Resize
        if($this->resize)
        {

            $this->img_output = ImageCreateTrueColor($this->x_output, $this->y_output);
            ImageCopyResampled($this->img_output, $this->img_input, 0, 0, 0, 0, $this->x_output, $this->y_output, $this->x_input, $this->y_input);

        }

        // Save JPEG
        if($this->format == "JPG" OR $this->format == "JPEG")
        {

            if($this->resize) { imageJPEG($this->img_output, __DIR__ . "/../uploads/" . $path, $this->quality); }
            else { copy($this->img_src, __DIR__ . "/../uploads/" . $path); }

        }

        // Save PNG
        elseif($this->format == "PNG")
        {

            if($this->resize) { imagePNG($this->img_output, __DIR__ . "/../uploads/" . $path, 9); }
            else { copy($this->img_src, __DIR__ . "/../uploads/" . $path); }

        }

        // Save GIF
        elseif($this->format == "GIF")
        {

            if($this->resize) { imageGIF($this->img_output, __DIR__ . "/../uploads/" . $path); }
            else { copy($this->img_src, __DIR__ . "/../uploads/" . $path); }

        }

    }

    // Get width
    public function get_width()
    {

        return $this->x_input;

    }

    // Get height
    public function get_height()
    {

        return $this->y_input;

    }

    // Clear image cache
    public function clear_cache()
    {

        @ImageDestroy($this->img_input);
        @ImageDestroy($this->img_output);

    }

}

并调用上传

$img = new imaging;
$img->upload($src['tmp_name'], $name, $dir);
$img->set_img($dir . 'orig_' . $name); // upload original file
$img->set_quality(100);

// Small thumbnail
$img->set_size(700);                   // upload thumbnail
$img->save_img($dir . $name);

// Finalize
$img->clear_cache();

结果不是很好,而不是设置quality=100。原始文件(宽度 cca 1100px)已正确上传(服务器上没有调整大小),当我在 Photoshop 中打开它时,将其调整为 700px 宽度并与 PHP 中调整大小的 700px 拇指进行比较,质量差异很大。

查看两个图像,在 Photoshop 中调整原始大小(顶部)和通过 PHP 调整图像大小(底部) - 文本、图像等模糊,颜色不亮。

原始大小




在 Photoshop 中放大 200%


有什么想法吗?感谢您的回复:-)

【问题讨论】:

  • 您的示例图片不是很大,也不是很清楚地说明您的问题
  • @Martin:嗯,这里上传的可能不好...我添加了更好的图片,原尺寸并在 Photoshop 中放大。
  • 请问有现场版吗?你可以发布原始图像进行测试吗?和可能的完整代码来测试你如何上传图片
  • 您正在使用捆绑的 GD 库。使用另一个后端处理图像可能会产生更好的结果。您是否尝试过使用 ImageMagick 或 GraphicsMagick(或 GMagick)?两者都是 PHP 的 supported,尽管对于这两个选项,主机上都需要额外的库。

标签: php file-upload image-resizing image-quality


【解决方案1】:

我会推荐

将您的图像转换为 base64,然后更新 base64 字符串,然后在服务器端再次将该 base64 字符串转换为图像。你会找到转换的方法

图片转base64:https://stackoverflow.com/a/13758760/11956865

base64 转图片:https://stackoverflow.com/a/15153931/11956865

【讨论】:

    【解决方案2】:

    我建议您使用Intervention Image Library 进行图像处理和操作。我过去遇到过这类问题,切换到不同的库解决了这个问题。

    其次,这个库的使用非常简单,已经在网站上提供。

    【讨论】:

      【解决方案3】:

      你可以试试这个包http://image.intervention.io/getting_started/introductionhttp://image.intervention.io/getting_started/installation,这里有所有的配置说明。 它应该可以正常工作,并且很容易使用 composer 进行设置。

      【讨论】:

        【解决方案4】:

        我会在这里回答,因为我没有足够的声誉来发表评论。

        要调整图像大小,我认为最好使用命令imagescale。最好使用ImageCreateTrueColor + ImageCopyResampled

        所以我会这样做

                // Resize
                if($this->resize)
                {
                    $this->img_output = imagescale($this->img_input, $this->x_output, $this->y_output);
        
                }
        

        但您也可以更改其他一些内容:

        • 我创建了 create_thumbnail() 方法,您将向它传递图像的路径、保存新图像的路径以及可选的大小(默认为 100)。
        • 要具有宽度和高度,您不需要创建图像。可以使用getimagesize的方法,这样可以节省很多资源。
        • 当您调用该方法调整大小时,它会根据需要调整图像大小。
        • 当您要保存图像时,您可以知道图像是否已调整大小,以便您复制或不复制它。

        另外,我尽量不重复任何代码并优化资源。

        编辑:您也可以清除缓存并结束进程。

        class imaging
        {
        
            // Variables
            private $img_input;
            private $img_output;
            private $img_src;
            private $format;
            private $quality = 100;
            private $x_input;
            private $y_input;
            private $x_output;
            private $y_output;
            private $resize;
        
            // Set image
            public function upload($orig, $name, $path)
            {
                move_uploaded_file($orig, __DIR__ . "/../uploads/" . $path . 'orig_' . $name);
            }
        
                // Set image quality (JPEG only)
            public function set_quality($quality)
            {
        
                if(is_int($quality))
                {
        
                    $this->quality = $quality;
        
                }
        
            }
        
            // Set maximum image size (pixels)
            private function resize($size = 100)
            {   
                $resize = FALSE; 
                // Wide
                if($this->x_input >= $this->y_input && $this->x_input > $size)
                {
                    $this->x_output = $size;
                    $this->y_output = ($this->x_output / $this->x_input) * $this->y_input;
        
                    $resize = TRUE;
                }
        
                // Tall
                elseif($this->y_input > $this->x_input && $this->y_input > $size)
                {
                    $this->y_output = $size;
                    $this->x_output = ($this->y_output / $this->y_input) * $this->x_input;
        
                    $resize = TRUE;
                }
        
                if($resize){
        
                    switch ($this->format) {
                        case 'JPEG':
                        case 'JPG':
                            $this->img_input = ImageCreateFromJPEG($img);
                            break;
                        case 'PNG':
                            $this->img_input = ImageCreateFromPNG($img);
                            break;
                        case 'GIF':
                            $this->img_input = ImageCreateFromGIF($img);
                            break;
                        
                        default:
                            throw new Exception("This extension " . $ext . " it's not compatible.");
                            
                            break;
                    }
        
                }
        
            }
        
            // Save image
            public function save_img($path)
            {
                // We have resized the image
                if($this->img_input){
                    switch ($this->format) {
                        case 'JPEG':
                        case 'JPG':
                            imageJPEG($this->img_output, __DIR__ . "/../uploads/" . $path, $this->quality);
                            break;
                        case 'PNG':
                            imagePNG( $this->img_output, __DIR__ . "/../uploads/" . $path, 9);
                            break;
                        case 'GIF':
                            imageGIF( $this->img_output, __DIR__ . "/../uploads/" . $path);
                            break;
                        
                        default:
                            throw new Exception("This extension " . $ext . " it's not compatible.");
                            
                            break;
                    }
                }else{
                    copy($this->img_src, __DIR__ . "/../uploads/" . $path);
                }
        
            }
        
            public function create_thumbnail($imgSrc, $savePath, $size = 100)
            {
                $this->img_src = '../uploads/' . $img;
        
                if(file_exists($this->img_src)){
                    list($this->x_input, $this->y_input) = getimagesize($imageSrc);
                    $this->format = strtoupper(pathinfo($img, PATHINFO_EXTENSION));
                    $this->resize($size);
                    $this->save_img($savePath);
                    $this->clear_cache();
                }else{
                    throw new Exception("Image not found in " . $imdSrc);
                }
        
            }
        
            // Get width
            public function get_width()
            {
        
                return $this->x_input;
        
            }
        
            // Get height
            public function get_height()
            {
        
                return $this->y_input;
        
            }
        
            // Clear image cache
            public function clear_cache()
            {
        
                @ImageDestroy($this->img_input);
                @ImageDestroy($this->img_output);
        
            }
        
        }
        

        【讨论】:

        • $this->img_output = imagescale($this->img_output, $this->x_output, $this->y_output); 什么都不做,$this->img_output 在函数内部是 NULL。首先我需要解决 img 质量问题,然后才能调整代码 :-)
        • 当然可以!哈哈 原来是$this->img_input 所以if($this->resize) { $this->img_output = imagescale($this->img_input, $this->x_output, $this->y_output); }
        猜你喜欢
        • 2011-12-31
        • 2021-05-21
        • 1970-01-01
        • 2015-09-02
        • 1970-01-01
        • 2019-07-17
        • 1970-01-01
        • 2013-02-26
        • 1970-01-01
        相关资源
        最近更新 更多