【问题标题】:How to resize image without filling background with color如何在不使用颜色填充背景的情况下调整图像大小
【发布时间】:2015-07-05 21:47:30
【问题描述】:

我在 PHP 中转换 JPEG 图像时遇到一个问题。

我在做什么

我有不同的图像格式,我需要根据使用它的容器来调整它们的大小,例如在幻灯片中。

说实话,这是 OpenCart 引擎的默认实现。这有什么问题?

当我调整 PNG 的大小时,一切正常,并且我得到了一个透明的背景。

但是,当我转换 JPEG 图像时,我得到了一个背景,默认情况下,矩形填充有白色。我知道不可能让 JPEG 具有透明度。

我想要的结果

我想调整 JPEG 图像的大小并将它们合成到透明背景上。例如,转换和调整 JPEG 大小,绘制透明矩形,将 JPEG 放到这个矩形上,然后将其保存为 PNG 或其他允许透明的格式。

代码如下:

$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $height);

if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {     
    imagealphablending($this->image, false);
    imagesavealpha($this->image, true);
    $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
    imagecolortransparent($this->image, $background);
} else {
    $background = imagecolorallocate($this->image, 255, 255, 255);
}

imagefilledrectangle($this->image, 0, 0, $width, $height, $background);

imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, 
    $new_width, $new_height, $this->info['width'], $this->info['height']);
imagedestroy($image_old);

我曾尝试寻找解决方案、阅读手册和文档,但我所有的尝试都失败了。 (黑色背景,质量差,但背景仍然是填充的)

请帮助获得所需的结果。
感谢大家的帮助和建议。

【问题讨论】:

  • 你可以用 css 调整图像的大小,不是吗?
  • 问题是JPEG从一开始就不是透明的,所以永远不可能得到透明背景。您也许可以尝试找出背景颜色并尝试用一些模糊的颜色替换该颜色并使其透明......但是它看起来很糟糕,因为边缘不会被正确地消除锯齿或混合。
  • 没有办法只是将 JPEG(不透明)与透明 PNG 合并并获得透明的 PNG 或 GIF 吗?
  • @user4836275。你的说法不正确。您可以在 JPG 中添加透明边框并在 ImageMagick 中另存为 PNG。 convert image.jpg -bordercolor none -border 20 image.png 原始 jpg 区域外 20 像素的结果将是透明的。
  • 您可以使用 CSS 使白色像素透明:请参阅此答案:stackoverflow.com/a/34297963/273403

标签: php image image-processing jpeg opencart


【解决方案1】:
    if(isset($_GET["path"]) && $_GET["path"] != "" && file_exists($_GET["path"])){

        $path = $_GET["path"];
        //getting extension type (jpg, png, etc)
        $type = explode(".", $path);
        $ext = strtolower($type[sizeof($type)-1]);
        $ext = (!in_array($ext, array("jpeg","png","gif"))) ? "jpeg" : $ext;

        //get image size
        $size = getimagesize($path);
        $width = $size[0];
        $height = $size[1];

        //get source image
        $func = "imagecreatefrom".$ext;
        $source = $func($path);

        //setting default values

        $new_width = $width;
        $new_height = $height;
        $k_w = 1;
        $k_h = 1;
        $dst_x =0;
        $dst_y =0;
        $src_x =0;
        $src_y =0;

        //selecting width and height
        if(!isset ($_GET["width"]) && !isset ($_GET["height"]))
        {
            $new_height = $height;
            $new_width = $width;
        }
        else if(!isset ($_GET["width"]))
        {
            $new_height = $_GET["height"];
            $new_width = ($width*$_GET["height"])/$height;
        }
        else if(!isset ($_GET["height"]))
        {
            $new_height = ($height*$_GET["width"])/$width;
            $new_width = $_GET["width"];
        }
        else
        {
            $new_width = $_GET["width"];
            $new_height = $_GET["height"];
        }

        //secelcting_offsets

        if($new_width>$width )//by width
        {
            $dst_x = ($new_width-$width)/2;
        }
        if($new_height>$height)//by height
        {
            $dst_y = ($new_height-$height)/2;
        }
        if( $new_width<$width || $new_height<$height )
        {
            $k_w = $new_width/$width;
            $k_h = $new_height/$height;

            if($new_height>$height)
            {
                $src_x  = ($width-$new_width)/2;
            }
            else if ($new_width>$width)
            {
                    $src_y  = ($height-$new_height)/2;
            }
            else
            {
                if($k_h>$k_w)
                {
                    $src_x = round(($width-($new_width/$k_h))/2);
                }
                else
                {
                    $src_y = round(($height-($new_height/$k_w))/2);
                }
            }
        }
        $output = imagecreatetruecolor( $new_width, $new_height);

        //to preserve PNG transparency
        if($ext == "png")
        {
            //saving all full alpha channel information
            imagesavealpha($output, true);
            //setting completely transparent color
            $transparent = imagecolorallocatealpha($output, 0, 0, 0, 127);
            //filling created image with transparent color
            imagefill($output, 0, 0, $transparent);
        }

        imagecopyresampled( $output, $source,  $dst_x, $dst_y, $src_x, $src_y, 
                        $new_width-2*$dst_x, $new_height-2*$dst_y, 
                        $width-2*$src_x, $height-2*$src_y);
        //free resources
        ImageDestroy($source);

        //output image
        header('Content-Type: image/'.$ext);
        $func = "image".$ext;
        $func($output); 

        //free resources
        ImageDestroy($output);
    }

试试这个

【讨论】:

    【解决方案2】:

    你可以使用imagemagick来获取它。

    调整图片大小没有黑边,你可以这样做:

    convert in.jpg -resize 800x572 -background white -alpha remove -bordercolor black out.jpg

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 2021-01-17
      • 2014-07-01
      • 2022-10-13
      • 1970-01-01
      • 2016-03-23
      相关资源
      最近更新 更多