7

このコードを使用して、別の png 画像から画像を作成します。背景はデフォルトで黒です。私の質問は、透明な背景を設定する方法ですか?

$input = imagecreatefrompng('image.png');
$output = imagecreatetruecolor(50, 50);

imagecopy($output, $input, 4,0, 8,8, 8,8);
imagecopy... etc.

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

これを行う簡単な方法はありますか?ありがとう

4

6 に答える 6

12

指定されたイメージの透明色を設定します。

int imagecolortransparent ( resource $image [, int $color ] )

リンクはこちら

于 2011-10-11T15:34:31.637 に答える
11

PHP 関数imagecopymergeはアルファ チャネルでは機能しないため、このページの最初のコメントの関数を使用することをお勧めしますimagecopymerge_alpha: http://php.net/manual/en/function.imagecopymerge.php

透明な画像をベースにして、必要な画像とマージするだけです。

私はそれを試してみましたが、私のプロジェクトでは問題なく動作します。

于 2011-10-11T15:32:25.980 に答える
0

完全なクレジットは次のとおりです: http://consistentcoder.com/combine-a-transparent-png-image-on-top-of-another-image-with-php

次のコードは、オーバーレイの透明度を維持しながら、前景画像を背景画像にオーバーレイします。

//set the source image (foreground)
$sourceImage = 'table.png';

//set the destination image (background)
$destImage = 'create-a-surreal-head-of-tree-photo-manipulation.jpg';

//get the size of the source image, needed for imagecopy()
list($srcWidth, $srcHeight) = getimagesize($sourceImage);

//create a new image from the source image
$src = imagecreatefrompng($sourceImage);

//create a new image from the destination image
$dest = imagecreatefromjpeg($destImage);

//set the x and y positions of the source image on top of the destination image
$src_xPosition = 75; //75 pixels from the left
$src_yPosition = 50; //50 pixels from the top

//set the x and y positions of the source image to be copied to the destination image
$src_cropXposition = 0; //do not crop at the side
$src_cropYposition = 0; //do not crop on the top

//merge the source and destination images
imagecopy($dest,$src,$src_xPosition,$src_yPosition,
          $src_cropXposition,$src_cropYposition,
          $srcWidth,$srcHeight);

//output the merged images to a file
/*
 * '100' is an optional parameter,
 * it represents the quality of the image to be created,
 * if not set, the default is about '75'
 */
imagejpeg($dest,
    'combine-a-transparent-png-image-on-top-of-another-image-with-php-01.jpg',
    100);

//destroy the source image
imagedestroy($src);

//destroy the destination image
imagedestroy($dest);
于 2018-10-30T18:47:21.323 に答える
0

またはおそらく

int imagesavealpha($img,true);

http://www.php.net/manual/en/function.imagesavealpha.php

于 2011-10-11T15:42:41.687 に答える