/    Sign up×
Community /Pin to ProfileBookmark

Watermark image script

Hi,
Thanks for looking and I appreciate any help you can offer, I have a small script that I place in a folder, this folder has lots of image files, the script adds a small watermark to the images in the folder when viewed. Everything is working fine with JPG files but it is not showing the watermark with PNG file formats? any ideas on what could be wrong?

[CODE]<?

*/

// 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’, ‘my watermark’); // 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);
}

?>[/CODE]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@markandkelleauthorDec 11.2007 — so, I still can not find anything wrong with the script, perhaps it is the htaccess file, do I need to change this to allow the watermark on png files?

this is what my htaccess file looks like right now

[CODE]
addhandler wtmrk jpg
action wtmrk /photoimages/modify.php
[/CODE]


any ideas???
Copy linkTweet thisAlerts:
@bokehDec 11.2007 — do I need to change this to allow the watermark on png files?[/QUOTE]Yes! Add a handler for png files too.
Copy linkTweet thisAlerts:
@markandkelleauthorDec 11.2007 — hey thanks for the reply, so how do I do that?

like this...

[CODE]
addhandler wtmrk jpg
addhandler wtmrk png
action wtmrk /photoimages/modify.php
[/CODE]


because this does not work? any ideas?
Copy linkTweet thisAlerts:
@bokehDec 11.2007 — hey thanks for the reply, so how do I do that?

like this...

[CODE]
addhandler wtmrk jpg
addhandler wtmrk png
action wtmrk /photoimages/modify.php
[/CODE]


because this does not work? any ideas?[/QUOTE]
Are you sure the script is not running?
Copy linkTweet thisAlerts:
@markandkelleauthorDec 11.2007 — yes, this just shows an x where image should be?

however

I have modded the script so much that it could be a mistake somewhere I have made, so I have just downloaded it again and made the change you said and it works now :-) thanks for your help. awesome!
Copy linkTweet thisAlerts:
@WPpluginexpertsOct 21.2020 — Hey markandkelle,

Need and easy way to do it?

Just go through this plugin, may this help you.

[https://www.wppluginexperts.com/wp-plugin/product-image-watermark-for-woocommerce/](https://www.wppluginexperts.com/wp-plugin/product-image-watermark-for-woocommerce/)
×

Success!

Help @markandkelle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...