[英]PHP imagecopy & imagecopymerge changing background from transparent to black
[英]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);
這樣做有簡單的方法嗎? 謝謝
由於PHP函數imagecopymerge
不能與Alpha通道一起使用,因此您需要使用此頁面上第imagecopymerge_alpha
注釋中的函數imagecopymerge_alpha
: http : //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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.