automatic image stamp

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
ssyfrig
Posts: 364
Joined: Tue 2. Mar 2004, 17:01
Location: Zürich / Switzerland
Contact:

automatic image stamp

Post by ssyfrig »

Hello phpwcms community

does any body have a image stamp solution for phpwcms?


Image

Image

I found this on the web ... it works standalone ..



thanks, Sven

Code: Select all

<?php 
// This script will show image with logo on it 
// Supported are PNGs,GIFs,JPEGs 
// ------------------------------------------------------ 
// call it: http://www.domain.com/showimage.php?img=<name> 
// where <name> is name of image... 
// example: 
// http://www.domain.com/showimage.php?img=001.jpg 
// ------------------------------------------------------ 
// You have to set parameters here: 
// ------------------------------------------------------ 
$image_quality="70"; 
// quality of JPEG conpression [0-100] 
// ------------------------------------------------------ 
$image_path="./images/"; 
// path to images 
// examples: 
// $image_path="./images/"; 
// $image_path="../../images/"; 
// ------------------------------------------------------ 
$logo_path="./logo.png"; 
// path and name of the LOGO image (PNG,GIF,JPEG) 
// examples: 
// $logo_path="./logos/img_logo.png"; 
// $logo_path="../../logos/img_logo.png" 
// ------------------------------------------------------ 
$logo_pos_x="center"; 
// left, right, center 
$logo_pos_y="bottom"; 
// top, middle, bottom 
// ------------------------------------------------------ 
$error_not_found="File doesn't exists"; 
// where image is not found, show this error text 
$error_not_supported="This image type isn't supported"; 
// where image is not supported, show this error text 
$error_bg_color=array(255,255,255); 
// image background color in RGB - (RED,GREEN,BLUE) 
$error_text_color=array(255,0,0); 
// text color in RGB - (RED,GREEN,BLUE) 
// ------------------------------------------------------ 
//  YOU DON'T HAVE TO EDIT CODE BELOW THIS LINE 
// ------------------------------------------------------ 
// SCRIPT written by Ladislav Soukup, [root@soundboss.cz] 
// ------------------------------------------------------ 
function NewImage($width,$height,$text=""){ 
   global $error_bg_color,$error_text_color; 
   if (function_exists("imagecreatetruecolor")){ 
      if (!@$img=imagecreatetruecolor($width,$height)){ 
         $img=imagecreate($width,$height); 
      } 
   } else { 
      $img=imagecreate($width,$height); 
   } 
   $imgbgcolor=ImageColorAllocate($img,$error_bg_color[0],$error_bg_color[1],$error_bg_color[2]); 
   $imgtextcolor=ImageColorAllocate($img,$error_text_color[0],$error_text_color[1],$error_text_color[2]); 
   imagefilledrectangle($img,0,0,$width,$height,$imgbgcolor); 
   imagestring($img,5,10,10,$text,$imgtextcolor); 
   return($img); 
} 
Header("Content-type: image/jpeg"); 
$exp=GMDate("D, d M Y H:i:s",time()+999); 
Header("Expires: $exp GMT"); 
$rep_from=array("./","../"); 
$rep_to=array("","",); 
$_GET["img"]=str_replace($rep_from,$rep_to,$_GET["img"]); 
$file=$image_path . $_GET["img"]; 
if (file_exists($file)){ 
   $info=getimagesize($file); 
   $width=$info[0]; 
   $height=$info[1]; 
   if ($info[2]==1){ 
      $img=@imagecreatefromgif($file); 
   } else if ($info[2]==2){ 
      $img=@imagecreatefromjpeg($file); 
   } else if ($info[2]==3){ 
      $img=@imagecreatefrompng($file); 
   } else { 
      $width=640; 
      $height=480; 
      $img=NewImage($width,$height,$error_not_supported); 
   } 
} else { 
   $width=640; 
   $height=480; 
   $img=NewImage($width,$height,$error_not_found); 
} 
if (file_exists($logo_path)){ 
   $info=getimagesize($logo_path); 
   $logo_width=$info[0]; 
   $logo_height=$info[1]; 
   if ($info[2]==1){ 
      $img_logo=imagecreatefromgif($logo_path); 
   } else if ($info[2]==2){ 
      $img_logo=imagecreatefromjpeg($logo_path); 
   } else if ($info[2]==3){ 
      $img_logo=imagecreatefrompng($logo_path); 
   } else { 
      $logo_width=120; 
      $logo_height=20; 
      $img=NewImage($logo_width,$logo_height,$error_not_supported); 
   } 
   // positioning - X 
   if ($logo_pos_x=="left"){ 
      $dst_x=10; 
   } else if ($logo_pos_x=="center"){ 
      $dst_x=round(($width-$logo_width)/2); 
   } else if ($logo_pos_x=="right"){ 
      $dst_x=$width-10-$logo_width; 
   } else { 
      $dst_x=round(($width-$logo_width)/2); 
   } 
   // positioning - Y 
   if ($logo_pos_y=="top"){ 
      $dst_y=5; 
   } else if ($logo_pos_y=="middle"){ 
      $dst_y=round(($height-($logo_height/2))/2); 
   } else if ($logo_pos_y=="bottom"){ 
      $dst_y=$height-5-$logo_height; 
   } else { 
      $dst_y=round(($height-($logo_height/2))/2); 
   } 
   imagecopy($img,$img_logo,$dst_x,$dst_y,0,0,$logo_width,$logo_height); 
} 
ImageJpeg($img,"",$image_quality); 
?> 
ssyfrig
Posts: 364
Joined: Tue 2. Mar 2004, 17:01
Location: Zürich / Switzerland
Contact:

