Jump to content

Recommended Posts

$uploadDir = '../uploads/';

if(isset($_POST['upload']))
{
foreach ($_FILES as $file)
{
$fileName = $file['name'];
$tmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileType = $file['type'];

if($fileName==""){

$filePath = '../img/none.jpg';
}
else{

$filePath = $uploadDir . $fileName;
}

////generate random name
//// get the file extension first
$ext = substr(strrchr($fileName, "."), 1); 
//
//// make the random file name
$randName = md5(rand() * time());
//
//// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;
// Replace spaces with a '_'
$filePath = str_replace(" ", "_", $filePath);
$result = move_uploaded_file($tmpName, $filePath);

//if (!$result) {
//echo "Error uploading file";
//exit;
//}


if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$fileinsert[]=$filePath;
}
/*
 * PHP GD
 * adding watermark to an image with GD library
 */

// Load the watermark and the photo to apply the watermark to
$stamp = imagecreatefrompng('../img/watermark_fotos.png');
$im = imagecreatefromjpeg('$filePath');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

Hi, I use this code to upload an image and save the path to my DB. I have added some code to watermark the image but it doesn't seem to be doing it... please help.

Thanks :-)

 

Link to comment
https://forums.phpfreaks.com/topic/290074-please-help-watermarking-image/
Share on other sites

you need to determine what your code is actually doing to find where the problem is at.

 

to start with, do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects? the following line - $im = imagecreatefromjpeg('$filePath'); should be producing php error(s), because php variables are not replaced with their content when they are between single-quotes. you may also need to turn OFF any output_buffering setting that might be ON in your php.ini since that can hide php error messages.

 

next, your upload code is looping over multiple uploaded files. do you want to water-mark all of them or just the last one (which is what your current code is trying to do) and what do you want to do with the final water-marked image(s)? the current code is outputting it (the last image) back to the browser immediately after processing the uploaded file. do you want to save the water-marked image as a file so that you don't need to run the gd functions (which take a lot of memory and processing time) every time you display the image? also, if your intent is to upload multiple images at one time (what the first part of your code supports), you won't be able to output the images back to the browser all at once as you can only output one image at a time.

Hi mac_gyver, thanks for your answer.

In this case what I want to do is to upload one image to my "uploads" folder, watermark the image and save the path to my database.

The uploading and path saving parts work fine, but I have trouble watermarking the image.

I don't need the image to be outputed to the browser after being watermarked, so I have removed tha part of the code.

..... :confused:

Try this function that I have in my code base.

//Creates a water marked image
// $wm is the watermark image you want to use
// $img is the image to watermark
// $xy should be an array of x and y coordinates - $xy['x'] = 150 $xy['y'] = 40
// placement defaults to center if $xy is not supplied
// $dest woudl be a place to save the file if you are not displaying the new image directly to the browser.
function watermarkImage($wm, $img, $xy = NULL, $dest = NULL)
{
	$watermark = imagecreatefrompng($wm);
	$wm_src = getimagesize($wm);
	$source = getimagesize($img);
	$mime = $source['mime'];
	
	if($mime === 'image/jpg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/jpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/pjpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/png')
	{$src = imagecreatefrompng($img);}
	elseif($mime === 'image/gif')
	{$src = imagecreatefromgif($img);}

	$x = ($xy !== NULL) ? $xy['x'] : floor(($source[0] / 2) - ($wm_src[0] / 2));
	$y = ($xy !== NULL) ? $xy['y'] : floor(($source[1] / 2) - ($wm_src[1] / 2));
	
	imagecopy($src, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));
	imagealphablending($src, false);
	imagesavealpha($src, true);

	if($mime === 'image/jpg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/jpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/pjpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/png')
	{
		if($dest !== NULL)
		{ imagepng($src, $dest); }
		else
		{
			header('Content-Type: image/png');
			imagepng($src);
		}
	}
	elseif($mime === 'image/gif')
	{
		if($dest !== NULL)
		{ imagegif($src, $dest); }
		else
		{
			header('Content-Type: image/gif');
			imagegif($src);
		}
	}

	imagedestroy($src);
}

