Solved

put watermark on an image

Posted on 2010-08-13
13
560 Views
Last Modified: 2012-05-10
this code puts a watermark on an image

i want to put an image on a watermark


I tried reversing these lines
but no watermark was visible

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

<?php

 

//upload image

$image_uploaded = $_FILES['image'] ['name'];

$image_uploaded_temp = $_FILES['image'] ['tmp_name'];

 

$location="uploaded/$image_uploaded"; 

move_uploaded_file($image_uploaded_temp, $location);

 

 

// call function

watermarkImage("$location");







 

function watermarkImage($location)

{

 

//load watermark image

$watermark = imagecreatefromjpeg("uploaded/c.jpg");

$watermark_height = imagesy($watermark);

$watermark_width = imagesx($watermark); 

 

$image = imagecreatetruecolor($watermark_width, $watermark_height);

$image = imagecreatefromjpeg($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); 

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







imagejpeg($image, $location,100);





 

imagedestroy($image);

imagedestroy($watermark);

 

}

 

 

 

 

?>

Open in new window

0
Comment
Question by:rgb192
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33431593
you need to

imagejpeg($watermark,$location,100);

the current way you are putting your image on your watermark, and then saving your original image not the watermaker image.
0
 

Author Comment

by:rgb192
ID: 33431656
puts a
black square on
watermark
0
 

Author Comment

by:rgb192
ID: 33431690
imagecopymerge($image, $watermark, $x_pos, $y_pos, 0, 0, $watermark_width, $watermark_height, 100);
//imagejpeg($image, $location,100);

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


shows only watermark


------------------------------------------

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

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


puts a
black square on
watermark
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33432032
did you comment out or change the order of these statements?

$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($location);

imagecreatetruecolor will create a black, image of the specified size. Which would cause you to end up with a black square image on your watermark.
0
 

Author Comment

by:rgb192
ID: 33432208

Warning: imagecopymerge(): supplied argument is not a valid Image resource in

Warning: imagedestroy(): supplied argument is not a valid Image resource in


and no image is changed
<?php

 

//upload image

$image_uploaded = $_FILES['image'] ['name'];

$image_uploaded_temp = $_FILES['image'] ['tmp_name'];

 

$location="uploaded/$image_uploaded"; 

move_uploaded_file($image_uploaded_temp, $location);









// This is the temporary file created by PHP

//$uploadedfile = $_FILES['uploadfile']['tmp_name'];

$uploadedfile=$location;



// Create an Image from it so we can do the resize

$src = imagecreatefromjpeg($uploadedfile);



// Capture the original size of the uploaded image

list($width,$height)=getimagesize($uploadedfile);



// For our purposes, I have resized the image to be

// 600 pixels wide, and maintain the original aspect

// ratio. This prevents the image from being "stretched"

// or "squashed". If you prefer some max width other than

// 600, simply change the $newwidth variable

$newwidth=100;

$newheight=($height/$width)*100;

$tmp=imagecreatetruecolor($newwidth,$newheight);



// this line actually does the image resizing, copying from the original

// image into the $tmp image

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);



// now write the resized image to disk. I have assumed that you want the

// resized, uploaded image file to reside in the ./images subdirectory.

//$filename = "images/". $_FILES['uploadfile']['name'];

//$filename = "uploaded/newfile.jpg";

$secret="uploaded/secretfile.jpg";

$filename = "$secret";

imagejpeg($tmp,$filename,100);



imagedestroy($src);

imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request

// has completed.





 

// call function

//watermarkImage("$location");

//watermarkImage("$uploadedfile");

watermarkImage("$secret");





 

function watermarkImage($location)

{

 

//load watermark image

$watermark = imagecreatefromjpeg("uploaded/c.jpg");

$watermark_height = imagesy($watermark);

$watermark_width = imagesx($watermark); 

 

//$image = imagecreatetruecolor($watermark_width, $watermark_height);

//$image = imagecreatefromjpeg($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);

imagejpeg($watermark, $location,100);





 

imagedestroy($image);

imagedestroy($watermark);

 

}

 

 

 

 