Post by ssyfrig »

hello

does nobody have any input?

thanks, Sven
ssyfrig
Posts: 364
Joined: Tue 2. Mar 2004, 17:01
Location: Zürich / Switzerland
Contact:

[solved] automatic image stamp

Post by ssyfrig »

i found a solution


1.)
create a file called wrapper.php

Code: Select all

<?php

$watermark = "watermark.png";
$image = $_SERVER["PATH_TRANSLATED"];

if (empty($image)) die();

if (!file_exists($image)) {
   header("404 Not Found");
   echo "File Not Found."; die();
}

$outputType = getFileType($image);

watermark($image, $watermark, $outputType);

/**
   Outputs the image $source with $watermark in the lower right corner.
   @param $source the source image
   @param $watermark the watermark to apply
   @param $outputType the type to output as (png, jpg, gif, etc.)
                      defaults to the image type of $source if left blank
*/
function watermark($source, $watermark, $outputType="") {
   $sourceType = getFileType($source);
   $watermarkType = getFileType($watermark);

   if (empty($outputType)) $outputType = $sourceType;
   if ($outputType == "gif") $outputType = "png"; // Okay to remove after July 2004
   header("Content-type:image/$outputType");

   // We're only looking at the file name, not the directory name
   $fileName = basename($source);

   // Match dots "."
   preg_match_all("/(\.)/", $fileName, $dots);

   // We only care about match #1
   $dots = $dots[1];

   if (count($dots) > 1) {
      // More than one dot, so don't do watermarking

      $fp = fopen($source, "rb");
      $contents = fread($fp, filesize($source));
      fclose($fp);
      echo $contents;
   }

   else {

      // Continue with watermarking

      // Derive function names
      $createSource = "ImageCreateFrom".strtoupper($sourceType);
      $showImage = "Image".strtoupper($outputType);
      $createWatermark = "ImageCreateFrom".strtoupper($watermarkType);

      // Load original and watermark to memory
      $output = $createSource($source);
      $logo = $createWatermark($watermark);
      ImageAlphaBlending($output, true);

      // Find proper coordinates so watermark will be in the lower right corner
      $x = ImageSX($output) - ImageSX($logo);
      $y = ImageSY($output) - ImageSY($logo);

      // Display
      ImageCopy($output, $logo, $x, $y, 0, 0, ImageSX($logo), ImageSY($logo));
      $showImage($output);

      // Purge
      ImageDestroy($output);
      ImageDestroy($logo);
   }
}

function getFileType($string) {
   $type = strtolower(eregi_replace("^(.*)\.","",$string));
   if ($type == "jpg") $type = "jpeg";
   return $type;
}
?>
2.)
create a file or edit your .htaccess file with this content

Code: Select all

AddHandler watermarked .jpg
Action watermarked /watermark/wrapper.php
3.)
create a png file with your watermark and save it as watermark.png

4.)

create a folder under root called watermark
upload watermark.png & wrapper.php to to the watermark folder

5.)
in Release 1.1-RC4 27-08-2004 copy the .htaccess file to the folder /phpwcms_tmp/thumb_preview/


that's it.


(I doesen't write this code, but I lost the download URL)

have fun , Sven

PS: tested with IE 6.0 and Firefox

you can see the result on the pop-up's
http://www.igamt.ch/index.php?id=32,98,0,0,1,0

...
Last edited by ssyfrig on Thu 8. Sep 2005, 09:46, edited 4 times in total.
Pappnase

Post by Pappnase »

hello

nice script! i move this thread to the hacks and enhancements section!
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

Very nice
I think there is a mistake in the code, you have forgotten to close the php code

"?>"

TriP
ssyfrig
Posts: 364
Joined: Tue 2. Mar 2004, 17:01
Location: Zürich / Switzerland
Contact:

Post by ssyfrig »

yep ....

is fixt...

Sven
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

Just one quick question

is the only thing you need to do is the following

1. create wrapper file
2. add code to .htaccess

and then it works??

TriP
ssyfrig
Posts: 364
Joined: Tue 2. Mar 2004, 17:01
Location: Zürich / Switzerland
Contact:

Post by ssyfrig »

yes and create a watermark.png file with your watermark
and place the .htaccess to your image folders

Sven
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

ssyfrig wrote:yes and create a watermark.png file with your watermark
and place the .htaccess to your image folders

Sven
Could you amend your first post and add the process step by step, you did not mention that the .htaccess needs to sit in the image directory... this is a vital step... if it is in root it wont work...

question
can this work for gif images?

TriP
ssyfrig
Posts: 364
Joined: Tue 2. Mar 2004, 17:01
Location: Zürich / Switzerland
Contact:

Post by ssyfrig »

can this work for gif images?
yes

Code: Select all

AddHandler watermarked .jpg 
AddHandler watermarked .gif

Action watermarked /watermark/wrapper.php 
Sven
jscholtysik

Post by jscholtysik »

Hi ssyfrig,


but the website visitor still has the possibility to save the picture without the watermark to his harddisk. So why should I watermark my pictures when the visitor can get it without... :roll:


Joachim
ssyfrig
Posts: 364
Joined: Tue 2. Mar 2004, 17:01
Location: Zürich / Switzerland
Contact:

Post by ssyfrig »

@jscholtysik

tell us how?

thanks, Sven
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

jscholtysik wrote:Hi ssyfrig,


but the website visitor still has the possibility to save the picture without the watermark to his harddisk. So why should I watermark my pictures when the visitor can get it without... :roll:


Joachim
mmm
this is a deterant.... it could stop people from doing it... however you do have a point here....

TriP
jscholtysik

Post by jscholtysik »

Hi trip,


when I want to prevent the image stealing, the only effective in my opinion way is to upload images already containing watermarks, do that noboby is able to remove the watermark...


Joachim
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

jscholtysik wrote:Hi trip,


when I want to prevent the image stealing, the only effective in my opinion way is to upload images already containing watermarks, do that noboby is able to remove the watermark...


Joachim
This is an issue all over the internet, the problem is the time taken to add watermark to images!

This method although is an overlay, does help keep people from trying.

TriP
Post Reply