Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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 透明图像上的透明图像_Php_Image_Transparent - Fatal编程技术网

Php 透明图像上的透明图像

Php 透明图像上的透明图像,php,image,transparent,Php,Image,Transparent,我会解释一下我的情况 我们有一个有城市的岛。这些可以是: 属于我的联盟 为敌人所有 无人拥有(免费) 我们得到了3个“城市”——图像和一个岛屿图像,如下所示: <?php // Get image $im = imagecreatefrompng('island.png'); imagealphablending($im,true); // Get our "Free-city-position" image $stamp = imagecreatefrompng('free.png

我会解释一下我的情况

我们有一个有城市的岛。这些可以是:

  • 属于我的联盟
  • 为敌人所有
  • 无人拥有(免费)
我们得到了3个“城市”——图像和一个岛屿图像,如下所示:

<?php
// Get image
$im = imagecreatefrompng('island.png');
imagealphablending($im,true);

// Get our "Free-city-position" image
$stamp = imagecreatefrompng('free.png');

$pos_x = 190 - 15; // Position X = 190 - the half of the free.png image = 30 / 2 = 15
$pos_y = 225 - 15;// Position Y = 225 - the half of the free.png image = 30 / 2 = 15

imagealphablending($stamp,true);
imagecopy($im, $stamp, $pos_x, $pos_y, 0, 0, imagesx($stamp), imagesy($stamp));

// Output image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

现在我们想把这些城市的图片放到岛上的图片上。例如,我们在岛上放置了一个无人拥有的城市,如下所示:

<?php
// Get image
$im = imagecreatefrompng('island.png');
imagealphablending($im,true);

// Get our "Free-city-position" image
$stamp = imagecreatefrompng('free.png');

$pos_x = 190 - 15; // Position X = 190 - the half of the free.png image = 30 / 2 = 15
$pos_y = 225 - 15;// Position Y = 225 - the half of the free.png image = 30 / 2 = 15

imagealphablending($stamp,true);
imagecopy($im, $stamp, $pos_x, $pos_y, 0, 0, imagesx($stamp), imagesy($stamp));

// Output image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

现在我们的问题是:城市形象在岛屿形象上是不透明的!看起来是这样的:

我原以为
imagealphabling
应该做到这一点,但不幸的是,事实并非如此


我们如何在岛上获得透明的城市形象?

摘自PHP对GD函数的评论:

如果试图将透明图像复制到另一个图像上,则 可能假设您应该将ImageAlphaBlending函数应用于 具有透明度的图像,即源图像。事实上,你 必须将ImageAlphaBlending功能应用于目标图像。 基本上是说,“让特定的图像得到尊重 透明性”

试着这样做:

#!/usr/bin/php -f
<?php
// Read island image and get dimensions
$island=imagecreatefrompng("island.png");
$w_island=imagesx($island);
$h_island=imagesy($island);

// Read city image and get dimensions
$city= imagecreatefrompng("city.png");
$w_city=imagesx($city);
$h_city=imagesy($city);

// Create output image
$result=imagecreatetruecolor($w_island,$h_island);
imagesavealpha($result,true);
$transparent=imagecolorallocatealpha($result, 0, 0, 0, 127);
imagefill($result, 0, 0, $transparent);

// Splat island onto transparent background
imagecopy($result, $island, 0, 0, 0, 0, $w_island, $h_island);
// Splat city ontop
imagecopy($result, $city, 100, 280, 0, 0, $w_city, $h_city);
imagepng($result,"result.png");
?>
#/usr/bin/php-f

这意味着我必须在
标题(…)
上方添加
imagealphabling
函数,以
$im
?遗憾的是,这不起作用……不,这意味着,只将imagealphablending设置为背景图像,而不是前景图像。请参见第2条注释,但这不是解决方案。当我将图像更改为JPG(JPEG)图像时,它可以工作。有没有PNG图像的解决方案?也许可以检查这个线程