Reputation: 4885
im trying to overlay a png onto a jpeg, so far it works, but for some reason the pngs alpha is not being used, as you can see it has a black background and it should be transparent.
Code:
<?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);
?>
Upvotes: 0
Views: 430
Reputation: 4885
Never mind, found the answer now. Here is my completed function. Works just how I need it, should probably add some error handling though 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;
}
Upvotes: 2