Possible to add background color to transparent image using GD and PHP

本小妞迷上赌 提交于 2019-11-28 05:29:13

问题


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.


回答1:


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);



回答2:


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.



回答3:


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>


来源:https://stackoverflow.com/questions/1496169/possible-to-add-background-color-to-transparent-image-using-gd-and-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!