Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

put a png watermark on an image jpg

Posted on 2010-08-18
6
Medium Priority
?
690 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 1000 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
Industry Leaders: 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 …
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
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 …

688 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