working alone but not working with gd
what should we do ?
Thats how my orignal code looks like
/////////// ////Snapshot creation //////////////////////////////
if (!file_exists(“$site_dir/” . “snap_$userfile_name”))
{
$thumb->size_auto(416);
$thumb->jpeg_quality(60);
$thumb->save(“$site_dir/snap_$userfile_name”, $gd_version);
$image_src = $site_dir . “snap_$userfile_name”;
header(‘content-type: image/jpeg’);
$watermark = imagecreatefrompng(‘watermark.png’);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($image_src);
$size = getimagesize($image_src);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
$thumb=new thumbnail(“$site_dir/$userfile_name”);
}
thumbnail is successfully created but with no watermark and that what i get after uploading
ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛCaaaa $.’ ",#(7),01444’9=82<.342ÿÛC 2!!22222222222222222222222222222222222222222222222222ÿÀ8 "ÿÄa
try fixing your apache’s mime-type forcing.
it seems apache thinks “it’s a xxx.php-file so force it to text/html” or something like that, because
header(‘Content-Type: image/jpeg’);
should say “show as jpeg”.
But maybe it’s wrong to type “content-type” (small case!!). So pay attention to that.
YOu can use 24-bit transparencu with this code if you change one line of the code
imagecopymarged($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
to
imagecopyresampled($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height);
This works fine. If you wanna know more about this function
imagecopyresampled()
good
thanks for the tutorial!
thanks man
I’ve been looking for this kind of tutorial for a while, yet this one like
any one else gives me the foolowing error:
Wend outputting to the browser I should use –>
header(‘Content-Type: image/jpeg’);
… yet when I try to save the image from the browser (Save image as…) the
image is saved as ‘script_name.php’.
Is there any way to go around this?
I’m storing all the original images in a folder and the watermarked ones in
another.
I’m watermarking them on the fly and calling the <img src…> that has
been watermarked. I’ve chossen this way because then I’m able to chanse the
logo whenever I wan’t.
I can’t go on like this 'cause of the server space I’ve bought is to small
to have duplicates.
Thank’s.
Sorry about the English.
I need some assistance in implementing this. Anyone have a sample zip?
I took the code from the article and saved it as watermark.php.
Next, I opened photoshop and created a watermark.png file that just had my name. I placed it in the same directory as watermark.php.
I placed a sample jpg img in that directory as well, and called it car.jpg.
Then I created an html page (test.html, placed in the same directory) to call the image. I did it like this:
<img src=“watermark.php?image=car.jpg” alt=“This is a test” />
When I view test.html, all I see is my alt tags. Obviously, I’m attempting to implement this completely wrong, but I didn’t see where to do that correctly. Can someone address that for me? I would definitely appreciate it. - Tony
Howdy,
Fantastic script, thanks!
tonywhite, by default you call it with watermark.php?src=car.jpg - see, ‘SRC’, not ‘IMAGE’. Looks like a few other people had that problem also.
I made an upgrade to the script to better suit my needs:
I don’t want to have to call the image like <img src=“watermark.php?src=…”> - I would prefer to do a regular call.
So, my server setup is this:
Images get uploaded into a folder called ‘upload_img’. Thumbnails are auto-generated on upload- same filename, just with a ‘th_’ prefix. I don’t want to watermark the thumbnails. Here’s what I did:
Step 1: .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} upload_img(.*) [NC]
RewriteRule ^(.*)$ http://www.domain.com/watermark.php?src=$1 [NC]
This tells the server to direct any request for a file in the ‘upload_img’ folder through the watermark.php handler.
Step 2: ignore th_ files
Modify the php file to have an if/else statement using strpos for the filename. eg,
if (strpos contains 'th_') {
show raw image
} else {
run the script
}
Tada!
Now all my existing image calls still work without needing modification. I don’t need to include ‘watermark.php?src=…’ in my <img> tags (so users can’t view source to see what i’m doing). Public users simply CANNOT access the raw image without the watermark anymore (due to the .htaccess). And it leaves my thumbnails alone.
Hope that helps someone.
if you want the real name of your image use this, where $filename is the name of your image (get this from the GET string, but split this string when you use a full path)
header(“Content-type: image/jpeg;”);
header("Content-Disposition: filename= " . $filenaam);
What ever I try, it does not work. My code is okay, maybe a problem with the watermark.png… zip-file anyone ?
I only get to see an cann’t display image icon in my browser
http://www.ts-intermedia.nl/plaatje/index.html
My code of index.html
<img src=“watermark.php?scr=1.jpg” />
photo
http://www.ts-intermedia.nl/plaatje/1.jpg
watermark.png
http://www.ts-intermedia.nl/plaatje/watermark.png
I have GD 2.0.22
Anyone ?
(please post working Zip File, thanxs )
I find if you are going to create a png image with php’s gd libary extension you should tell the browser that your creating a png image. Otherwise you might end up with something bad outputed. So, instead of doing image/jpeg as the header information do a image/png or an image/x-png. Also, I know that the gd libary can’t change the file type of an image. If it is read in as a .png it will only be able to be outputed as a png, same with the other formats. I noticed the article said that .gif support did not work in 2.0+ gd. Yes, it does work. It just doesn’t support animated .gif files. It has to be static .gif files.
Now, for a more robust error proof way of reading in the images then you should look towards checking the mime type of he image then do a switch or if then statements to set the proper imagecreatefrom with the mime type of the image.
I made comments to the listed code example. I know the comments and code isn’t 100%, but correct me where you can and help out with this.
name: watermark.php
call by: watermark.php?photofile=name_of_png_image.png
<?php
/*
orginal image need to be bigger then the watermark image
orginal image and watermark image needs to be a .png file type
Either a .htaccesss file or aphache has to be setup to output this .php file as a image file.
*/
//read in the varibles from command line
$photofile = $_GET['photofile'];
/* read the watermark image into memory. This would be a good place to detect the mime of the photofile and setup a switch to create the rigth type of image from it. */
$watermark = imagecreatefrompng('watermark.png');
/* get the width and height of the watermark image. */
list($watermark_width,$watermark_height) = getimagesize('watermark.png');
/* read in the photofile into memory. Another good place to setup a mime type switch to create the right image from the detected mime type. */
$image = imagecreatefrompng($photofile);
/* this does the x,y position of the watermark need to cover the orginal image */
list($img_w,$img_h) = getimagesize($photofile);
$dest_x = $img_w - $watermark_width - 5;
$dest_y = $img_h - $watermark_height - 5;
/* save the transparentcy of the orginal image. */
imagealphablending($image, false);
imagesavealpha($image,true);
/* copy the watermarked image onto the orginal image. */
imagecopyresampled($image,$watermark,0,0,0,0,$dest_x,$dest_y,$watermark_width,$watermark_height);
/* output the image. Great place to setup a mime switch to output the correct content type message */
header("Content-Type: image/png");
header("Content-Disposition: filename= " . $photofile);
imagepng($image);
//free up the memory used
imagedestroy($image);
imagedestroy($watermark);
?>
allstar …Correct me if I am wrong… but I can not use a PNG watermark with JPG files ?
When I use your code and I use a PNG watermark and a PNG photo… then it works…
But I do want to use JPG files.
Well still like to get working files (incl watermark.png and jpg) in zipped file
Guys … don’t ask me why… but I’ve got it working !!
Thanxs for all the help !
T
That is why I said to use a switch to detect what the mime types of the files are. .jpg files are not made to be transparent like .png/.gif files are. So, your watermark is going to have the black stuff to it. If your orginal file was a .jpg and your watermark is a .png/.gif then it would work, because the .png can have transparent parts to it. Though the final image will not, because it will be a .jpg file. The example is just the basic structure to do the task for .png files.
For PNG24 alpha transparency, use imagecopy() instead (take out the last pct argument though)
Very nice feature. However I found the ability to display the image using the imagejpeg() directly to the web very limiting due to the restriction that the header must be modified for image/jpeg only. Any attempt to display something other then the image before resulted in just the text string being displayed. I have semi comlex annotation software where I need to temporarily overlay arrow and text objects on a biological image. The imagecopymerge() function worked quite nicely and efficienty however I had to write image out to a file using the imagejpeg() function before using the <img src html function. If there are any ways around this I would like to know.