?>

Open in new window

0
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 167 total points
ID: 33432469
Hi, sorry, I wasn't asking you to comment those lines 65, and 66. I was asking if you had, because if you had either commented out 66 or swapped 65 and 66, I would have expected the result you were getting.

The code you have listed there is going to take whatever is in the $watermark, and place it into the $image image. But then you are writing to disk with imagejpg the $watermark image.  So no, nothing will have changed.

So you either want to be writing out $image, or in imagecopymerge switching $image and $watermark
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:rgb192
ID: 33432801
okay

I uncommented those lines
<?php

 

//upload image

$image_uploaded = $_FILES['image'] ['name'];

$image_uploaded_temp = $_FILES['image'] ['tmp_name'];

 

$location="uploaded/$image_uploaded"; 

move_uploaded_file($image_uploaded_temp, $location);









// This is the temporary file created by PHP

//$uploadedfile = $_FILES['uploadfile']['tmp_name'];

$uploadedfile=$location;



// Create an Image from it so we can do the resize

$src = imagecreatefromjpeg($uploadedfile);



// Capture the original size of the uploaded image

list($width,$height)=getimagesize($uploadedfile);



// For our purposes, I have resized the image to be

// 600 pixels wide, and maintain the original aspect

// ratio. This prevents the image from being "stretched"

// or "squashed". If you prefer some max width other than

// 600, simply change the $newwidth variable

$newwidth=100;

$newheight=($height/$width)*100;

$tmp=imagecreatetruecolor($newwidth,$newheight);



// this line actually does the image resizing, copying from the original

// image into the $tmp image

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);



// now write the resized image to disk. I have assumed that you want the

// resized, uploaded image file to reside in the ./images subdirectory.

//$filename = "images/". $_FILES['uploadfile']['name'];

//$filename = "uploaded/newfile.jpg";

$secret="uploaded/secretfile.jpg";

$filename = "$secret";

imagejpeg($tmp,$filename,100);



imagedestroy($src);

imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request

// has completed.





 

// call function

//watermarkImage("$location");

//watermarkImage("$uploadedfile");

watermarkImage("$secret");





 

function watermarkImage($location)

{

 

//load watermark image

$watermark = imagecreatefromjpeg("uploaded/c.jpg");

$watermark_height = imagesy($watermark);

$watermark_width = imagesx($watermark); 

 

$image = imagecreatetruecolor($watermark_width, $watermark_height);

$image = imagecreatefromjpeg($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);

imagejpeg($watermark, $location,100);





 

imagedestroy($image);

imagedestroy($watermark);

 

}

 

 

 

 

?>

Open in new window

0
 
LVL 33

Expert Comment

by:Slick812
ID: 33433251
greetings rgb192, you do not seem to understand what images you have and what you are doing with them. Maybe you should start with a simpler operation and do more as you learn what the GD functions are about.
you have -
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($location);

this does not make any sense , as you use the same variable $image for each, the second function will just replace $image, so the first function is worthless.
in your imagecopymerge( ) function you use 100 as you pct (transparency), which will completely OVERCOPY any image below the source image, this may be why you do not see the watermark, as the $location image is likely  larger.

Below I have two PHP functions in a web page for you to try, I do NOT try and get proper positioning for a watermark, since I am completely stumped and do not understand why you want to place an image on a watermark? ?
I intend these to help you see what images are produced and what the copy to another image may be doing.
Please ask questions if you need more information.

WARNING you will need to change ALL of the image paths to JPG images on your machine



<html><head><title>Trans PNG test</title></head><BODY BGCOLOR="#E3F7FF">
<?php

