Solved

put a png watermark on an image jpg

Posted on 2010-08-18
6
661 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
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:captainmish
Comment Utility
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
Comment Utility
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
Comment Utility
you replaced variablename


this does not work
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:captainmish
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now