Help on watermark php script

software development



In my old server i was using a watermark script, just edit .htacess and put a .php file in a folder with .png image (the watermark) and all my pictures automatically were watermarked.

But i don’t know how to make it works in dreamhost, i try everything i know, and nothing works.
The script code is :
Could someone take a look in this script please?



I think the problem might that the default php configuration on Dreamhost is that PHP is running as CGI, and the variables that are manipulated in the beginning of the script grab environment/system variables that are different than the programmer expected when he wrote the script, though I’m not completely sure about that.

I’ll have to look at it a bit further, as this is not my area of expertise (Simon, are you out there?), but I would think some minor changes to the beginning of the script might make it work.



[quote]But i don’t know how to make it works in dreamhost, i try everything i know, and nothing works.
The script code is :
Could someone take a look in this script please?[/quote]
Played with it; Don’t think it’s gonna work if you’re using PHP as CGI. Not even sure how to revert PHP to Apache module, or even if it’s offered.
One thing for certain that I noticed is the path info is missing; i’d replace $dr=preg_replace('/modify\.php.+/', '', $_SERVER['PHP_SELF']); $filename=str_replace($dr, './', $_SERVER['PATH_INFO']);…with:

$dr=preg_replace('/modify\.php.+/', '', $_SERVER['PHP_SELF']); $filename=$_SERVER['DOCUMENT_ROOT'].$dr;…for starters.

Aside from that, there’s a workaround for this that you can employ; simply modify this code to accept a query-string argument for a file name and keep all of the images in a fixed, separate directory that you have “access deny all” set in .htaccess; that way the PHP script should be able to access its files while the httpd won’t allow it to be linked.

Alternatively, if you’re feeling lazy and ya got a few $$ burning a hole in yer pocket and ya want my Rent-a-Coder account name, just lemme know. :wink:


Not even sure how to revert PHP to Apache module

To enable mod_php4 on a per directory basis, put this in .htaccess:

AddHandler application/x-httpd-php .php

If you want useful replies, ask smart questions.



Pretty much the same experience I had, and I played around with it just a bit.

I was hopeful that doing a little creative substitution of variables in those lines woujld make for a simple fix but, as you noted, it’s not that simple.



Thank you all for the quick responses.

kchrist, the line "AddHandler application/x-httpd-php .php"
in .htacess did the trick, now the script is working.

Thank you :slight_smile:

Edited: Now watermark script is working but i get errors uploading pictures in geeklog lol.


I was able to get it working with PHP 5 using:

$filename = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['PATH_INFO'];One other thing I noticed is that it does not send the proper Content-Type header. Here is the fix for that: if ($image_format==1) { // GIF format, send proper content-type and do not process Header("Content-Type: image/gif"); readfile($filename); exit; } elseif ($image_format==2) { // JPEG format, send proper content-type and process Header("Content-Type: image/jpg"); $old_image=imagecreatefromjpeg($filename); } elseif ($image_format==3) { // PNG format, send proper content-type and process Header("Content-Type: image/png"); $old_image=imagecreatefrompng($filename); } else { // Show error message instead Header("Content-Type: text/plain"); echo "Unknown file format: $_SERVER['PATH_INFO']\n"; exit; }And then of course another problem is it does not send a Last-Modified header that would allow a browser to cache the image. Well you get what you paid for.

:cool: [color=#6600CC]Atropos[/color] |


One of the reasons I enjoy hanging out here so much is the ability to learn from the “Dreamninjas”. Thanks Atropos7! You Rock! Since the script is GNU/GPL, are you gonna submit that back to the author?


That is a nice “general purpose” work-around for situations like this one. Thanks for reminding me of that, as I had completely forgotten that capability remained. There are lots of smart people here!


How about I just write a replacement that will work with DreamHost and PHP 5. I wouldn’t feel like submitting a patch for that script because it would be better to re-write it anyway. It might be a good example of watermarking an image but its a bad example of a script in general.

:cool: [color=#6600CC]Atropos[/color] |


Agreed! And I think a re-write would be great. Frankly, I don’t yet have the skills to do it. I would use the re-write compared to the original as a learning tool to increase my understanding, and I’m sure many Dreamhoster’s would appreciate having it work.

I also think that your doing such a re-write would be a generous thing to do. If you have the time, I’d love to see the result!



Yes, true, it would be great a re-write!
It’s a very useful script for people (like me) don’t know PHP stuff.


I think it would be an excellent idea, I beleive information about using graphics with php is a liuttle lacking anyway.


If y’all seem interested in some of these gd lib goodies, I’ll slap together a Wiki stub once all my end-of-month nuisance at $DAYJOB has settled in a coupla days.
Just simple stuff about how to proportionally resize to thumbnails without having to store a separate thumbnail, how to resize images based on the client’s screen resolution, alternate means of protecting images, etc.
If anyone thinks it’d be worth the fuss, that is.

Oh, and ps. to fakestar: That bit about RentaCoder was just a gag (…geek humor - go figure…); I sorta got the impression you would’ve wanted to fix it yourself once I popped the hints. The whole (somewhat quick-n-dirty) fix would’ve only taken about 2-3 lines of replaced code, creating a new directory for your images with some tight (700) permissions, adding two lines to your .htaccess, then you’d have to go change the tags with the new src= in your HTML or whatever. Wouldn’t even make 10 mins work since I’ve already got the first part coded.

But since the best fix is the easiest and ya already got it working, don’t fix what ain’t broke :wink:


Instead of starting from scratch I am mostly cleaning up their code and adding a bit of error handling. Edited the README file as well.

The result (so far) is available at:

My error handling needs a bit of work still, and I will also need to go through the code and comment what the blocks do.

And again works for me on a subdomain configured to use PHP 5.1.2 instead of 4.4.2

:cool: [color=#6600CC]Atropos[/color] |


Wow, that was quick! I’ll run over an have a look-see. I think it is great you are doing this, and I appreciate the chance to learn from you re-written code. Thanks again!



Me too :slight_smile:

I am still learning PHP and this script will definitely help my learning process. Well done Atropos.

I might also have a use for the script in a site I am currently doing.

Once again, well done and thanks :slight_smile:


Save [color=#CC0000]$50[/color] on DreamHost hosting using promo code [color=#CC0000]SAVEMONEY[/color] ( Click for promo code details )


Yeah, it’s working! :slight_smile:


I’m comparing old and new script, i really want to learn php.
I think i’m doing well, i’m trying to modify an image gallery script, just to learn, not able to write my own stuff yet…so, thank you very much for spend your time in this watermark script!


I noticed the script is decreasing the quality of original Jpeg,
if i upload a 60kb jpeg image, after watermarking, the file is like 30kb or less.

How can i control the quality of jpeg output?

Thanks again!


The imagejpg() function can accept up to three agruments, the third of which is quality. It should default to ~75. If you don’t want to save, use NULL for the second argument.

Check out Gordaen’s Knowledge, the blog, and the MR2 page.