簡體   English   中英

PHP imagecopy與透明背景

[英]PHP imagecopy with transparent background

我使用此代碼從另一個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);

這樣做有簡單的方法嗎? 謝謝

設置給定圖像中的透明顏色。

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

這是鏈接

由於PHP函數imagecopymerge不能與Alpha通道一起使用,因此您需要使用此頁面上第imagecopymerge_alpha注釋中的函數imagecopymerge_alphahttp//php.net/manual/en/function.imagecopymerge.php

只需將透明圖像作為基礎,並將其與您需要的圖像合並在一起。

我已經試過了,它適用於我的一個項目。

imagealphablending($input, true);
imagesavealpha($input, true);

imagealphablending($output, true);
imagesavealpha($output, true);

這些解決方案都不適用於我,它總是會將源圖像上的透明像素轉換為目標圖像上的黑色。 有用的是將imagecopy / imagecopymerge / imagecopymerge_alpha改為imagecopyresampled,只是將相同的寬度和高度傳遞兩次。

    //Create destination image.
    $png = imagecreatetruecolor(1024, 1024);
    imagealphablending($png, false);
    imagesavealpha($png, true);
    //Make destination image be all transparent.
    $color = imagecolorallocatealpha($png, 0, 0, 0, 127); //127 means completely transparent.
    imagefill($png, 0, 0, $color);

    //Load source image.
    $png2 = imagecreatefrompng($sourceurl);
    imagealphablending($png2, false);
    imagesavealpha($png2, true);
    $sizex = imagesx($png2);
    $sizey = imagesy($png2);

    //Copy to destination and save to file.
    imagecopyresampled( $png, $png2, 
    0, 0,
    0, 0, 
    $sizex, $sizey, 
    $sizex, $sizey);
    imagepng($png, "result.png");

完全歸功於: 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);

或者可以

int imagesavealpha($img,true);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM