Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么透明背景不';你不能和GD一起工作吗?_Php_Gd - Fatal编程技术网

Php 为什么透明背景不';你不能和GD一起工作吗?

Php 为什么透明背景不';你不能和GD一起工作吗?,php,gd,Php,Gd,我正在使用PHP库GD创建图像: $image = imagecreatefrompng('http://polariton.ad-l.ink/8rRSf4CpN/thumb.png'); $w = imagesx($image); $h = imagesy($image); $border = imagecreatefrompng('https://www.w3schools.com/css/border.png'); // New border width $x1 = $w; $x2

我正在使用PHP库GD创建图像:

$image = imagecreatefrompng('http://polariton.ad-l.ink/8rRSf4CpN/thumb.png');
$w = imagesx($image);
$h = imagesy($image);

$border = imagecreatefrompng('https://www.w3schools.com/css/border.png');

// New border width

$x1 = $w;
$x2 = 28;
$x3 = (int)($x1 / $x2);
$x4 = $x1 - $x3 * $x2;
$x5 = $x4 / $x3;
$x2 = $x2 + $x5;

$bw = $x2;

// New border height

$y1 = $h;
$y2 = 28;
$y3 = (int)($y1 / $y2);
$y4 = $y1 - $y3 * $y2;
$y5 = $y4 / $y3;
$y2 = $y2 + $y5;

$bh = $y2;

// New image width and height

$newWidth = (int)$w * (1 - ((($bw * 100) / (int)$w) / 100 * 2));
$newHeight = (int)$h * (1 - ((($bh * 100) / (int)$h) / 100 * 2));

// Transparent border

$indent = imagecreatetruecolor($w, $h);
$color = imagecolorallocatealpha($indent, 0, 0, 0, 127);
imagefill($indent, 0, 0, $color);

imagecopyresampled($indent, $image, $bw, $bw, 0, 0, $newWidth, $newHeight, $w, $h);

// Vertical border

$verticalX = 0;
$verticalY = $bh;

while ($verticalY < $h - $bh) {
    // Left
    imagecopy($indent, $border, $verticalX, $verticalY, 0, 27, $bh, $bh);

    // Right
    imagecopy($indent, $border, $w - $bw, $verticalY, 0, 27, $bh, $bh);

    $verticalY += $bh;
}

// Horizontal border

$horizontalX = $bw;
$horizontalY = 0;

while ($horizontalX < $w - $bw) {
    // Top
    imagecopy($indent, $border, $horizontalX, $horizontalY, 0, 27, $bw, $bw);

    // Bottom
    imagecopy($indent, $border, $horizontalX, $h - $bh, 0, 27, $bw, $bw);

    $horizontalX += $bw;
}

// Left top border
imagecopy($indent, $border, 0, 0, 0, 0, $bw, $bh);

// Right top border
imagecopy($indent, $border, $w - $bw, 0, 0, 0, $bw, $bh);

// Right bottom border
imagecopy($indent, $border, $w - $bw, $h - $bh, 0, 0, $bw, $bh);

// Left bottom border
imagecopy($indent, $border, 0, $h - $bh, 0, 0, $bw, $bh);


// Save result

header('Content-Type: image/png');
imagepng($indent);
$image=imagecreatefrompng('http://polariton.ad-l.ink/8rRSf4CpN/thumb.png');
$w=imagesx($image);
$h=imagesy($image);
$border=imagecreatefrompng('https://www.w3schools.com/css/border.png');
//新边框宽度
$x1=$w;
$x2=28;
$x3=(整数)($x1/$x2);
$x4=$x1-$x3*$x2;
$x5=$x4/$x3;
$x2=$x2+x5;
$bw=$x2;
//新边界高度
$y1=$h;
$y2=28;
$y3=(整数)($y1/$y2);
$y4=$y1-$y3*$y2;
$y5=$y4/$y3;
$y2=$y2+$y5;
$bh=$y2;
//新图像宽度和高度
$newWidth=(int)$w*(1-(($bw*100)/(int)$w)/100*2);
$newHeight=(int)$h*(1-(($bh*100)/(int)$h)/100*2);
//透明边框
$indent=ImageCreateTureColor($w,$h);
$color=imagecolorallocatealpha($indent,0,0,0127);
图像填充($indent,0,0,$color);
imagecopyresampled($indent,$image,$bw,$bw,0,0,$newWidth,$newHeight,$w,$h);
//垂直边界
$verticalX=0;
$verticalY=$bh;
而($verticalY<$h-$bh){
//左
imagecopy($indent、$border、$verticalX、$verticalY、0、27、$bh、$bh);
//对
imagecopy($indent、$border、$w-$bw、$verticalY、0、27、$bh、$bh);
$verticalY+=$bh;
}
//水平边界
$horizontalX=$bw;
$horizontalY=0;
而($horizontalX<$w-$bw){
//顶
imagecopy($indent、$border、$horizontalX、$horizontalY、0、27、$bw、$bw);
//底部
图像复制($indent、$border、$horizontalX、$h-$bh、0、27、$bw、$bw);
$horizontalX+=$bw;
}
//左上边框
imagecopy($indent,$border,0,0,0,$bw,$bh);
//右上边框
imagecopy($indent,$border,$w-$bw,0,0,0,$bw,$bh);
//右下边框
图像复制($indent,$border,$w-$bw,$h-$bh,0,0,$bw,$bh);
//左下边框
imagecopy($indent、$border、0、$h-$bh、0、0、$bw、$bh);
//保存结果
标题('Content-Type:image/png');
imagepng($indent);
但透明度不起作用:

Idk为什么,我做的一切都像文档中写的那样


有什么问题吗?

JPEG文件不支持透明

您可以尝试其他格式,例如PNG

创建新的图像资源后,您需要对代码进行一个小的更改,以便通过调用
imagesavealpha
使输出保持alpha通道的透明度:

...
$indent = imagecreatetruecolor($w, $h);
imagesavealpha($indent, true);
...
然后更改最后两行:

header('Content-Type: image/png');
imagepng($indent);

JPEG文件不支持透明度。确定。我在PNG上更改了图像,并将imagecreatefromjpeg更改为imagecreatefrompng,将imagejpeg($indent,null,100)更改为imagepng($indent)。但仍然不起作用。我更新了消息。非常感谢!