Solved

put a png watermark on an image jpg

Posted on 2010-08-18
6
683 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

632 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