Solved

put a png watermark on an image jpg

Posted on 2010-08-18
6
676 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 109

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 look for a specific file type in a local or remote server directory using PHP.

821 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