|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
SitePoint Articles
Join Date: Apr 2001
Posts: 0
|
Article Discussion
This is an article discussion thread for discussing the SitePoint article, "Watermark Images on the Fly in PHP"
|
|
|
|
|
|
#2 |
|
SitePoint Zealot
![]() ![]() Join Date: May 2004
Location: Ontario, Canada
Posts: 176
|
Two words: Thank you
![]() |
|
|
|
|
|
#3 |
|
Web Design Ireland
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2004
Location: Dublin Ireland
Posts: 1,206
|
That is one of the best articles I have read in watermarking. Excellent resource. Thank you very much.
|
|
|
|
|
|
#4 |
|
SitePoint Zealot
![]() ![]() Join Date: Sep 2004
Location: Milwaukee, WI, USA
Posts: 111
|
Great Article. I love the ability to watermark in PHP. I have been using it for a while now. Works great! Here is an example of the output is creates:
The logo and Copyright info is the Watermark. |
|
|
|
|
|
#5 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Nov 2003
Location: England
Posts: 293
|
DocDave,
Any chance you could post the code you actually use, please? I still haven't managed to sort out my issues, mainly because I have very little time to do it. TIA CJ |
|
|
|
|
|
#6 |
|
SitePoint Zealot
![]() ![]() Join Date: Sep 2004
Location: Milwaukee, WI, USA
Posts: 111
|
Ok here you go, the variable $photo_file is the file that was uploaded earlier in the routine. There is other stuff in the routine to size the image and what not. Hope this helps.
PHP Code:
![]() |
|
|
|
|
|
#7 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Nov 2003
Location: England
Posts: 293
|
Perfection! Thanks Dave
|
|
|
|
|
|
#8 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jan 2004
Location: The Big Apple
Posts: 621
|
Would anyone care for a function to use during image upload?
|
|
|
|
|
|
#9 |
|
SitePoint Enthusiast
![]() Join Date: Apr 2005
Posts: 88
|
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"); } |
|
|
|
|
|
#10 |
|
SitePoint Enthusiast
![]() Join Date: Apr 2005
Posts: 88
|
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 ÿÛC $.' ",#(7),01444'9=82<.342ÿÛC 2!!22222222222222222222222222222222222222222222222222ÿÀ8 "ÿÄ |
|
|
|
|
|
#11 |
|
SitePoint Member
Join Date: Jun 2005
Posts: 3
|
YOu can use 24-bit transparencu with this code if you change one line of the code
PHP Code:
PHP Code:
imagecopyresampled() |
|
|
|
|
|
#12 |
|
SitePoint Enthusiast
![]() Join Date: Jul 2004
Location: Brisbane, Australia
Posts: 36
|
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 Code:
RewriteEngine on
RewriteCond %{REQUEST_URI} upload_img(.*) [NC]
RewriteRule ^(.*)$ http://www.domain.com/watermark.php?src=$1 [NC]
Step 2: ignore th_ files Modify the php file to have an if/else statement using strpos for the filename. eg, Code:
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. ![]() |
|
|
|
|
|
#13 | |
|
SitePoint Member
Join Date: Jan 2006
Posts: 2
|
Quote:
I am trying to use this cool code in my .htaccess file, but my server keeps giving me back Http Error 500. Would you kindly explain how I could get this to work? I have a bunch of listings on eBay and would like to watermark all the images. Thanks! |
|
|
|
|
|
|
#14 |
|
SitePoint Member
Join Date: Jan 2006
Posts: 2
|
nevermind, got it sorted...the problem was that I had put all the files in my images directory. Moved them to root and we are good to go.
Thanks all! |
|
|
|
|
|
#15 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Sep 2005
Location: in my box.
Posts: 386
|
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 Code:
|
|
|
|
|
|
#16 |
|
SitePoint Member
Join Date: Nov 2005
Posts: 3
|
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 |
|
|
|
|
|
#17 |
|
SitePoint Member
Join Date: Nov 2005
Posts: 3
|
Guys .... don't ask me why... but I've got it working !!
Thanxs for all the help ! T |
|
|
|
|
|
#18 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Sep 2005
Location: in my box.
Posts: 386
|
Quote:
|
|
|
|
|
|
|
#19 |
|
SitePoint Enthusiast
![]() Join Date: Sep 2005
Posts: 35
|
Is there a way to save the watermarked $image to a file? I tried using fopen and fwrite, but they didn't work.
|
|
|
|
|
|
#20 |
|
SitePoint Member
Join Date: Aug 2006
Posts: 3
|
Ok ! I have used it. But why I use "Watermark" is PNG file but it is opaque.
Plz help me ! Thanks. |
|
|
|
|
|
#21 |
|
SitePoint Member
Join Date: Mar 2007
Posts: 1
|
This does not work in the new firefox 2 for some reason, what the hell? Isn't it all server-sided???
|
|
|
|
|
|
#22 |
|
SitePoint Enthusiast
![]() Join Date: Sep 2005
Posts: 48
|
I know this is an old thread, but for all of you who want to apply watermarks on the fly, you might want to check the open-source project Asido: there are a lot of useful features when creating applying watermarks, like tile watermarks or watermark scaling. There's no problem at all using transparent images both as watermarks or as images that are about to be watermarked. Here's a brief example copied from Asido website:
PHP Code:
|
|
|
|
|
|
#23 |
|
SitePoint Zealot
![]() ![]() Join Date: Jul 2005
Posts: 143
|
I too failed to get the origional script to work... But after reading all your posts today about asido, I downloaded asido this afternoon from soundforge and hey! It works!!
The only thing it did that confused me was it made my watermark way smaller, but digging through the examples I found that by default it reduces the watermark to 25%. This reduction can be eliminated by adding:-- Code:
, ASIDO_WATERMARK_SCALABLE_DISABLED Code:
, ASIDO_WATERMARK_SCALABLE_ENABLED, 0.66 |
|
|
|
|
|
#24 | |
|
SitePoint Enthusiast
![]() Join Date: Sep 2005
Posts: 48
|
Quote:
Recently I've been thinking about how to improve the watermark scaling. Right now the scale factor is applied to the watermark image without any regard to the image being watermarked. Let me show you an example: you have an image that is 100x100, and you have a watermark that is 800x800; since the watermark is bigger, the watermark scaling will try to reduce it by the defautl scaling factor, which 0.25 (1/4), so the watermark will become 200x200 and it will be still bigger than the image that's about to be watermarked. The solution is to tie the scaling factor not to the dimensions of the watermark, but to the dimensions of thre image, so when the scaling factor is 1/4, this means that the watermark will become 1/4 from the size of the watermarked image. Using 100% (1.0) as scaling factor will cover the whole image w/ the watermark. Anyway, I don't want to drop the way the scaling is applied right now, so probably there will be a new "mode" for the scaling, and the available options will be: ASIDO_WATERMARK_SCALABLE_DISABLED, ASIDO_WATERMARK_SCALABLE_ENABLED_TARGET (current implementation) and ASIDO_WATERMARK_SCALABLE_ENABLED_SOURCE (the feature I just explained) |
|
|
|
|
|
|
#25 |
|
SitePoint Member
Join Date: Jul 2008
Posts: 5
|
I usually use programm to resize my pictures,then upload the pictures to my host!Batch Watermark Creator is a good choice,That's the one I know how to use.visit the on-line places and search it;
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 04:52.












Hybrid Mode