Seriously!, you don't know how to use a standard function?  I reworked your code to handle my function.  I can't say it's perfect cause I'm not going to make a html form to fully test it.

<?php
//Creates a water marked image
// $wm is the watermark image you want to use
// $img is the image to watermark
// $xy should be an array of x and y coordinates - $xy['x'] = 150 $xy['y'] = 40
// placement defaults to center if $xy is not supplied
// $dest woudl be a place to save the file if you are not displaying the new image directly to the browser.
function watermarkImage($wm, $img, $xy = NULL, $dest = NULL)
{
	$watermark = imagecreatefrompng($wm);
	$wm_src = getimagesize($wm);
	$source = getimagesize($img);
	$mime = $source['mime'];
	
	if($mime === 'image/jpg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/jpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/pjpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/png')
	{$src = imagecreatefrompng($img);}
	elseif($mime === 'image/gif')
	{$src = imagecreatefromgif($img);}

	$x = ($xy !== NULL) ? $xy['x'] : floor(($source[0] / 2) - ($wm_src[0] / 2));
	$y = ($xy !== NULL) ? $xy['y'] : floor(($source[1] / 2) - ($wm_src[1] / 2));
	
	imagecopy($src, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));
	imagealphablending($src, false);
	imagesavealpha($src, true);

	if($mime === 'image/jpg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/jpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/pjpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/png')
	{
		if($dest !== NULL)
		{ imagepng($src, $dest); }
		else
		{
			header('Content-Type: image/png');
			imagepng($src);
		}
	}
	elseif($mime === 'image/gif')
	{
		if($dest !== NULL)
		{ imagegif($src, $dest); }
		else
		{
			header('Content-Type: image/gif');
			imagegif($src);
		}
	}

	imagedestroy($src);
}


$uploadDir = '../uploads/';

if(isset($_POST['upload']))
{
	foreach ($_FILES as $file)
	{
	$fileName = $file['name'];
	$tmpName = $file['tmp_name'];
	$fileSize = $file['size'];
	$fileType = $file['type'];
	
	if($fileName==""){
	
	$filePath = '../img/none.jpg';
	}
	else{
	
	$filePath = $uploadDir . $fileName;
	}
	
	////generate random name
	//// get the file extension first
	$ext = substr(strrchr($fileName, "."), 1); 
	//
	//// make the random file name
	$randName = md5(rand() * time());
	//
	//// and now we have the unique file name for the upload file
	$filePath = $uploadDir . $randName . '.' . $ext;
	// Replace spaces with a '_'
	$filePath = str_replace(" ", "_", $filePath);
	$result = move_uploaded_file($tmpName, $filePath);
	
	//if (!$result) {
	//echo "Error uploading file";
	//exit;
	//}
	
	
	if(!get_magic_quotes_gpc())
	{
	$fileName = addslashes($fileName);
	$filePath = addslashes($filePath);
	}
	$fileinsert[]=$filePath;
	}
	/*
	 * PHP GD
	 * adding watermark to an image with GD library
	 */
	
	// Load the watermark and the photo to apply the watermark to
	$stamp = '../img/watermark_fotos.png';
	
	// Set the margins for the stamp and get the height/width of the stamp image
	$margin['x'] = 10;
	$margin['y'] = 10;
	
	$destination = 'path_where_you_want_the_image_stored';
	
	watermarkImage($filePath, $stamp, $margin, $destination);

}
?>

You have some other badly written code in there but I didn't focus on any of that.  This in particular is pretty bad.

if(!get_magic_quotes_gpc())
	{
	$fileName = addslashes($fileName);
	$filePath = addslashes($filePath);
	}

Magic quotes should be disabled by default on any server at this point, it's a very bad function that php used in the past and has since been removed.  Towards the end of the code you will need to edit the $destination var value to an actual path and file name you want the watermarked images stored to.

thanks!!... one question, what is $destination supposed to be? my script is uploading the image to te UPLOADS folder and saving the path to the database so I don't understand what this other path should be, What I want to do is to watermark the image ad then save the path to the database. Sorry I'm not sure I'm explaining this properly...

The destination can be whatever you want it to be for the final image name and where you want it stored.  If you simply want the uploaded file replaced with the new watermarked image then do

$destination = $filePath;

