Solved

put a png watermark on an image jpg

Posted on 2010-08-18
6
679 Views
Last Modified: 2012-05-10
could I put a png image over a jpg image


png image top
jpg image bottom


I think the attached code puts a gif over a jpg
watermarkImage("$filename");


 
function watermarkImage($location)
{
 
//load watermark image
$watermark = imagecreatefromgif("uploaded/canon.gif");
$watermark_height = imagesy($watermark);
$watermark_width = imagesx($watermark); 
 
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromgif($location);
 
$size = getimagesize($location);
 
$x_pos = $size[0] - $watermark_width;
$y_pos = $size[1] - $watermark_height;


imagecopymerge($image,$watermark, $x_pos, $y_pos, 0, 0, $watermark_width, $watermark_height, 100);
imagegif($image, $location,100);



 
imagedestroy($image);
imagedestroy($watermark);
 
}

Open in new window

0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:captainmish
ID: 33468807
Just needs a few lines replaced in that code to use png and jpg::


/*
 * Usage: watermarkImage(path to jpg image, path to watermark png);
 */

 
function watermarkImage($locationjpg, $watermarkpng)
{
 
//load watermark image
$watermark = imagecreatefrompng($watermarkpng);
$watermark_height = imagesy($watermark);
$watermark_width = imagesx($watermark); 
 
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($locationjpg);
 
$size = getimagesize($locationjpg);
 
$x_pos = $size[0] - $watermark_width;
$y_pos = $size[1] - $watermark_height;


imagecopymerge($image,$watermark, $x_pos, $y_pos, 0, 0, $watermark_width, $watermark_height, 100);
imagegif($image, $locationjpg,100);



 
imagedestroy($image);
imagedestroy($watermark);
 
}

Open in new window

0
 
LVL 3

Accepted Solution

by:
captainmish earned 250 total points
ID: 33468836
There are a few places i forgot to replace $watermark with $watermarkpng :
/*
 * Usage: watermarkImage(path to jpg image, path to watermark png);
 */

 
function watermarkImage($locationjpg, $watermarkpng)
{
 
//load watermark image
$watermark = imagecreatefrompng($watermarkpng);
$watermark_height = imagesy($watermarkpng);
$watermark_width = imagesx($watermarkpng); 
 
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($locationjpg);
 
$size = getimagesize($locationjpg);
 
$x_pos = $size[0] - $watermark_width;
$y_pos = $size[1] - $watermark_height;


imagecopymerge($image,$watermarkpng, $x_pos, $y_pos, 0, 0, $watermark_width, $watermark_height, 100);
imagegif($image, $locationjpg,100);



 
imagedestroy($image);
imagedestroy($watermarkpng);
 
}

Open in new window

0
 

Author Comment

by:rgb192
ID: 33469124
you replaced variablename


this does not work
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 3

Expert Comment

by:captainmish
ID: 33469164
as well as the imagecreatefrom functions: jpeg and png, not gif - the variable name changes were to try and make this more obvious: imagecreatefrom*PNG* imagecreatefrom*JPEG*

http://php.net/manual/en/function.imagecreatefrompng.php

http://php.net/manual/en/function.imagecreatefromjpeg.php
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 33470122
A tested and working example is available here:
http://www.laprbass.com/RAY_image_watermark.php
<?php // RAY_image_watermark.php
error_reporting(E_ALL);

// LOCATION OF THE IMAGES
$original = 'http://www.LAPRBass.com/RAY_add_watermark_source_image.jpg';
$h2o_mark = 'http://www.LAPRBass.com/RAY_add_watermark_watermark_image.png'; 

// MAN PAGE http://us3.php.net/manual/en/function.imagecreatefromjpeg.php
$im = ImageCreateFromJPEG($original);
// MAN PAGE http://us3.php.net/manual/en/function.imagecreatefrompng.php
$wm = ImageCreateFromPNG($h2o_mark);

// ADD WATERMARK
imagelogo($im, $wm, imagesx($im), imagesy($im), imagesx($wm), imagesy($wm));

// SHOW THE IMAGE
header('Content-type: image/jpg');
ImageJPEG($im);

// SEE http://us3.php.net/manual/en/function.imagealphablending.php#77085
function imagelogo (&$dst_image, $src_image, $dst_w, $dst_h, $src_w, $src_h) 
{
    ImageAlphaBlending($dst_image,TRUE);
    ImageAlphaBlending($src_image,TRUE);
    ImageCopy($dst_image, $src_image, ($dst_w-$src_w), ($dst_h-$src_h), 0, 0, $src_w, $src_h);
}

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 33582750
thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question