Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
create watermark on the fly in PHP?
I’m traying to add watermark into photos on the fly :
<txp:php> global $img_dir; $liste = custom_field(array('name'=>'Liste_ID_Images')); if (!empty($liste)) { foreach(preg_split('/[^\d]+/', $liste) as $id) { extract(safe_row('*', 'txp_image', 'id='.$id)); //tells the webserver that the output will be an image header('content-type: image/jpeg'); $_image = "".hu.$img_dir."/".$id.$ext.""; $watermark = "".hu.$img_dir."/filigrane.png"; //gets the watermark $watermark = imagecreatefrompng($watermark); echo $watermark; //gets the dimensions of the watermark $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $image = imagecreatetruecolor($watermark_width, $watermark_height); //gets the image to put the watermark on and related information about it $image = imagecreatefromjpeg($_image); $size = getimagesize($_image); $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); echo "<li><a href=\"".hu."".$img_dir."/".$id.$ext."\" class=\"lightwindow hidden\" rel=\"[Automne]\" params=\"\" title=\"".addslashes(htmlspecialchars($caption))."\"><img src=\"".hu.$img_dir."/".$id."t".$ext."\" width=\"12%\" height=\"12%\" alt=\"photography\" title=\"Show\"/></a></li>\r"; } } </txp:php>
but images are parsed in binary code.
Any ideas how to make that rule?
Many thanks for your help.
Regards,
Last edited by Pat64 (2007-11-03 09:49:09)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: create watermark on the fly in PHP?
Can’t help with the code Patrick but there is a plug-in that might be useful even if it is only to see how it is coded.
Stuart
In a Time of Universal Deceit
Telling the Truth is Revolutionary.
Offline
Re: create watermark on the fly in PHP?
thebombsite wrote:
Can’t help with the code Patrick but there is a plug-in that might be useful even if it is only to see how it is coded.
i think pat already attempted to looked into that plugin except all links are dead and its no longer available anywhere as far as i can tell
Offline
Re: create watermark on the fly in PHP?
Oh right. It is an “old” plug-in. I didn’t check the link.
Stuart
In a Time of Universal Deceit
Telling the Truth is Revolutionary.
Offline
Re: create watermark on the fly in PHP?
Hi Stuart and Steve,
Tks for your reply… but I’d just find a solution :) If you’re interested, look at this
It’s a very simple method and very cool one.
Here is how I made this rule :
1.° Put the “waternark.php” script into the root of your website ;
Here is the code :
<?php // this script creates a watermarked image from an image file - can be a .jpg .gif or .png file // where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script // where this script is named watermark.php // call this script with an image tag // <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg $imagesource = $_GET['path']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif") $image = @imagecreatefromgif($imagesource); if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource); if($filetype == ".png") $image = @imagecreatefrompng($imagesource); if (!$image) die(); $watermark = @imagecreatefromgif('watermark.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image); $watermarkwidth = imagesx($watermark); $watermarkheight = imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); $startheight = (($imageheight - $watermarkheight)/2); imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?>
2.° Create a form with this little PHP rows (with Rudd’s help ;) :
<txp:php> global $img_dir; $liste = custom_field(array('name'=>'Liste_ID_Images')); if (!empty($liste)) { foreach(preg_split('/[^\d]+/', $liste) as $id) { extract(safe_row('*, name', 'txp_image', 'id='.$id)); echo "<p><img src=\"watermark.php?path=".$img_dir."/".$id."t".$ext."\" /></p>\r"; echo "\r"; } } </txp:php>
3.° Now create your watermark image : a gif one or a png (need to change line #18 into watermark.php : $watermark = @imagecreatefrompng(‘watermark.png’) and put it on the root of your website ;
4.° It’s kind of magic : now all your images are render with a [beautiful] watermark on it :)
Have fun with this help, guys!
Now I have a new problem : how to deny access to url image by users within the browser address bar (cause all images within the “images” directory haven’t any watermark on it)? Some session can make the rule I think…
Cheers,
Last edited by Pat64 (2007-11-07 07:23:25)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: create watermark on the fly in PHP?
Pat64 wrote:
I’m traying to add watermark into photos on the fly
Nice solution, however, as you found out it’s only of use if people can’t guess the direct URL. With TXP’s numbered images it’s pretty easy (I’m sure there was a good reason for doing it that way, but I can’t figure it out!)
Now, you can deny access to the image directory using an .htaccess rule, though what effect that has on TXP’s operation I’ve yet to try. Guess it’ll be fine. But if you’re going that route then an alternative solution, albeit “outside” TXP, is to protect all images in a particular directory by adding an .htaccess rule that calls watermark.php
automatically based on the file extension and dir.
That way, even if people do get into your image dir and view a picture directly, it still looks like it has the watermark on it all the time.
I did contemplate pluginising my watermark solution; and may well do so, since you highlighted that sab_wm_image appears to have expired, and the community could probably do with one. But what I would do differently is offer it both as a plugin and a standalone piece of php that you could apply directly from .htaccess if you wished. It would be cool to offer it as a plugin-only solution but I don’t think it’s possible to do it completely automatically without .htaccess intervention. Anyone correct me on that?
For the record, my version was similar to the one you found but I gave it auto-detection of the watermark type (gif, png etc) and also built in the ability for the watermark to be sensitive to the picture itself (to a degree). i.e. you upload two watermarks; a dark and a light one. When the picture is predominantly light, the dark watermark is applied and vice versa.
Do you think that sort of thing would be of use to anyone? I could look into porting it to TXP if so.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: create watermark on the fly in PHP?
WoW! You’re amazing Stef! Your offer is great!
Depending number of answers of this thread, and the fact that sab_wm_image plugin have expired, seems it will be interesting to porting your solution to TXP.
As you said, watermarks are efficient only if visitors can’t access directly to images from url…
Based on the example of the file_download process works into TXP, do you think it will be difficult to adapt this feature for your project? Another idea seems more easy to code numbered images id as ZCR plugin seems to do with input names and ids.
What is your opinion Stef?
Cheeers,
Last edited by Pat64 (2007-11-17 17:38:30)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: create watermark on the fly in PHP?
> Bloke-eroo-ski
Dude, totally do it, plugin-only would be wicked.
Offline
Re: create watermark on the fly in PHP?
Isn’t this a bit CPU-intensive when doing the watermarking real-time for each image request?
Offline
Re: create watermark on the fly in PHP?
…So you suggest to add watermarks when we upload images into TXT, is’n it?
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: create watermark on the fly in PHP?
Correct. Perhaps keep both original and watermarked images in the images directory, redirecting all the requests for 1.jpg automatically to 1.watermarked.jpg using mod_redirect.
Offline
Re: create watermark on the fly in PHP?
Why not just upload a watermarked image right from the start? That’s what I’ve been doing so far so that I don’t have to worry about locking out the images folder etc. What are the additional benefits of having an unwatermarked and watermarked versions on the server. Won’t you always show the watermarked versions anyway? Also you’re going to double your images folder automatically.
Just curious, because I always go back & forth on this issue.
Last edited by JonahC (2007-11-16 22:18:23)
Jonah Calinawan
www.foodportraits.com
Offline