PHP - Dynamically Watermarking image and displaying within HTML page.

Discussion in 'OT Technology' started by jdw, Oct 10, 2006.

  1. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    Working on writing an image gallery and want the tumnails to open a popup with database information, couple links, and the image.

    I would like it to dynamically lay the watermark over it and display it without having to save the image. I've got it working if I save the image, but no dice on displaying it without saving it. Possible? How?

    I'm working with a bastardized version of a watermark code I found online.
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    post the code. Basically, you want to watermark the image in memory, and then put it in the response stream back to the client. Either that, or you can save a temporary image on the server, and just link to it....
     
  3. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    Code:
    <?
    $watermark = imagecreatefrompng('watermark.png');
    $watermark_width = imagesx($watermark);
    $watermark_height = imagesy($watermark);
    $newfile = "show.jpg";
    $image = imagecreatetruecolor($watermark_width, $watermark_height);
    $image = imagecreatefromjpeg("test.jpg");
    $size = getimagesize("test.jpg");
    $dest_x = ($size[0] - $watermark_width)/2;
    $dest_y = ($size[1] - $watermark_height)/2;
    imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 8);
    imagejpeg($image, $newfile);
    //print "start";
    //print "$image";
    print "<img src=\"show.jpg\">";
    //print "end";
    imagedestroy($image);
    imagedestroy($watermark);
    
    ?>
     
  4. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    http://us2.php.net/manual/en/function.imagejpeg.php

    calling imagejpeg with $image alone would output to the browser. calling with a second variable ($newfile) will output it to that path.

    change:

    imagejpeg($image, $newfile);

    to:

    imagejpeg($image);
     
  5. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    oh, and outputting to the browser won't create HTML for you, you would do something like this:

    display.htm:

    Code:
    <img src="watermark.php">
    
    watermark.php:

    Code:
    <?
    header("Content-type: image/jpeg");
    $watermark = imagecreatefrompng('watermark.png');
    $watermark_width = imagesx($watermark);
    $watermark_height = imagesy($watermark);
    $newfile = "show.jpg";
    $image = imagecreatetruecolor($watermark_width, $watermark_height);
    $image = imagecreatefromjpeg("test.jpg");
    $size = getimagesize("test.jpg");
    $dest_x = ($size[0] - $watermark_width)/2;
    $dest_y = ($size[1] - $watermark_height)/2;
    imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 8);
    imagejpeg($image);
    imagedestroy($image);
    imagedestroy($watermark);
    ?>
    
     
  6. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Or you could call watermark.php alone in your browser (as long as there are no print statements), the browser will recognize it as an image and display it. Make note of the header("Content-type: image/jpeg"); line
     
  7. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    Understood that part - issue was I couldn't get it to print the image without saving it.

    However, I think I fully understand what you wrote afterwards here so I will work on that and see how it works.

    :bowdown:




    EDIT
    :bigthumb:
    Works...
    now so much more to do.
     
    Last edited: Oct 10, 2006
  8. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Oh my jesus god PHP is fucking ugly. God damn.
     
  9. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    Wait, so you want to have it watermark the image every time its viewed? Why not just save a watermarked image (as a copy) and just call that up?

    You will rape your server this way....
     
  10. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    php? ugly? You are kidding right?

    perl is like the messiest piece of shit language Ive ever seen.
     
  11. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    I agree with o2 that calling php on every image view is going to be more intensive than saving an actual file with the watermark but I can understand why you'd want it that way (masking true paths, keeping track of views/downloads).

    You could always use .htaccess to prevent hotlinking of certain images from other sites.
     
  12. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    :werd:
     
  13. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    I would process the image once, watermarking it, then save it to the disk, and reference that for each additional time after that. Then you only process the image the very first time somebody views it, which saves on your processing time, and still solves your problem of not wanting to pre-watermark all of your images up front
     
  14. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    I considered saving the file for bandwidth reasons. Question is, just how much more bandwidth will it burn building that image? It still has to load it each time. I've been through this dilema before - thought about building thumbs dynamically. I just want to keep the server a little cleaner.


    More I read through this the more I realize I need to do to get the desired effect.

    have been messing with .htaccess the last couple hours. Question I had with that - can I prevent hotlinking to specific URLs? I've got an uploads folder and a directory for this business inside my public html folder. I only want to prevent hotlinking to files in the business directory. Possible?
     
  15. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    The dynamic image vs saved image thing is about processor load rather than actual bandwidth, the bandwidth will remain the same but when you run PHP to create an image the server has to work more to create it (as opposed to saying, "here it is!").

    As for .htaccess preventing hotlinking, yep, I don't know it offhand so I'd just be searching on google for the answers the same as you so I'll leave that to you (or someone here). :)
     
  16. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND

    Guess I"ll make that decision.

    Couldn't find anything about only hotlinking to certain URLs yet on Google - guess I'll look more later/tomorrow.
     
  17. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Try these:

    http://www.netmore.net/mini_htaccess.shtml

    http://en.wikipedia.org/wiki/Htaccess

    Prevent hotlinking from a specific domain:
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} ^http://([^/]+\.)?baddomain1\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} ^http://([^/]+\.)?baddomain2\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} ^http://([^/]+\.)?baddomain3\.com [NC]
    RewriteRule \.(gif|jpg)$ http://www.example.com/hotlink.gif [R,L]
    
    -or-

    Only allow linking from domains:
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]
    RewriteRule \.(gif|jpg)$ http://www.example.com/hotlink.gif [R,L]
    
     
  18. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Read ANY code on the CPAN. Its clean.
     
  19. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Whats more, your namespace is so huge you have random fucking function names being called there, in series, with no comments or indentation. That is the definition of SHIT code. In perl, you would never, ever do that. Have you actually read any perl code? Me thinks not. Almost all the code in the... largest online code repository in existence, the CPAN, is beautiful.
     
  20. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    I watermark images on my site, usually it doesnt add more than 5kb. Watermarking images on the fly on every page load is VERY unefficient. You might save a few megs-gigs of bw, but your server will be overloaded so badly that the beneift would be nullified.
     
  21. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    While you're right about shit code indentation and the lack of comments is 100% up to the author of the code. You can't fault a language because someone doesn't comment his own code. I could go and take any perl snippet and remove all the comments and indentation and it'd look just as ugly. The thing about PHP is just that it's incredibly easy to code, as such you're going to have a lot more inexperienced programmers using it and with that you sometimes get shitty code. I don't understand all the PHP hate.
     
  22. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    The namespace issues encourage shit code.
     
  23. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    That's all stuff I've worked with already - issue is different though. For instance I want to prevent hotlinking from OT to any images in the 'business' folders however I want to allow hotlinking to images in my personal side.

    Apparently the .htaccess file can't be outside of the public_html folder or I'd just stick it in the 'root' for the business (unless I screwed something up when I tried it out).
     

Share This Page