PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Nov 24, 2017 1:35 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sun Nov 06, 2005 1:53 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
I have a 200 X 200 pixel watermark, and a 4,000 X 4,000 destination image. I want to copy the watermark ontop of the destination image and stretch the watermark, so I resample the watermark:

Syntax: [ Download ] [ Hide ]
$watermark = imagecreatetruecolor($width_photo_orig, $height_photo_orig);

$watermark_orig = imagecreatefrompng('watermark.png');

imageAlphaBlending($watermark_orig, false);

imageSaveAlpha($watermark_orig, true);

imagecopyresampled($watermark, $watermark_orig, 0, 0, 0, 0, $width_photo_orig, $height_photo_orig, $width_water_orig, $height_water_orig);


Problem is now the watermark is black where it should be transparent because imagecreatetruecolor creates a black image. Then the transparency was simply lost, now watermarking this image creates a black image with a watermark overriding my original photo. Is there a way for imagecreatetruecolor to produce a transparent image to start with, or for imagecopyresample to preserve transparency?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 7:47 am 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
It sounds a bit mad, but you need to set imageAlphaBlending($watermark_orig, true); rather than false, and remove the imageSaveAlpha($watermark_orig, true); line altogether.

Syntax: [ Download ] [ Hide ]
<?php



        $file = "wm.jpg"; // Original image to have watermark applied to it.

        $wm = "watermark.png"; // Watermark image



        $image = imagecreatefromjpeg($file);

        imagealphablending($image,true); // Tell PHP to use alpha blending when the destination image is $image

       

        $wm_image = imagecreatefrompng($wm);

        imagecopyresampled($image,$wm_image,0,0,0,0,imagesx($image),imagesy($image),imagesx($wm_image),imagesy($wm_image));



        header("Content-type: image/jpeg");

        imagejpeg($image);



?>

Obviously that code applies the watermark directly to the image instead of going via an image created with imagecreatetruecolour(). If you really need to do that I have an imagecreatetruecolortransparent() function that should do the job ..
Syntax: [ Download ] [ Hide ]
function imagecreatetruecolortransparent($x,$y) {

                $i = imagecreatetruecolor($x,$y);

                $b = imagecreatefromstring(base64_decode(blankpng()));

                imagealphablending($i,false);

                imagesavealpha($i,true);

                imagecopyresized($i,$b,0,0,0,0,$x,$y,imagesx($b),imagesy($b));

                return $i;

        }



        function blankpng() {



                $c  = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m";

                $c .= "dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg";

                $c .= "dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN";

                $c .= "egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ";

                $c .= "oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA";

                $c .= "DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=";



                return $c;



        }



        $image = imagecreatetruecolortransparent(200,200);

       

        header("Content-type: image/png");

        imagepng($image);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 3:56 pm 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
If you don't fancy resizing the watermark you could tile it over the image instead..

Syntax: [ Download ] [ Hide ]
<?php



        $file = "wm.jpg";

        $wm = "watermark.png";



        $image = imagecreatefromjpeg($file);

        imagealphablending($image,true);



        $wm_image = imagecreatefrompng($wm);



        imageSetTile($image,$wm_image);

        imagefilledrectangle($image,0,0,imagesx($image),imagesy($image),IMG_COLOR_TILED);



        header("Content-type: image/jpeg");

        imagejpeg($image);



?>

Sadly you can't use imagefill() unless your image is blank to start with.. coz it's a fill and only goes up to boundaries..


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 7:36 am 
Offline
Forum Newbie

Joined: Tue Apr 24, 2012 7:17 am
Posts: 1
nice informative post i like it....


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group