function watermarkImage($location){
/*
	I have made TWO functions, each to show you how to make changes in the
	source and destination images
	
	you seem to have no idea about image sizes and copy position that will
	change the output image. if you change the destination Image AND the source
	Image, then you also will need to change the POSITIONING and the size of copy 
	which in this case is the $watermark_height and $watermark_width
*/

// change image path to a JPG in your folder
$watermark = imagecreatefromjpeg('images3/inspir1.jpg');// should be SMALLER image than $location
$watermark_height = imagesy($watermark);
$watermark_width = imagesx($watermark); 
 
$image = imagecreatefromjpeg($location);

// for learning purposes I do NOT use the positioning $y_pos and $x_pos
//$y_pos = imagesy($image) - $watermark_height;
//$x_pos = imagesx($image) - $watermark_width; 

// warning - for learning purposes I place both images at 0 , 0
// warning - for learning I set the transpaency (pct) to 50 percent in imagecopymerge
// $image is destination
imagecopymerge($image,$watermark, 0, 0, 0, 0, $watermark_width, $watermark_height, 50);
// if you set pct to 100 in imagecopymerge() you should use imagecopy() insted
//imagecopy($image, $watermark, 0, 0, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image, 'images3/wmark1.jpg',88);// just for this test I do NOT overwrite the image
imagedestroy($image);
imagedestroy($watermark);
}



function changeImage($location){
/*
	This is the function that will reverse the source and destination image.
	Please notice that I change the "source" as the
	$watermark = imagecreatefromjpeg($location);
	I do this because I get the $watermark_height from the source image not
	the destination image.
*/
$watermark = imagecreatefromjpeg($location);// this is the opposite from the above function
$watermark_height = imagesy($watermark);
$watermark_width = imagesx($watermark); 

$image = imagecreatefromjpeg('images3/inspir1.jpg');// this was $location in the above function
// $image is destination
imagecopymerge($image,$watermark, 0, 0, 0, 0, $watermark_width, $watermark_height, 50);
//imagecopy($image, $watermark, 0, 0, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image, 'images3/change1.jpg',88);
imagedestroy($image);
imagedestroy($watermark);
}

watermarkImage('images3/river1.jpg');// change image path to a JPG in your folder
changeImage('images3/river1.jpg');
?>

<h2>GD Transparent PNG Image test</h2>
<p><img src="images3/wmark1.jpg"></p><img src="images3/change1.jpg">
</body></html>

Open in new window

0
 

Author Comment

by:rgb192
ID: 33433397
i need to have an image on a watermark

because my watermark is on the bottom and the left    

image   w
             a
             t
              e
              r
              m
              a
              r
              k
watermark


needs transparancy
an because I need high quality
I can not use gif      

imagecopymerge  does not work with png
 
0
 

Author Comment

by:rgb192
ID: 33433408
correction


my watermark is to the bottom and the <i>RIGHT</i>




image   w
             a
             t
              e
              r
              m
              a
              r
              k
watermark
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 167 total points
ID: 33434470
OK, At this point I can not deal with where your watermark may be located. But no matter where you may need or want your watermark to be located, it is my opinion from doing images with transparency in PHP GD that you will copy the water mark ON TO the main image, I do NOT see how it can work if you over copy the main image ON TO the water mark. It is possible to do that with some special alpha channel for your water mark, but from your code you are getting your water mark from a 24 bit color JPG image, which can not use 32 bit alpha channel rendering.
You say "imagecopymerge  does not work with png", in my use of GD functions I have seen that it works for me. Once you load any image into the GD image cell, it is converted to a "Bitmap" type of memory map, and no longer has any relation to the the hard drive image or it's format. I have use the the imagecopymerge( ) with png images, and it does ignore all alpha channel byte settings of the PNG, but still will copy the image.

If you are mainly interested in the positioning of the water mark image, you need to change these factors -
$y_pos = imagesy($image) - $watermark_height;
$x_pos = imagesx($image) - $watermark_width;

but it looks like this should place the water mark at the bottom-right of the larger main image.

could you get any of my code to work? ?



0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 33442115
http://www.laprbass.com/RAY_add_watermark_watermark_image.png
http://www.laprbass.com/RAY_add_watermark_source_image.png
http://www.laprbass.com/RAY_add_watermark.php
<?php // RAY_add_watermark.php
error_reporting(E_ALL);

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

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

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

// SHOW THE IMAGE
header('Content-type: image/png');
ImagePNG($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: 33560849
thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
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 …

762 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

20 Experts available now in Live!

Get 1:1 Help Now