help on watermark php script

help on watermark php script

Posted by: fakestar
Posted on: 2006-07-27 23:36:00

hi,

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 :
http://www.activeunit.com/scripts/watermark.zip
Could someone take a look in this script please?

Thanks!



Re: help on watermark php script

Posted by: rlparker
Posted on: 2006-07-28 01:08:00

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.

--rlparker


Edited by rlparker on 07/28/06 02:17 AM (server time).

Re: help on watermark php script

Posted by: netdcon
Posted on: 2006-07-28 10:10:00

In reply to:

But i don't know how to make it works in dreamhost, i try everything i know, and nothing works.
The script code is :
http://www.activeunit.com/scripts/watermark.zip
Could someone take a look in this script please?


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

Re: help on watermark php script

Posted by: kchrist
Posted on: 2006-07-28 10:55:00

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

Re: help on watermark php script

Posted by: rlparker
Posted on: 2006-07-28 13:32:00

Nedcon,

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.

--rlparker

Re: help on watermark php script

Posted by: fakestar
Posted on: 2006-07-28 15:38:00

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 :)

---------------------------

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

Edited by fakestar on 07/28/06 04:15 PM (server time).

Re: help on watermark php script

Posted by: Atropos7
Posted on: 2006-07-28 16:36:00

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 Atropos | openvein.org

Re: help on watermark php script

Posted by: rlparker
Posted on: 2006-07-28 18:35:00

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?
--rlparker

Re: help on watermark php script

Posted by: rlparker
Posted on: 2006-07-28 18:39:00

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!
--rlparker


Re: help on watermark php script

Posted by: Atropos7
Posted on: 2006-07-28 23:25:00

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 Atropos | openvein.org

Re: help on watermark php script

Posted by: rlparker
Posted on: 2006-07-29 00:00:00

In reply to:

It might be a good example of watermarking an image but its a bad example of a script in general.


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!

--rlparker

Re: help on watermark php script

Posted by: fakestar
Posted on: 2006-07-29 17:06:00

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


Re: help on watermark php script

Posted by: silkrooster
Posted on: 2006-07-29 20:56:00

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

Re: help on watermark php script

Posted by: netdcon
Posted on: 2006-07-31 15:47:00

In reply to:

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 <img> 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


Re: help on watermark php script

Posted by: Atropos7
Posted on: 2006-08-03 02:21:00

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: http://atropos.openvein.org/web/dreamhost/php/watermark/source/

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 Atropos | openvein.org

Re: help on watermark php script

Posted by: rlparker
Posted on: 2006-08-03 02:31:00

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!

--rlparker

Re: help on watermark php script

Posted by: Raz2133
Posted on: 2006-08-03 21:38:00

In reply to:

I think it is *great* you are doing this, and I appreciate the chance to learn from you re-written code.


Me too :)

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 :)

Mark

Re: help on watermark php script

Posted by: fakestar
Posted on: 2006-08-06 02:09:00

Yeah, it's working! :)

Example:

http://fotocomedia.com/images/articles/coelho-gigante-assustando-menina_1.jpg

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!

Re: help on watermark php script

Posted by: fakestar
Posted on: 2006-09-20 20:59:00

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!



Re: help on watermark php script

Posted by: gordaen
Posted on: 2006-09-21 09:54:00

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.

Re: help on watermark php script

Posted by: kngtaco
Posted on: 2006-09-24 11:34:00

i think you'd be better off (and a lot less coding) using imagemagick commands in PHP.

Re: help on watermark php script

Posted by: hopsterguy
Posted on: 2006-12-29 20:47:00

Is there a way to get this script to work when another script is calling the image on the server side?? For example, I have a php script that calls and displays the images as part of a forum. If I directly link the image, this works fine, but when the php script calls the image, nothing is displayed.

See an example here...

http://www.buggynews.com/topic7317.html

In the above thread, the thumbnails are fine, but when you click on the thumbnail for the large version it doesn't work.

Re: help on watermark php script

Posted by: Atropos7
Posted on: 2006-12-29 21:29:00

The watermark script as-is works standalone.

It would be possible to write a library that can be included to other scripts of course, so that modifying the image is just a matter of a function call, eg

image = get_image(file);
watermark(image, 'text');
display_image(image);


cool Atropos | openvein.org

Tags: php scriptpng imagephp filedreamhosthttpscriptshelp