I'm trying to re-size and then add a logo to a picture that is uploaded via FormHandler.
All is working fine except that I cant get any transparency on the logo. I've tried the logo as a gif and as a png file and I've tried using array(0,0,0) and array(255,255,255) and neither the black or the white on the logo becomes transparent.
And it works with a png stamp?
I did try a png stamp and coildn't get it to work selecting a transparent colour.
Or did you mean to use a png file that has alpha transparency already set?
I have tested the script below and came to the following conclusion:
- Gif files won't work (Tried with: resizeIMG and resizeIMG)
- JPG files won't work (resizeIMG)
- PNG files work if you export them as PNG24 format (dont ask me what that is.. I use photoshop.) PNG8 format isn't working
This one works (png24): resizeIMG
This one isn't (png8): resizeIMG
I hope that this solves the problem.
<?php
$original = $_SERVER['DOCUMENT_ROOT'] . "/images/Image.jpg";
$stamp = $_SERVER['DOCUMENT_ROOT'] . "/images/stamp/png24.png";
// red color transparant
$aTransparant = array(
255, # red
0, # green
0 # blue
);
// open the current file (get the resource )
$rImgSrc = imagecreatefromjpeg( $original );
// get the size of the image
$aSize = getimagesize( $original );
// create the "new" file recourse (the destination)
$rImgDest = imagecreatetruecolor( $aSize[0], $aSize[1] );
// open the stamp image (get the resource)
$rImgStamp = imagecreatefrompng( $stamp );
// transparant color...
if( is_array($aTransparant) )
{
$color = ImageColorAllocate( $rImgStamp, $aTransparant[0], $aTransparant[1], $aTransparant[2] );
ImageColorTransparent( $rImgStamp, $color );
}
// copy the current file to the new one
ImageCopy( $rImgDest, $rImgSrc, 0,0,0,0, $aSize[0], $aSize[1] );
// delete the resource of the original
ImageDestroy( $rImgSrc );
// get the size of the stamp
$x = ImageSX( $rImgStamp );
$y = ImageSY( $rImgStamp );
// X and Y position on the original!
$posX = 100;
$posY = 100;
// copy the stamp to the new image
ImageCopyMerge( $rImgDest, $rImgStamp, $posX, $posY, 0, 0, $x, $y, 100 );
//ImageCopy( $rImgDest, $rImgStamp, $posX, $posY, 0, 0, $x, $y ); # transparant isnt working!
//ImageCopyResampled( $rImgDest, $rImgStamp, 0, 0, $x, $y, $x, $y ); # transparant isnt working!
ImageDestroy( $rImgStamp );
header('Content-Type: image/png');
// Save the new image
imagepng( $rImgDest, '', 100 );
ImageDestroy( $rImgDest );
?>