If it doesn't store it then you'll need to verify the path to the folder you're trying to store it in, but since it seems to work for the upload, in theory it should work for the watermark too.

$query = "INSERT INTO t_images (imagen_path) ".
"VALUES ('$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 
//Creates a water marked image
// $wm is the watermark image you want to use
// $img is the image to watermark
// $xy should be an array of x and y coordinates - $xy['x'] = 150 $xy['y'] = 40
// placement defaults to center if $xy is not supplied
// $dest woudl be a place to save the file if you are not displaying the new image directly to the browser.
function watermarkImage($wm, $img, $xy = NULL, $dest = NULL)
{
	$watermark = imagecreatefrompng($wm);
	$wm_src = getimagesize($wm);
	$source = getimagesize($img);
	$mime = $source['mime'];
	
	if($mime === 'image/jpg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/jpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/pjpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/png')
	{$src = imagecreatefrompng($img);}
	elseif($mime === 'image/gif')
	{$src = imagecreatefromgif($img);}

	$x = ($xy !== NULL) ? $xy['x'] : floor(($source[0] / 2) - ($wm_src[0] / 2));
	$y = ($xy !== NULL) ? $xy['y'] : floor(($source[1] / 2) - ($wm_src[1] / 2));
	
	imagecopy($src, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));
	imagealphablending($src, false);
	imagesavealpha($src, true);

	if($mime === 'image/jpg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/jpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/pjpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/png')
	{
		if($dest !== NULL)
		{ imagepng($src, $dest); }
		else
		{
			header('Content-Type: image/png');
			imagepng($src);
		}
	}
	elseif($mime === 'image/gif')
	{
		if($dest !== NULL)
		{ imagegif($src, $dest); }
		else
		{
			header('Content-Type: image/gif');
			imagegif($src);
		}
	}

	imagedestroy($src);
}


$uploadDir = '../uploads/';

if(isset($_POST['upload']))
{
	foreach ($_FILES as $file)
	{
	$fileName = $file['name'];
	$tmpName = $file['tmp_name'];
	$fileSize = $file['size'];
	$fileType = $file['type'];
	
	if($fileName==""){
	
	$filePath = '../img/none.jpg';
	}
	else{
	
	$filePath = $uploadDir . $fileName;
	}
	
	////generate random name
	//// get the file extension first
	$ext = substr(strrchr($fileName, "."), 1); 
	//
	//// make the random file name
	$randName = md5(rand() * time());
	//
	//// and now we have the unique file name for the upload file
	$filePath = $uploadDir . $randName . '.' . $ext;
	// Replace spaces with a '_'
	$filePath = str_replace(" ", "_", $filePath);
	$result = move_uploaded_file($tmpName, $filePath);
	
	//if (!$result) {
	//echo "Error uploading file";
	//exit;
	//}
	
	
	if(!get_magic_quotes_gpc())
	{
	$fileName = addslashes($fileName);
	$filePath = addslashes($filePath);
	}
	$fileinsert[]=$filePath;
	}
	/*
	 * PHP GD
	 * adding watermark to an image with GD library
	 */
	
	// Load the watermark and the photo to apply the watermark to
	$stamp = '../img/watermark_fotos.png';
	
	// Set the margins for the stamp and get the height/width of the stamp image
	$margin['x'] = 10;
	$margin['y'] = 10;
	
	$destination = $filePath;
	
	watermarkImage($filePath, $stamp, $margin, $destination);

Hi, this is the code you modified for me

 

and this is my db update query

//Creates a water marked image
// $wm is the watermark image you want to use
// $img is the image to watermark
// $xy should be an array of x and y coordinates - $xy['x'] = 150 $xy['y'] = 40
// placement defaults to center if $xy is not supplied
// $dest woudl be a place to save the file if you are not displaying the new image directly to the browser.
function watermarkImage($wm, $img, $xy = NULL, $dest = NULL )
{
	$watermark = imagecreatefrompng($wm);
	$wm_src = getimagesize($wm);
	$source = getimagesize($img);
	$mime = $source['mime'];
	
	if($mime === 'image/jpg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/jpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/pjpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/png')
	{$src = imagecreatefrompng($img);}
	elseif($mime === 'image/gif')
	{$src = imagecreatefromgif($img);}

	$x = ($xy !== NULL) ? $xy['x'] : floor(($source[0] / 2) - ($wm_src[0] / 2));
	$y = ($xy !== NULL) ? $xy['y'] : floor(($source[1] / 2) - ($wm_src[1] / 2));
	
	imagecopy($src, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));
	imagealphablending($src, false);
	imagesavealpha($src, true);

	if($mime === 'image/jpg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/jpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/pjpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/png')
	{
		if($dest !== NULL)
		{ imagepng($src, $dest); }
		else
		{
			header('Content-Type: image/png');
			imagepng($src);
		}
	}
	elseif($mime === 'image/gif')
	{
		if($dest !== NULL)
		{ imagegif($src, $dest); }
		else
		{
			header('Content-Type: image/gif');
			imagegif($src);
		}
	}

	imagedestroy($src);
}


