如何改进我的 php 图像调整器以支持 alpha png 和透明 GIF

发布于 2024-08-31 06:41:03 字数 2081 浏览 5 评论 0原文

我使用这个函数来调整图像大小,但如果它是透明的 GIF 或带有 alpha 的 PNG,我最终会得到带有黑色背景的丑陋的令人毛骨悚然的图像,但它非常适合 jpg 和普通 png。

function cropImage($nw, $nh, $source, $stype, $dest) {
     $size = getimagesize($source);
     $w = $size[0];
      $h = $size[1];

      switch($stype) {
          case 'gif':
          $simg = imagecreatefromgif($source);
          break;
          case 'jpg':
          $simg = imagecreatefromjpeg($source);
          break;
          case 'png':
          $simg = imagecreatefrompng($source);
          break;
      }


     $dimg = imagecreatetruecolor($nw, $nh);

     switch ($stype)
    {
         case "png":

     imagealphablending( $dimg, false );
     imagesavealpha( $dimg, true );
     $transparent = imagecolorallocatealpha($dimg, 255, 255, 255, 127);
     imagefilledrectangle($dimg, 0,  0, $nw, $nh,  $transparent);

    break;
case "gif":
    // integer representation of the color black (rgb: 0,0,0)
    $background = imagecolorallocate($simg, 0, 0, 0);
    // removing the black from the placeholder
    imagecolortransparent($simg, $background);

    break;
     }



      $wm = $w/$nw;
      $hm = $h/$nh;
      $h_height = $nh/2;
      $w_height = $nw/2;

      if($w> $h) {
          $adjusted_width = $w / $hm;
          $half_width = $adjusted_width / 2;
          $int_width = $half_width - $w_height;
          imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
      } elseif(($w <$h) || ($w == $h)) {
          $adjusted_height = $h / $wm;
          $half_height = $adjusted_height / 2;
          $int_height = $half_height - $h_height;

     imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
      } else {
          imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
      }

      imagejpeg($dimg,$dest,100);
     }

示例: cropImage("300","200","original.png","png","new.png");

我使用 php 5.3.2 和捆绑的 GD 库 (2.0.1) 34兼容)

如何使其支持透明?我添加了 imagealphablending()imagesavealpha 但它不起作用。或者还有类似的好课吗?

谢谢

I use this function to resize images but i end up with ugly creepy image with a black background if it's a transparent GIF or PNG with alpha, however it works perfectly for jpg and normal png.

function cropImage($nw, $nh, $source, $stype, $dest) {
     $size = getimagesize($source);
     $w = $size[0];
      $h = $size[1];

      switch($stype) {
          case 'gif':
          $simg = imagecreatefromgif($source);
          break;
          case 'jpg':
          $simg = imagecreatefromjpeg($source);
          break;
          case 'png':
          $simg = imagecreatefrompng($source);
          break;
      }


     $dimg = imagecreatetruecolor($nw, $nh);

     switch ($stype)
    {
         case "png":

     imagealphablending( $dimg, false );
     imagesavealpha( $dimg, true );
     $transparent = imagecolorallocatealpha($dimg, 255, 255, 255, 127);
     imagefilledrectangle($dimg, 0,  0, $nw, $nh,  $transparent);

    break;
case "gif":
    // integer representation of the color black (rgb: 0,0,0)
    $background = imagecolorallocate($simg, 0, 0, 0);
    // removing the black from the placeholder
    imagecolortransparent($simg, $background);

    break;
     }



      $wm = $w/$nw;
      $hm = $h/$nh;
      $h_height = $nh/2;
      $w_height = $nw/2;

      if($w> $h) {
          $adjusted_width = $w / $hm;
          $half_width = $adjusted_width / 2;
          $int_width = $half_width - $w_height;
          imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
      } elseif(($w <$h) || ($w == $h)) {
          $adjusted_height = $h / $wm;
          $half_height = $adjusted_height / 2;
          $int_height = $half_height - $h_height;

     imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
      } else {
          imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
      }

      imagejpeg($dimg,$dest,100);
     }

Example : cropImage("300","200","original.png","png","new.png");

I use php 5.3.2 and the GD library bundled (2.0.34 compatible)

How to make it support transparency? i've added imagealphablending() and imagesavealpha but it didn't work. Or atlast is there any similar good classes?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-09-07 06:41:03

如果将图像输出为 png,丑陋的黑色背景就会消失。因此,这里有两种替代解决方案,均经过测试:

  1. 如果您可以将缩略图存储为 png,只需执行以下操作:将 imagejpeg($dimg,$dest,100); 更改为 imagepng($dimg,$dest);

  2. 如果将其存储为 jpeg 很重要,请删除行 imagealphablending( $dimg, false );imagesavealpha( $dimg, true ); -- 分别使用 truefalse 的默认值将提供所需的效果。仅当结果图像也具有 Alpha 通道时,禁用 Alpha 混合才有意义。

The ugly black background disappears if you output the image to png. So here are the two alternative solutions, both tested:

  1. If you can store the thumbnail as png, just do it: change imagejpeg($dimg,$dest,100); to imagepng($dimg,$dest);

  2. If it's important to store it as jpeg, remove the lines imagealphablending( $dimg, false ); and imagesavealpha( $dimg, true ); -- the default values of true and false, respectively, will provide the desired effect. Disabling alpha blending only makes sense if the result image also has an alpha channel.

呆° 2024-09-07 06:41:03

我自己还没有测试过,但这是我在处理我的项目时提出的一个想法。

首先,找到图像中未使用的颜色,并以此为背景创建一个新图像(例如,非常闪亮的绿色,就像它们在动作捕捉中所做的那样)

接下来,复制带有透明度的图像(我知道这适用于 PHP,我以前这样做是为了在图像上放置边框)

然后,使用函数 imagecolortransparent 来定义该图像中的哪种颜色是透明的,并为其赋予闪亮的绿色。

我认为它会起作用,但我还没有测试过。

I haven't tested it myself but this was an idea I've had for doing exactly that when working on a project of mine.

First, find a color that isn't used in the image and create a new image with that as the background (really flashy green for example, just like they do with motion capture)

Next, copy your image with transparency over it (I know this works with PHP I used to do this to put borders over images)

Then, use the function imagecolortransparent to define which color in that image is transparent and give it your flashy green color.

I think it would work but I haven't tested it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文