【问题标题】:Image transparency in imagepng() and imagegif()imagepng() 和 imagegif() 中的图像透明度
【发布时间】:2015-02-06 04:04:41
【问题描述】:

我想调整 2 种具有透明背景的图像(png、gif)的大小...

  1. 但在 png 中,它适用于大尺寸的原始图像。 例如。原始文件尺寸为 150x150

    更改为 200x200 或超过原始尺寸效果很好。但它以 100x100 显示

  2. 那么最后一个 GIF 就不行了。

    这是我的 png 代码

$new_img = imagecreatetruecolor($max_w, $max_h); imagealphablending($new_img, false); imagesavealpha($new_img, true); $transparentindex = imagecolorallocatealpha($new_img, 255, 255, 255, 127); imagefill($new_img, 0, 0, $transparentindex); imagepng($new_img, $dir); 这是我的 gif 代码 imagegif($new_img, $dir);

【问题讨论】:

  • 如何获得具有透明背景的 JPEG?这是不可能的!您是真的要调整图像的大小 - 还是只想将它们叠加到更大、更透明的背景上?
  • 对不起。我只需要png和gif。这是我的错。
  • 是的,我想将图像调整为更大或更小。在 png 中更大的作品很好。但较小的适用于错误。那么另一端的 gif 完全不起作用。

标签: php image


【解决方案1】:

我不懂 PHP,但这似乎可以解决问题:

#!/usr/local/bin/php -f
<?php
    $neww=100;
    $newh=100;

    // Load original image and get its dimensions
    $img = imagecreatefrompng("badge.png");
    $w=imagesx($img);
    $h=imagesy($img);

    // Create output image, and fill with lots of transparency
    $out = imagecreatetruecolor($neww,$newh);
    imagealphablending($out,false);
    $transparentindex = imagecolorallocatealpha($out,0,0,0,127);
    imagefill($out,0,0,$transparentindex);
    imagesavealpha($out, true);

    // Copy original image, reized on top
    imagecopyresized($out,$img,0,0,0,0,$neww,$newh,$w,$h);
    imagepng($out,"out.png");
?>

【讨论】:

  • 这不是一个很好的工作!什么不工作,PNG 或 GIF?它以什么方式“不起作用”?您使用的是什么版本的 PHP?你用的是什么版本的GDphp -i | grep -A10 -i gd
【解决方案2】:

这是一个小技巧:) 没有其他想法。

它涉及第二张图片(1x1 透明 gif)

  1. 创建图像
  2. 调整大小
  3. 创建与调整后图像大小相同的透明背景
  4. 使用imagecolortransparent()获取rgb索引
  5. imagecopymerge() 图片

.

$dir = 'http://i.stack.imgur.com/DbhrJ.png';
$max_w = 50;
$max_h = 50;

// get image size
list($src_w, $src_h) = getimagesize($dir);

if ($src_w > $max_w) {
    $ratio = $max_w / $src_w;
    $max_w = $src_w * $ratio;
}
else if ($src_h > $max_h) {
    $ratio = $max_h / $src_h;
    $max_h = $src_h * $ratio;
}

// resize image to $max_w and $max_h, and also save alpha
$src = imagecreatefromstring(file_get_contents($dir));
$new_img = imagecreatetruecolor($max_w, $max_h);
imagealphablending($new_img, false);
imagesavealpha($new_img, true);
imagecopyresampled($new_img, $src, 0, 0, 0, 0, $max_w, $max_h, $src_w, $src_h);

// create a new image with $max_w and $max_h
$maxsize = imagecreatetruecolor($max_w, $max_h);

// add 1x1 gif and resize, then copy over $maxsize
$background = imagecreatefromgif("http://i.imgur.com/atRjCdk.gif"); // transparent 1x1 gif
imagecopyresampled($maxsize, $background, 0, 0, 0, 0, $max_w, $max_h, 1, 1);

// allocate transparency
$transparent = imagecolortransparent($maxsize);
$transparent_index = imagecolorallocate($maxsize, $transparent['red'], $transparent['green'], $transparent['blue']);
imagecolortransparent($maxsize, $transparent_index);

// image copy
imagecopymerge($maxsize, $new_img, 0, 0, 0, 0, $max_w, $max_h, 100);

// save or output
imagegif($maxsize, $path_to_save);

// destroy images
imagedestroy($maxsize);
imagedestroy($new_img);
imagedestroy($background);
imagedestroy($src);

【讨论】:

  • 经过测试......并且工作......你得到什么错误? demosource
猜你喜欢
  • 2010-12-14
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 2020-08-31
  • 2020-01-03
  • 2016-02-27
相关资源
最近更新 更多