2014-02-06 51 views
0

即時嘗試覆蓋到一個JPEG的PNG,到目前爲止它的工作,但由於某種原因,PNG阿爾法沒有被使用,你可以看到它有一個黑色的背景,它應該透明。 enter image description herePHP疊加圖像,阿爾法正在丟失

代碼:

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

回答

1

沒關係,現在找到了答案。這是我完成的功能。工作只是我需要它,應該可能會添加一些錯誤處理雖然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; 
}