|
Posted by Chuck Anderson on 03/20/07 05:33
j.l.l@web.de wrote:
> Rami Elomaa schrieb:
>
>> Chuck Anderson kirjoitti:
>>
>>> Is it possible to overlay a transparent watermark on an image -
>>> dynamically?
>>>
>
>
>> Yeah, I've done this. What you do is you open image handles to both
>> files, the original image (imagecreatefromjpg) and the png watermark
>> (imagecreatefrompng), then combine them with either imagecopy,
>> imagecopyresampled or some other imagecopy* function that works best for
>> you. Just go ahead and try them.
>>
>
> I've done one transparent overlay of two pictures, too, but found it a
> bit tricky to do.
>
> You have to be careful with using imagecopymerge. As i remember there is
> a parameter to allow transparency to be used.
>
> I wonder if different imagetypes can be used without any probs? Please
> tell if you have tested it, i'm curious about it.
>
> moido apinoiden planeetalta!
>
Okay, thanks for all the help. I see now that the imagecopy....
functions do what I need. The only one that lets you adjust the
transparency of the overlaying image, though, is imagecopymerge.
One big problem is that I am having no luck with using a png for the
overlay. There is a background color, not transparency.
I've added more to the example page.
<http://www.cycletourist.com/temp/photo.php>
The top image is one I created in Paint Shop Pro by pasting the
transparent png over a photo image multiple times. I really like the
look of the transparency there.
The two gray boxes under the photo contain the png and gif images I am
using for the watermark. The background behind the watermark images is
set with CSS. The png version is 24 bit with alpha channel
transparency. The gif image had to be blended with some color so I
chose a light medium gray (which you can see on top of the background).
If you click on either of those you can see the respective Php generated
results.
Using a transparent gif for the watermark when generating the image with
Php looks shoddy in comparison to the one I made in Paint Shop Pro.
The png watermark is not even transparent ... and I don't know why. I
wonder if it would look better than the gif version, but I can't figure
out how to make it overlay and be transparent.
So, ... questions ....
1. Can I get results more like the image I created in Paint Shop Pro?
2. Why is the png not transparent when I use imagecopymerge (and will it
look better if I get it working)?
Here is the Php script I am using:
<?php
function display_with_watermark($src, $dest, $png=0)
{
// use less than 100% opacity in imagecopymerge to improve the appearance
$pct = 70;
if ($png)
$watermark = imagecreatefrompng('cycletourist_x2.png');
else
$watermark = imagecreatefromgif('cycletourist_x2.gif');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($src);
$size = getimagesize($src);
// center the watermark
$dest_x = ($size[0] - $watermark_width)/2;
$dest_y = $size[1]/2 - $watermark_height/2;
// lay it on 3 times - upper, middle, and lower
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0,
$watermark_width, $watermark_height, $pct);
imagecopymerge($image, $watermark, $dest_x,
$dest_y/2 - $watermark_height/2, 0, 0, $watermark_width,
$watermark_height, $pct);
imagecopymerge($image, $watermark, $dest_x,
($dest_y + $dest_y/2) + $watermark_height/2, 0, 0,
$watermark_width, $watermark_height, $pct);
header('Content-Type: image/jpg');
header('Content-Disposition: inline; filename=' . $src);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
}
$src = '../Scenes/St_Peters_Basilica.jpg';
display_with_watermark($src, $dest, 0); // 0 = gif watermark, 1 = png
?>
--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Navigation:
[Reply to this message]
|