我试图将png叠加到jpeg上,到目前为止它可以工作,但由于某些原因,pngs alpha没有被使用,因为你可以看到它有一个黑色的背景,它应该是透明的。
代码:
<?php
header('Content-Type: image/jpeg');
$source_image = imagecreatefromjpeg("data/images/20140123/0/sexy-briana-evigan-gets-worked-up-and-wet.jpg");
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$overlay_max_width = $source_imagex * 40 / 100;
//Resize overlay to fit
$overlay = imagecreatefrompng("overlay.png");
$overlay_imagex = imagesx($overlay);
$overlay_imagey = imagesy($overlay);
$ratio = $overlay_max_width / $overlay_imagex;
$newwidth = $overlay_max_width;
$newheight = $overlay_imagey * $ratio;
//Make new size
$overlay_image = imagecreatetruecolor($newwidth,$newheight);
imagecopyresized($overlay_image, $overlay, 0, 0, 0, 0, $newwidth, $newheight, $overlay_imagex, $overlay_imagey);
imagecopymerge($source_image, $overlay_image,$source_imagex-$newwidth,$source_imagey-$newheight,0,0, imagesx($source_image),imagesy($source_image),100);
imagejpeg($source_image,null,100);
?>
答案 0 :(得分:2)
没关系,现在找到了答案。这是我完成的功能。工作正常我需要它,应该添加一些错误处理虽然tbf
//background(url/resource), watermarl(url), size percent, left/right, padding(px)
function apply_watermark($bg,$wt,$p,$lr,$pad=0) {
//Load background image into memory,can apply more watermarks to same image
if (gettype($bg) == "resource") {
$background = $bg;
} else {
$background = imagecreatefromjpeg($bg);
}
//Get the width and height and generate watermark max width
$bx = imagesx($background);
$by = imagesy($background);
$overlay_max_width = $bx * $p / 100;
//Create container for image
$imagecontainer = imagecreatetruecolor($bx,$by);
//Allow alpha channels to be saved and fill it with alpha
imagesavealpha($imagecontainer,true);
$alphacolor = imagecolorallocatealpha($imagecontainer,0,0,0,127);
imagefill($imagecontainer,0,0,$alphacolor);
//Copy background image into the container
imagecopyresampled($imagecontainer,$background,0,0,0,0,$bx,$by,$bx,$by);
//Load the watermark
$overlay = imagecreatefrompng($wt);
//get the watermark width ad height and generate the aspect ratio
$ratio = $overlay_max_width / imagesx($overlay);
$newwidth = $overlay_max_width;
$newheight = imagesy($overlay) * $ratio;
//Create container for the watermark and apply alpha to it
$newoverlay = imagecreatetruecolor($newwidth,$newheight);
imagesavealpha($newoverlay,true);
imagefill($newoverlay,0,0,$alphacolor);
//Copy the watermark to the watermark container with alpha
imagecopyresized($newoverlay, $overlay, 0, 0, 0, 0, $newwidth, $newheight, imagesx($overlay), imagesy($overlay));
//Copy the watermark to the background image container, choose left or right
if ($lr == 0) {
imagecopyresampled($imagecontainer,$newoverlay,0+$pad,($by-$newheight-$pad),0,0,$newwidth,$newheight,$newwidth,$newheight);
} elseif ($lr == 1) {
imagecopyresampled($imagecontainer,$newoverlay,($bx-$newwidth-$pad),($by-$newheight-$pad),0,0,$newwidth,$newheight,$newwidth,$newheight);
}
//Return the generated image back to the function call to further handle
return $imagecontainer;
}