Php 将3幅图像与透明度相结合
我在这个项目上的工作,我想结合成一个3个图像 到目前为止,有效的方法是使每个图像(jpg)透明。 但是在将它们合并成新的png图像后,透明度就消失了 代码如下:Php 将3幅图像与透明度相结合,php,image-processing,Php,Image Processing,我在这个项目上的工作,我想结合成一个3个图像 到目前为止,有效的方法是使每个图像(jpg)透明。 但是在将它们合并成新的png图像后,透明度就消失了 代码如下: function CreateMyCoolOutfitBaby () { $Outfitwidth = 250; $Outfitheight = 350; $newoutfit = imagecreatetruecolor($Outfitwidth, $Outfitheight); // create emp
function CreateMyCoolOutfitBaby () {
$Outfitwidth = 250;
$Outfitheight = 350;
$newoutfit = imagecreatetruecolor($Outfitwidth, $Outfitheight); // create empty new image
$dress = imagecreatefromstring(file_get_contents("http://images180.affili.net/001089/c/bb9c33888aae07d839b6724e31f462bc.jpg"));
imagealphablending($dress, true);
$white = imagecolorallocate($dress, 255, 255, 255);
imagecolortransparent($dress, $white);
$bag = imagecreatefromstring(file_get_contents("http://images180.affili.net/000698/6/40afc9ed65a94177635d1c7675fd3756.jpg"));
imagealphablending($bag, true);
$white = imagecolorallocate($bag, 255, 255, 255);
imagecolortransparent($bag, $white);
$shoe = imagecreatefromstring(file_get_contents("http://images180.affili.net/000389/4/4482beed9a949f895debe13d9dd28704.jpg"));
imagealphablending($shoe, true);
$white = imagecolorallocate($shoe, 255, 255, 255);
imagecolortransparent($shoe, $white);
imagealphablending($newoutfit,true); //on each new layer.
// UN-COMMENT THIS PARAGRAPH TO SEE SINGLE FILES BEING TRANSPARENT
//header('Content-Type: image/png');
//imagepng($dress); // output to the browser
//imagepng($bag); // output to the browser
//imagepng($shoe); // output to the browser
// WORKS TILL HERE :)
// get with and height from images
$Dresswidth = imagesx($dress); $Bagwidth = imagesx($bag); $Shoewidth = imagesx($shoe);
$Dressheight = imagesy($dress); $Bagheight = imagesy($bag); $Shoeheight = imagesy($shoe);
// Calc Dress Position (middle)
$DressPosWidth = ( ( $Outfitwidth / 2 ) - ( $Dresswidth / 2 ) );
$DressPosHeight = ( ( $Outfitheight / 2 ) - ( $Dressheight / 2 ) );
// Calc Bag Position (right beside dress)
$BagPosWidth = ( ( $Outfitwidth / 2 ) - ( $Bagwidth / 2 ) + 60 ); // place bag in middle but more to the right
$BagPosHeight = ( ( $Outfitheight / 2 ) - ( $Bagheight / 2 ) );
// Calc Shoe Position (middle under dress)
$ShoePosWidth = ( ( $Outfitwidth / 2 ) - ( $Shoewidth / 2 ) );
$ShoePosHeight = ( ( $Outfitheight / 2 ) - ( $Shoeheight / 2 ) + 100); // place further down
// merge images together
imagecopy($newoutfit,$dress,$DressPosWidth,$DressPosHeight,0,0,$Dresswidth,$Dressheight);
imagealphablending($newoutfit,true);
imagecopy($newoutfit,$bag,$BagPosWidth,$BagPosHeight,0,0,$Bagwidth,$Bagheight);
imagealphablending($newoutfit,true);
imagecopy($newoutfit,$shoe,$ShoePosWidth,$ShoePosHeight,0,0,$Shoewidth,$Shoeheight);
imagealphablending($newoutfit,true);
// make sure alpha is saved
imagesavealpha($newoutfit, true);
imagealphablending($newoutfit, true);
// output to browser
header('Content-Type: image/png');
imagepng($newoutfit); // output to the browser
imagedestroy($newoutfit);
}
因此,单个图像的透明度工作良好。就在$NewFaile推出时,我得到了一个黑色背景,图像又回到了白色背景
我能做什么?使用以下方法修复了此问题:
有效,谢谢!但您更改的内容超过imagecopymerge()。您先将背景设置为黑色,然后设置为透明。。。。干得好,谢谢@DoJoChi:提交的代码通常不受您的版权限制。你无权要求删除它。请参阅本页页脚处的版权。事实上,如果您计划在产品中使用此代码,您必须将其归属于HamZa和Stack Overflow。@DoJoChi这一点正在讨论中,如果您愿意,我们将邀请您参与。@DoJoChi如果它受您的版权保护(实际上并非如此),既然哈姆扎帮助了你的产品,你打算怎么付钱给他?@DoJoChi-我不能代表哈姆扎说话,但他可能会删除任何提及你公司名称的内容,所以答案仍然是一般性的。这种情况偶尔会发生在SSL问题上。我们获取域名,发现问题,然后根据请求将其更改为example.com
。请记住,历史总是可用的,因此您只能在一定程度上适应。
<?php
CreateMyCoolOutfitBaby ();
function CreateMyCoolOutfitBaby () {
$Outfitwidth = 250;
$Outfitheight = 350;
$newoutfit = imagecreatetruecolor($Outfitwidth, $Outfitheight); // create empty new image
imagealphablending($newoutfit, true);
$black = imagecolorallocate($newoutfit, 0, 0, 0);imagecolortransparent($newoutfit, $black); // Making it transparent
$dress = imagecreatefromstring(file_get_contents('http://example.com/dress.jpg'));
imagealphablending($dress, true);
$white = imagecolorallocate($dress, 255, 255, 255);
imagecolortransparent($dress, $white);
$bag = imagecreatefromstring(file_get_contents('http://example.com/bag.jpg'));
imagealphablending($bag, true);
$white = imagecolorallocate($bag, 255, 255, 255);
imagecolortransparent($bag, $white);
$shoe = imagecreatefromstring(file_get_contents('http://example.com/shoe.jpg'));
imagealphablending($shoe, true);
$white = imagecolorallocate($shoe, 255, 255, 255);
imagecolortransparent($shoe, $white);
// get with and height from images
$Dresswidth = imagesx($dress); $Bagwidth = imagesx($bag); $Shoewidth = imagesx($shoe);
$Dressheight = imagesy($dress); $Bagheight = imagesy($bag); $Shoeheight = imagesy($shoe);
// Calc Dress Position (middle)
$DressPosWidth = ( ( $Outfitwidth / 2 ) - ( $Dresswidth / 2 ) );
$DressPosHeight = ( ( $Outfitheight / 2 ) - ( $Dressheight / 2 ) );
// Calc Bag Position (right beside dress)
$BagPosWidth = ( ( $Outfitwidth / 2 ) - ( $Bagwidth / 2 ) + 60 ); // place bag in middle but more to the right
$BagPosHeight = ( ( $Outfitheight / 2 ) - ( $Bagheight / 2 ) );
// Calc Shoe Position (middle under dress)
$ShoePosWidth = ( ( $Outfitwidth / 2 ) - ( $Shoewidth / 2 ) );
$ShoePosHeight = ( ( $Outfitheight / 2 ) - ( $Shoeheight / 2 ) + 100); // place further down
// merge images together
imagecopymerge($newoutfit,$dress,$DressPosWidth,$DressPosHeight,0,0,$Dresswidth,$Dressheight,100);
imagealphablending($newoutfit,true);
imagecopymerge($newoutfit,$bag,$BagPosWidth,$BagPosHeight,0,0,$Bagwidth,$Bagheight,100);
imagealphablending($newoutfit,true);
imagecopymerge($newoutfit,$shoe,$ShoePosWidth,$ShoePosHeight,0,0,$Shoewidth,$Shoeheight,100);
imagealphablending($newoutfit,true);
header('Content-Type: image/png');
imagepng($newoutfit); // output to the browser
imagedestroy($newoutfit);
}