Jump to content

PHP watermark


Spunky

Recommended Posts

Ok so thanks to the help of a couple other members I was able to put together this code that adds a watermark to an image before it displays it. However, I cannot figure out how to make it do it for more than one image. Here is my code:

	$directory = "clientPhotos/" . $_SESSION['Client'] . "/";	foreach (glob($directory . "*.jpg") as $filename) {			echo '<img src="show_image.php?f=' . rawurlencode(basename($filename)) . '" />';		echo rawurlencode(basename($filename));

This code is within show_image.php:

<?phpsession_start(); // open the file in a binary mode $name = "images/dog.jpg"; //$fp = fopen($name, 'rb'); // Load the stamp and the photo to apply the watermark to $watermark = imagecreatefromstring(file_get_contents($name)); $image = imagecreatefromjpeg("clientPhotos/" . $_SESSION['Client'] . "/Blue hills.jpg");  // Set the margins for the stamp and get the height/width of the stamp image $marge_right = 10; $marge_bottom = 10; $sx = imagesx($watermark); $sy = imagesy($watermark); // Copy the stamp image onto our photo using the margin offsets and the photo  // width to calculate positioning of the stamp.  imagecopy($image, $watermark, imagesx($image) - $sx - $marge_right, imagesy($image) - $sy - $marge_bottom, 0, 0, imagesx($watermark), imagesy($watermark)); // Output and free memory header('Content-type: image/jpeg'); imagejpeg($image); imagedestroy($image); exit;?>

Right now, dog.php is the watermark for Blue hills.jpg both of which that are within the separate php file. the $directory from the top code is irrelevant as long as the image exists, it will just take the images from within show_image and replace it. Which is fine I guess, although weird. But since show_image is outputting the image info on the bottom, I can't figure out how to do this for more than one image. I can't have separate files for each image because the original purpose of using "for each" was so it didn't matter how many images were in the folder nor the names of them, they would just display, and I need them to do so with the watermark.

Link to comment
Share on other sites

The show_image script isn't doing anything with the filename passed in the URL. You have the filename just hardcoded in there. You should be getting the filename from the URL parameter.
It has to be doing something. When I display the image, dog.jpg is indeed overlapping Blue hills.jpg. Or if I change that to a different image, dog.jpg is overlapping that different image even though Blue hills.jpg is in the code outside of show_image. At that point, Blue hills.jpg pretty much becomes like, a placeholder. Even though it says Blue hills.jpg, the main image being used is established within show_image.
Link to comment
Share on other sites

The code boxes are messed up since the update, I'm not sure what else is. The script is using hardcoded filenames for the watermark image and the base image. It would be much more flexible if those were variables instead of being hardcoded. The img tag is passing a filename to the script, so the script should be using that filename instead of having it hardcoded. The URL for the script can also include a filename for the watermark image if you want to add that. You can even set a default watermark in case the one that was passed wasn't found.

Link to comment
Share on other sites

The script is using hardcoded filenames for the watermark image and the base image. It would be much more flexible if those were variables instead of being hardcoded. The img tag is passing a filename to the script, so the script should be using that filename instead of having it hardcoded. The URL for the script can also include a filename for the watermark image if you want to add that. You can even set a default watermark in case the one that was passed wasn't found.
Ok I get what you are saying I think. Yea, right now the images are hardcoded for testing purposes to make sure the watermark works. Now I need to know how to make it usable for multiple images. You say variables. Ok, but can show_image output the data of multiple images? My code outside the show_image.php code is doing 'for each' so it looks through and finds all the images that end in .jpg. For each of those it is running the show_image script, I guess Im confused then how I would have show_image output the same images that the for each line is finding at the same time. You said something about the URL for the script. Do I need to pass the image name through the url for show_image to use?
Link to comment
Share on other sites

Yes. the script that is adding the watermark should expect an image URL as a member of the GET array, since that is how you are calling the script

show_image.php?f=somefilename

show_image.php

$name = $_GET['f'];

Link to comment
Share on other sites

Ok, but can show_image output the data of multiple images?
Any image display script can only display a single image at a time, but the same script doesn't always need to display the same image.
I guess Im confused then how I would have show_image output the same images that the for each line is finding at the same time.
That's why you pass the image filename to the script:
echo '<img src="show_image.php?f=' . rawurlencode(basename($filename)) . '" />';

The script checks $_GET['f'] to figure out which image to work with instead of having the filename hard-coded. It's the exact same script you have now, but instead of the filename being a string that you wrote in the code it's a value that you get from the URL. The only thing you need to do extra, other than getting the filename from the URL, is to validate that the file exists before trying to use it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...