可以使用GD和PHP为透明图像添加背景颜色

发布于 2024-08-06 03:09:11 字数 90 浏览 5 评论 0原文

我有使用 GD 用 php 语言编写的缩略图创建类。我想知道当我上传 png 或 gif 透明图像时,我可以在缩略图中添加背景吗?如果可能的话,请指导我如何做。谢谢。

I have the thumbnail creation class written with php language using GD. I want to know is when i upload the transparent image which is png or gif, can i be able to put background in that thumbnail image ? If that's possible, please kindly guide me how. Thanks.

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

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

发布评论

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

评论(3

永言不败 2024-08-13 03:09:11

这是 PNG 文件的有效解决方案:

$filePath = '';  //full path to your png, including filename and extension
$savePath = '';  //full path to saved png, including filename and extension
$colorRgb = array('red' => 255, 'green' => 0, 'blue' => 0);  //background color

$img = @imagecreatefrompng($filePath);
$width  = imagesx($img);
$height = imagesy($img);

//create new image and fill with background color
$backgroundImg = @imagecreatetruecolor($width, $height);
$color = imagecolorallocate($backgroundImg, $colorRgb['red'], $colorRgb['green'], $colorRgb['blue']);
imagefill($backgroundImg, 0, 0, $color);

//copy original image to background
imagecopy($backgroundImg, $img, 0, 0, 0, 0, $width, $height);

//save as png
imagepng($backgroundImg, $savePath, 0);

Here's a working solution for PNG files:

$filePath = '';  //full path to your png, including filename and extension
$savePath = '';  //full path to saved png, including filename and extension
$colorRgb = array('red' => 255, 'green' => 0, 'blue' => 0);  //background color

$img = @imagecreatefrompng($filePath);
$width  = imagesx($img);
$height = imagesy($img);

//create new image and fill with background color
$backgroundImg = @imagecreatetruecolor($width, $height);
$color = imagecolorallocate($backgroundImg, $colorRgb['red'], $colorRgb['green'], $colorRgb['blue']);
imagefill($backgroundImg, 0, 0, $color);

//copy original image to background
imagecopy($backgroundImg, $img, 0, 0, 0, 0, $width, $height);

//save as png
imagepng($backgroundImg, $savePath, 0);
骷髅 2024-08-13 03:09:11

为什么不:

  1. 创建一张具有所需背景的图像
  2. 在其上方绘制透明图像
  3. 将新图像保存在透明图像上。

Why not:

  1. Create an image with the desired background
  2. Paint the transparent image above it
  3. Save the new image over the transparent one.
饮惑 2024-08-13 03:09:11

非常简单的代码,具有动态背景颜色。

<?php
    function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
        $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
        $rgbArray = array();
        if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
            $colorVal = hexdec($hexStr);
            $rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
            $rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
            $rgbArray['blue'] = 0xFF & $colorVal;
        } elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
            $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
            $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
            $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
        } else {
            return false; //Invalid hex color code
        }
        return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray;die;
    }
    $color_code = hex2RGB('#fff000');

    $imgName = uniqid();
    $filePath = 'old-img/clothing_type_test.png';  //full path to your png, including filename and extension
    $savePath = 'new-img/'.$imgName.'.png';  //full path to saved png, including filename and extension
    $colorRgb = array('red' => $color_code['red'], 'green' => $color_code['green'], 'blue' => $color_code['blue']);  //background color

    $img = @imagecreatefrompng($filePath);
    $width  = imagesx($img);
    $height = imagesy($img);

    $backgroundImg = @imagecreatetruecolor($width, $height);
    $color = imagecolorallocate($backgroundImg, $colorRgb['red'], $colorRgb['green'], $colorRgb['blue']);
    imagefill($backgroundImg, 0, 0, $color);
    imagecopy($backgroundImg, $img, 0, 0, 0, 0, $width, $height);
    imagepng($backgroundImg, $savePath, 0);
?>
<p align="center"><img align="absmiddle" src="<?php echo $savePath;?>" /></p>

Very simple code with dynamic background color.

<?php
    function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
        $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
        $rgbArray = array();
        if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
            $colorVal = hexdec($hexStr);
            $rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
            $rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
            $rgbArray['blue'] = 0xFF & $colorVal;
        } elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
            $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
            $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
            $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
        } else {
            return false; //Invalid hex color code
        }
        return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray;die;
    }
    $color_code = hex2RGB('#fff000');

    $imgName = uniqid();
    $filePath = 'old-img/clothing_type_test.png';  //full path to your png, including filename and extension
    $savePath = 'new-img/'.$imgName.'.png';  //full path to saved png, including filename and extension
    $colorRgb = array('red' => $color_code['red'], 'green' => $color_code['green'], 'blue' => $color_code['blue']);  //background color

    $img = @imagecreatefrompng($filePath);
    $width  = imagesx($img);
    $height = imagesy($img);

    $backgroundImg = @imagecreatetruecolor($width, $height);
    $color = imagecolorallocate($backgroundImg, $colorRgb['red'], $colorRgb['green'], $colorRgb['blue']);
    imagefill($backgroundImg, 0, 0, $color);
    imagecopy($backgroundImg, $img, 0, 0, 0, 0, $width, $height);
    imagepng($backgroundImg, $savePath, 0);
?>
<p align="center"><img align="absmiddle" src="<?php echo $savePath;?>" /></p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文