Jump to content

Archived

This topic is now archived and is closed to further replies.

benjrox

Merging a JPEG and PNG with alpha transparency

Recommended Posts

Hi,
I'm currently trying to merge a jpeg (background image), and a png (border), that is mainly transparent, but has some parts that need 100% opacity.
I've worked almost all of it out, except, when it merges, there's a "white shade" over my background.
You can see what I mean here: [a href=\"http://benjrox.magazinefashion.org/img1.jpg\" target=\"_blank\"]http://benjrox.magazinefashion.org/img1.jpg[/a]
As you can see, the white is there, but is it posibal to get rid of it?

The code I have currently is:
[code]<?php
$bg_file = "/home/www/benjrox.net/myspace/images/". $_GET["img"];
$png_file = "/home/www/benjrox.net/myspace/images/bgtop.png";

$width = "1126";
$height = "562";

list($bgW, $bgH ) = getimagesize($bg_file);
list($borderW, $borderH ) = getimagesize($png_file);

$bgimg = ImageCreateFromJPEG($bg_file);
$borderimg = ImageCreateFromPNG($png_file);

ImageAlphaBlending($bgimg, true);

$resized = imagecreatetruecolor( $width, $height );
imagecopyresampled( $resized, $bgimg, 0, 0, 0, 0, $width, $height, $bgW, $bgH );

ImageCopyMerge( $resized, $borderimg,  0, 0, 0, 0, $borderW, $borderH, 50);

ImageJPEG($resized,FALSE,100);

ImageDestroy($resized);
ImageDestroy($bgimg);
ImageDestroy($borderimg);

?>[/code]

Thanks :)
Benj

Share this post


Link to post
Share on other sites
Well, that was tough, finally figured it out, thanks to some helpful php.net user comments :)
Here's what I had to do:
[code]<?php
  
  function watermark($sourcefile, $watermarkfile) {
  
     $watermarkfile_id = imagecreatefrompng($watermarkfile);
    
     imageAlphaBlending($watermarkfile_id, false);
     imageSaveAlpha($watermarkfile_id, true);
  
     $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));
  
     switch($fileType) {
         case('gif'):
             $sourcefile_id = imagecreatefromgif($sourcefile);
             break;
            
         case('png'):
             $sourcefile_id = imagecreatefrompng($sourcefile);
             break;
            
         default:
             $sourcefile_id = imagecreatefromjpeg($sourcefile);
     }
  
    $sourcefile_width=imageSX($sourcefile_id);
    $sourcefile_height=imageSY($sourcefile_id);
    $watermarkfile_width=imageSX($watermarkfile_id);
    $watermarkfile_height=imageSY($watermarkfile_id);
  
     $dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 );
     $dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 );
    
     if($fileType == 'gif') {
         $tempimage = imagecreatetruecolor($sourcefile_width,
                                                                             $sourcefile_height);
        
         imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0,
                             $sourcefile_width, $sourcefile_height);
        
         $sourcefile_id = $tempimage;
     }
  
     imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0,
                         $watermarkfile_width, $watermarkfile_height);
  
     switch($fileType) {
  
         case('png'):
             header("Content-type: image/png");
             imagepng ($sourcefile_id);
             break;
            
         default:
             header("Content-type: image/jpg");
             imagejpeg ($sourcefile_id);
     }        
  
     imagedestroy($sourcefile_id);
     imagedestroy($watermarkfile_id);
    
  }
  
  $pngFile = "bgtop2.png";
  $bgFile = $_GET["img"] .".jpg";
  
  list($pngW, $pngH) = getimagesize($pngFile);
  list($bgW, $bgH) = getimagesize($bgFile);
  
  $bgImg = imagecreatefromjpeg($bgFile);
  
  $im = imagecreatetruecolor($pngW, $pngH);
  imagealphablending($im, false);
  $col = imagecolorallocatealpha($im, 0, 0, 0, 127);
  imagefilledrectangle($im, 0, 0, $pngW, $pngH, $col);
  imagealphablending($im, true);
  
  imageCopyResized($im, $bgImg, 0,0,0,0,$pngW,$pngH,$bgW,$bgH);
  
  imagealphablending( $im, false );
  imagesavealpha( $im, true );
  imagejpeg( $im, "images/bg/resized/". $_GET["img"] .".jpg", 75 );
  
  watermark("images/bg/resized/". $_GET["img"] .".jpg","bgtop2.png");
  
  ?>    [/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.