$uploadDir = '../uploads/';

if(isset($_POST['upload']))
{
	foreach ($_FILES as $file)
	{
	$fileName = $file['name'];
	$tmpName = $file['tmp_name'];
	$fileSize = $file['size'];
	$fileType = $file['type'];
	
	if($fileName==""){
	
	$filePath = '../img/none.jpg';
	}
	else{
	
	$filePath = $uploadDir . $fileName;
	}
	
	////generate random name
	//// get the file extension first
	$ext = substr(strrchr($fileName, "."), 1); 
	//
	//// make the random file name
	$randName = md5(rand() * time());
	//
	//// and now we have the unique file name for the upload file
	$filePath = $uploadDir . $randName . '.' . $ext;
	// Replace spaces with a '_'
	$filePath = str_replace(" ", "_", $filePath);
	$result = move_uploaded_file($tmpName, $filePath);
	
	//if (!$result) {
	//echo "Error uploading file";
	//exit;
	//}
	
	
	if(!get_magic_quotes_gpc())
	{
	$fileName = addslashes($fileName);
	$filePath = addslashes($filePath);
	}
	$fileinsert[]=$filePath;
	}
	
	$id_trabajo = $colname_trabajos_selected_RS;
	$query = "INSERT INTO t_imagenes_trabajos (imagen_path,id_trabajo) ".
"VALUES ('$filePath','$id_trabajo')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

//echo "<br>Files uploaded<br>";

header("Location: PC_images_display.php?id_trabajo=$colname_trabajos_selected_RS&imageninserted=true"); 

	/*
	 * PHP GD
	 * adding watermark to an image with GD library
	 */
	
	// Load the watermark and the photo to apply the watermark to
	$stamp = '../img/watermark_fotos.png';
	
	// Set the margins for the stamp and get the height/width of the stamp image
	$margin['x'] = 10;
	$margin['y'] = 10;
	
	$destination = $filePath;
	
	watermarkImage($filePath, $stamp, $margin, $destination);


}

Hi, I tried that but still not placing the watermark over the image, just saving the watermark aolone to my db... :confused:

the first parameter to the watermarkImage() function is the watermark,  the second parameter is the image to watermark.

Ahh, good eye.  I got those backwards when I gave you the code.

This line

watermarkImage($filePath, $stamp, $margin, $destination);

Should be this

watermarkImage($stamp, $filePath, $margin, $destination);

ah ok, thanks.. so this places the watermark on the top left

	// Set the margins for the stamp and get the height/width of the stamp image
	$margin['x'] = 20;
	$margin['y'] = 20;

what if I want it on the bottom right?. I have tried "-" values and it didn't work

You'd have to calculate the size of the image receiving the watermark.

 

Let's say the original image TO be watermarked is 100 wide and 100 tall. x = 100, y = 100 would be the very far bottom right.

let's say the watermark image itself that is being applied to the original image is 10 wide and 10 tall.

 

So you'd take 100 - 10 for the x value, and same for the y value. So you'd put the watermark at x=90, y=90 to be in the bottom right.

 

You are already getting the sizes of the watermark image an the source image here:

$wm_src = getimagesize($wm);
$source = getimagesize($img);

 

So you just need to use them in your calculation

Edited by CroNiX
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.