put watermark on an image

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

LVL 1
rgb192Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jrm213jrm213Commented:
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
rgb192Author Commented:
puts a
black square on
watermark
0
rgb192Author Commented:
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
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

jrm213jrm213Commented:
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
rgb192Author Commented:

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
jrm213jrm213Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
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
Slick812Commented:
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
rgb192Author Commented:
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
rgb192Author Commented:
correction


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




image   w
             a
             t
              e
              r
              m
              a
              r
              k
watermark
0
Slick812Commented:
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
Ray PaseurCommented:
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
rgb192Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.