gd - PHP Overlay Image, alpha is being lost -


im trying overlay png onto jpeg, far works, reason pngs alpha not being used, can see has black background , should transparent. enter image description here

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 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); ?> 

never mind, found answer now. here completed function. works how need it, should add 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 memory,can apply more watermarks same image     if (gettype($bg) == "resource") {         $background = $bg;     } else {         $background = imagecreatefromjpeg($bg);     }      //get width , height , generate watermark max width     $bx = imagesx($background);     $by = imagesy($background);     $overlay_max_width = $bx * $p / 100;      //create container image     $imagecontainer = imagecreatetruecolor($bx,$by);      //allow alpha channels saved , fill alpha     imagesavealpha($imagecontainer,true);     $alphacolor = imagecolorallocatealpha($imagecontainer,0,0,0,127);     imagefill($imagecontainer,0,0,$alphacolor);      //copy background image container     imagecopyresampled($imagecontainer,$background,0,0,0,0,$bx,$by,$bx,$by);      //load watermark     $overlay = imagecreatefrompng($wt);      //get watermark width ad height , generate aspect ratio     $ratio = $overlay_max_width / imagesx($overlay);     $newwidth = $overlay_max_width;     $newheight = imagesy($overlay) * $ratio;      //create container watermark , apply alpha     $newoverlay = imagecreatetruecolor($newwidth,$newheight);     imagesavealpha($newoverlay,true);     imagefill($newoverlay,0,0,$alphacolor);      //copy watermark watermark container alpha     imagecopyresized($newoverlay, $overlay, 0, 0, 0, 0, $newwidth, $newheight, imagesx($overlay), imagesy($overlay));      //copy watermark 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 generated image function call further handle     return $imagecontainer; } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -