Jump to content

watermark on all images in a folder


s4salman

Recommended Posts

Hello i m using this script for putting watermark on all images in a folder, but it is not putting watermark.I entered modify.php(name of this script file)  so that it could add watermark, but it is not doing its job.Anyone can help.I m attaching  whole zip file of the script.

 

<?



// IMAGE WATERMARK (comment line below if you do not want to use image watermark)
Define('WATERMARK_IMAGE', './wt.png'); // path to watermark image
Define('WATERMARK_PERCENT', '60'); // Intensity of the transition (in percent)


// TEXT WATERMARK (comment line below if you do not want to use text)
//Define('WATERMARK_TEXT', 'Copyright (c) 2005 www.activeunit.com'); // text to place (image will not be used)
Define('WATERMARK_TEXT_FONT', '3'); // font 1 / 2 / 3 / 4 / 5
Define('TEXT_SHADOW', '1'); // 1 - yes / 0 - no
Define('TEXT_COLOR', '#FFFFFF'); // text color 


// GENERAL SETTINGS
Define('WATERMARK_ALIGN_H', 'right'); // left / right / center
Define('WATERMARK_ALIGN_V', 'bottom'); // top / bottom / center
Define('WATERMARK_MARGIN', '10'); // margin








// ----------------------------------------------------------------------------------------






$dr=preg_replace('/modify\.php.+/', '', $_SERVER['PHP_SELF']);
$filename=str_replace($dr, './', $_SERVER['PATH_INFO']);

$lst=GetImageSize($filename);
$image_width=$lst[0];
$image_height=$lst[1];
$image_format=$lst[2];

if ($image_format==1) {
   Header("Content-Type:image/gif");
   readfile($filename);
   exit;
} elseif ($image_format==2) {
  $old_image=imagecreatefromjpeg($filename);
} elseif ($image_format==3) {
  $old_image=imagecreatefrompng($filename);
} else {
   readfile($filename);
   exit;
}


if (Defined('WATERMARK_TEXT') && WATERMARK_TEXT!='') {
// text

  $color = eregi_replace("#","", TEXT_COLOR);
  $red = hexdec(substr($color,0,2));
  $green = hexdec(substr($color,2,2));
  $blue = hexdec(substr($color,4,2));

  $text_color = imagecolorallocate ($old_image, $red, $green, $blue); 

  $text_height=imagefontheight(WATERMARK_TEXT_FONT);
  $text_width=strlen(WATERMARK_TEXT)*imagefontwidth(WATERMARK_TEXT_FONT);
  $wt_y=WATERMARK_MARGIN;
  if (WATERMARK_ALIGN_V=='top') {
   $wt_y=WATERMARK_MARGIN;
  } elseif (WATERMARK_ALIGN_V=='bottom') {
   $wt_y=$image_height-$text_height-WATERMARK_MARGIN;
  } elseif (WATERMARK_ALIGN_V=='center') {
   $wt_y=(int)($image_height/2-$text_height/2);
  }

  $wt_x=WATERMARK_MARGIN;
  if (WATERMARK_ALIGN_H=='left') {
   $wt_x=WATERMARK_MARGIN;
  } elseif (WATERMARK_ALIGN_H=='right') {
   $wt_x=$image_width-$text_width-WATERMARK_MARGIN;
  } elseif (WATERMARK_ALIGN_H=='center') {
   $wt_x=(int)($image_width/2-$text_width/2);
  }

  if (TEXT_SHADOW=='1') {
   imagestring($old_image, WATERMARK_TEXT_FONT, $wt_x+1, $wt_y+1, WATERMARK_TEXT, 0);
  }
  imagestring($old_image, WATERMARK_TEXT_FONT, $wt_x, $wt_y, WATERMARK_TEXT, $text_color);

} 

if (Defined('WATERMARK_IMAGE') && WATERMARK_IMAGE!='' && file_exists(WATERMARK_IMAGE)) {
// image


$lst2=GetImageSize(WATERMARK_IMAGE);
$image2_width=$lst2[0];
$image2_height=$lst2[1];
$image2_format=$lst2[2];

if ($image2_format==2) {
  $wt_image=imagecreatefromjpeg(WATERMARK_IMAGE);
} elseif ($image2_format==3) {
  $wt_image=imagecreatefrompng(WATERMARK_IMAGE);
}

  if ($wt_image) {

   $wt_y=WATERMARK_MARGIN;
   if (WATERMARK_ALIGN_V=='top') {
    $wt_y=WATERMARK_MARGIN;
   } elseif (WATERMARK_ALIGN_V=='bottom') {
    $wt_y=$image_height-$image2_height-WATERMARK_MARGIN;
   } elseif (WATERMARK_ALIGN_V=='center') {
    $wt_y=(int)($image_height/2-$image2_height/2);
   }

   $wt_x=WATERMARK_MARGIN;
   if (WATERMARK_ALIGN_H=='left') {
    $wt_x=WATERMARK_MARGIN;
   } elseif (WATERMARK_ALIGN_H=='right') {
    $wt_x=$image_width-$image2_width-WATERMARK_MARGIN;
   } elseif (WATERMARK_ALIGN_H=='center') {
    $wt_x=(int)($image_width/2-$image2_width/2);
   }

   imagecopymerge($old_image, $wt_image, $wt_x, $wt_y, 0, 0, $image2_width, $image2_height, WATERMARK_PERCENT);
  }

}

if ($image_format==2) {
  imageJpeg($old_image);
}
if ($image_format==3) {
  imagePng($old_image);
}


?>

 

[attachment deleted by admin]

Link to comment
Share on other sites

 $dr=preg_replace('/modify\.php.+/', '', $_SERVER['PHP_SELF']);
$filename=str_replace($dr, './', $_SERVER['PATH_INFO']);

 

Here's the problem: It's not getting a filename properly.

 

If you absolutely don't want users downloading the file without the watermark, the images need to go in a folder that is not web-accessible.

Then you need to format how the script will get a filename. You might use a query string from a hard-coded path.

 

Example:

Images in this folder: /images

$directory = "/images/";
$filename = "{$directory}{$_SERVER[QUERY_STRING]}";

 

Image file: /images/test.jpg

HTML code: http://yoursite.com/path/modify.php?test.jpg

(Of course, you'd want to rename modify.php to something like image.php)

Link to comment
Share on other sites

This is interesting concept I want to look into. Does it put a water-mark on all image perminantly or just whenever loaded into php script ?

 

No. It dynamically adds the watermark. The image it outputs isn't saved to disk. If you want it to, you'd need to modify the source code, which is fairly easy.

Link to comment
Share on other sites

What exactly are you trying to do with the script? Just trying to run through a directory and save each image that has the watermark? If so, are you trying to replace the files that are there already, or save the watermarked images with a different name?

Link to comment
Share on other sites

i got this better tutorial.other people may like it.

 

<?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);
?>

 

Name this script, i.e. watermark.php and call this script as following:

HTML

<img src="watermark.php?path=image_name.filetype">

 

the only thing you need to chage is "image_name.filetype" and of course you can have the relative path such as:

HTML

<img src="folder/watermark.php?path=folder/imagename">

 

 

The caution here is the script watermark.php and watermark.gif should be in the same location. watermark.gif should have transparent background.

 

As you can read it from the site, the location where the watermark.gif appears can be modified by adjusting this line of the code:

CODE

$startwidth = (($imagewidth - $watermarkwidth)/2);

$startheight = (($imageheight - $watermarkheight)/2);

 

To have it appear on the bottom right corner, try this:

CODE

$startwidth = (($imagewidth - $watermarkwidth) );

$startheight = (($imageheight - $watermarkheight) );

Link to comment
Share on other sites

why u want to save same images again and again.just use the method i mentioned above.your images will be without water mark but when u call them like

 

<img src="folder/watermark.php?path=images/1.jpg">

 

the script just put water mark on it, but ur images will remain without watermark.

in short, there will be no alteration on images.

Link to comment
Share on other sites

I want to add a water mark to all images within a particular folder and then save them in another location?? is this a simple thing to do ?

 

Just alter this line:

imagejpeg($image);

to

// $newdir is the new folder where you want to save watermarked images -- relative to where the script is (and make sure it ends in a trailing slash)
$newdir = "../../new_folder/";

// JPEG quality is from 0-100 -- 90-95% will give you approximately your original filesize for the watermarked image
$quality = 90; 

// Save the file to disk
imagejpeg($image, "{$newdir}{$_GET[path]}", $quality);

 

Make sure you place the script in the folder where your images are. Then call watermark.php?path=image.jpg

The watermarked image will be saved in the folder that $newdir points to.

 

You'll have to manually call each file through this script to get it watermarked.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.