/    Sign up×
Community /Pin to ProfileBookmark

Moving watermark from centre & remove RGB code

Hi there,

Im using this code to watermark images but i want to remove the watermark from the centre and move it to the bottom right of the image.

There is also some sort of RGB code there but i dont think i need it though.
i dont want to remove anything from it incase im remove something thats needed. im still new to php

heres the code:

[code=php]<?php
class watermark{

# given two images, return a blended watermarked image
function create_watermark( $main_img_obj, $watermark_img_obj, $alpha_level = 100 ) {
$alpha_level /= 100; # convert 0-100 (%) alpha to decimal

# calculate our images dimensions
$main_img_obj_w = imagesx( $main_img_obj );
$main_img_obj_h = imagesy( $main_img_obj );
$watermark_img_obj_w = imagesx( $watermark_img_obj );
$watermark_img_obj_h = imagesy( $watermark_img_obj );

# determine center position coordinates
$main_img_obj_min_x = floor( ( $main_img_obj_w / 2 ) – ( $watermark_img_obj_w / 2 ) );
$main_img_obj_max_x = ceil( ( $main_img_obj_w / 2 ) + ( $watermark_img_obj_w / 2 ) );
$main_img_obj_min_y = floor( ( $main_img_obj_h / 2 ) – ( $watermark_img_obj_h / 2 ) );
$main_img_obj_max_y = ceil( ( $main_img_obj_h / 2 ) + ( $watermark_img_obj_h / 2 ) );

# create new image to hold merged changes
$return_img = imagecreatetruecolor( $main_img_obj_w, $main_img_obj_h );

# walk through main image
for( $y = 0; $y < $main_img_obj_h; $y++ ) {
for( $x = 0; $x < $main_img_obj_w; $x++ ) {
$return_color = NULL;

# determine the correct pixel location within our watermark
$watermark_x = $x – $main_img_obj_min_x;
$watermark_y = $y – $main_img_obj_min_y;

# fetch color information for both of our images
$main_rgb = imagecolorsforindex( $main_img_obj, imagecolorat( $main_img_obj, $x, $y ) );

# if our watermark has a non-transparent value at this pixel intersection
# and we’re still within the bounds of the watermark image
if ( $watermark_x >= 0 && $watermark_x < $watermark_img_obj_w &&
$watermark_y >= 0 && $watermark_y < $watermark_img_obj_h ) {
$watermark_rbg = imagecolorsforindex( $watermark_img_obj, imagecolorat( $watermark_img_obj, $watermark_x, $watermark_y ) );

# using image alpha, and user specified alpha, calculate average
$watermark_alpha = round( ( ( 127 – $watermark_rbg[‘alpha’] ) / 127 ), 2 );
$watermark_alpha = $watermark_alpha * $alpha_level;

# calculate the color ‘average’ between the two – taking into account the specified alpha level
$avg_red = $this->_get_ave_color( $main_rgb[‘red’], $watermark_rbg[‘red’], $watermark_alpha );
$avg_green = $this->_get_ave_color( $main_rgb[‘green’], $watermark_rbg[‘green’], $watermark_alpha );
$avg_blue = $this->_get_ave_color( $main_rgb[‘blue’], $watermark_rbg[‘blue’], $watermark_alpha );

# calculate a color index value using the average RGB values we’ve determined
$return_color = $this->_get_image_color( $return_img, $avg_red, $avg_green, $avg_blue );

# if we’re not dealing with an average color here, then let’s just copy over the main color
} else {
$return_color = imagecolorat( $main_img_obj, $x, $y );

} # END if watermark

# draw the appropriate color onto the return image
imagesetpixel( $return_img, $x, $y, $return_color );

} # END for each X pixel
} # END for each Y pixel

# return the resulting, watermarked image for display
return $return_img;

} # END create_watermark()

# average two colors given an alpha
function _get_ave_color( $color_a, $color_b, $alpha_level ) {
return round( ( ( $color_a * ( 1 – $alpha_level ) ) + ( $color_b * $alpha_level ) ) );
} # END _get_ave_color()

# return closest pallette-color match for RGB values
function _get_image_color($im, $r, $g, $b) {
$c=imagecolorexact($im, $r, $g, $b);
if ($c!=-1) return $c;
$c=imagecolorallocate($im, $r, $g, $b);
if ($c!=-1) return $c;
return imagecolorclosest($im, $r, $g, $b);
} # EBD _get_image_color()

} # END watermark API
?>[/code]

can it be simplified

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@bokehAug 09.2006 — There is also some sort of RGB code there but i dont think i need it though.

i dont want to remove anything from it incase im remove something thats needed. [/QUOTE]
Yes it is!

[code=php]
# determine center position coordinates
$main_img_obj_min_x = floor( ( $main_img_obj_w / 2 ) - ( $watermark_img_obj_w / 2 ) );
$main_img_obj_max_x = ceil( ( $main_img_obj_w / 2 ) + ( $watermark_img_obj_w / 2 ) );
$main_img_obj_min_y = floor( ( $main_img_obj_h / 2 ) - ( $watermark_img_obj_h / 2 ) );
$main_img_obj_max_y = ceil( ( $main_img_obj_h / 2 ) + ( $watermark_img_obj_h / 2 ) ); [/code]
[/QUOTE]
Change to:[code=php]# determine bottom right corner -5 pixels
$main_img_obj_max_x = floor( $main_img_obj_w - 5) - ( $watermark_img_obj_w);
$main_img_obj_min_x = $main_img_obj_max_x - $watermark_img_obj_w;
$main_img_obj_max_y = floor( $main_img_obj_h - 5) - ( $watermark_img_obj_h);
$main_img_obj_min_y = $main_img_obj_max_y - $watermark_img_obj_h;[/code]


can it be simplified[/QUOTE]This code is already pretty well written so don't worry about changing it unless your needs are pretty specific.
Copy linkTweet thisAlerts:
@popcopauthorAug 09.2006 — http://www.popcop.co.uk/sawyou/watermark/watermark_test.php

it doesnt seem to moving it to the bottom right yet... want to get it right in the corner
Copy linkTweet thisAlerts:
@bokehAug 09.2006 — Well code below works for me but I do agree this function is unnecessarilly over complcated. Are you bothered about the varying the watermark transparency or do you just want to superimpose it?

[code=php]class watermark{

# given two images, return a blended watermarked image
function create_watermark( $main_img_obj, $watermark_img_obj, $alpha_level = 100 ) {
$alpha_level /= 100; # convert 0-100 (%) alpha to decimal

# calculate our images dimensions
$main_img_obj_w = imagesx( $main_img_obj );
$main_img_obj_h = imagesy( $main_img_obj );
$watermark_img_obj_w = imagesx( $watermark_img_obj );
$watermark_img_obj_h = imagesy( $watermark_img_obj );

# determine bottom right corner -5 pixels
$main_img_obj_max_x = floor( $main_img_obj_w - 5);
$main_img_obj_min_x = $main_img_obj_max_x - $watermark_img_obj_w;
$main_img_obj_max_y = floor( $main_img_obj_h - 5);
$main_img_obj_min_y = $main_img_obj_max_y - $watermark_img_obj_h;

# create new image to hold merged changes
$return_img = imagecreatetruecolor( $main_img_obj_w, $main_img_obj_h );

# walk through main image
for( $y = 0; $y < $main_img_obj_h; $y++ ) {
for( $x = 0; $x < $main_img_obj_w; $x++ ) {
$return_color = NULL;

# determine the correct pixel location within our watermark
$watermark_x = $x - $main_img_obj_min_x;
$watermark_y = $y - $main_img_obj_min_y;

# fetch color information for both of our images
$main_rgb = imagecolorsforindex( $main_img_obj, imagecolorat( $main_img_obj, $x, $y ) );

# if our watermark has a non-transparent value at this pixel intersection
# and we're still within the bounds of the watermark image
if ( $watermark_x >= 0 && $watermark_x < $watermark_img_obj_w &&
$watermark_y >= 0 && $watermark_y < $watermark_img_obj_h ) {
$watermark_rbg = imagecolorsforindex( $watermark_img_obj, imagecolorat( $watermark_img_obj, $watermark_x, $watermark_y ) );

# using image alpha, and user specified alpha, calculate average
$watermark_alpha = round( ( ( 127 - $watermark_rbg['alpha'] ) / 127 ), 2 );
$watermark_alpha = $watermark_alpha * $alpha_level;

# calculate the color 'average' between the two - taking into account the specified alpha level
$avg_red = $this->_get_ave_color( $main_rgb['red'], $watermark_rbg['red'], $watermark_alpha );
$avg_green = $this->_get_ave_color( $main_rgb['green'], $watermark_rbg['green'], $watermark_alpha );
$avg_blue = $this->_get_ave_color( $main_rgb['blue'], $watermark_rbg['blue'], $watermark_alpha );

# calculate a color index value using the average RGB values we've determined
$return_color = $this->_get_image_color( $return_img, $avg_red, $avg_green, $avg_blue );

# if we're not dealing with an average color here, then let's just copy over the main color
} else {
$return_color = imagecolorat( $main_img_obj, $x, $y );

} # END if watermark

# draw the appropriate color onto the return image
imagesetpixel( $return_img, $x, $y, $return_color );

} # END for each X pixel
} # END for each Y pixel

# return the resulting, watermarked image for display
return $return_img;

} # END create_watermark()

# average two colors given an alpha
function _get_ave_color( $color_a, $color_b, $alpha_level ) {
return round( ( ( $color_a * ( 1 - $alpha_level ) ) + ( $color_b * $alpha_level ) ) );
} # END _get_ave_color()

# return closest pallette-color match for RGB values
function _get_image_color($im, $r, $g, $b) {
$c=imagecolorexact($im, $r, $g, $b);
if ($c!=-1) return $c;
$c=imagecolorallocate($im, $r, $g, $b);
if ($c!=-1) return $c;
return imagecolorclosest($im, $r, $g, $b);
} # EBD _get_image_color()

} # END watermark API[/code]
Copy linkTweet thisAlerts:
@popcopauthorAug 09.2006 — basically ive created the png with a trasparent background.... it just want to display it with the transparent background but i want to have it in the bottom right corner
Copy linkTweet thisAlerts:
@bokehAug 09.2006 — Well check out the link in my signature "[I]watermarking images[/I]".
Copy linkTweet thisAlerts:
@popcopauthorAug 09.2006 — i had a look but cos im new to php i never understood how to create the files extra and what to name them..
Copy linkTweet thisAlerts:
@popcopauthorAug 09.2006 — i made the watermark_fuction.php

but for the second part of the code... what do i name that file

and what code do i use to actually see the watermark working?
Copy linkTweet thisAlerts:
@bokehAug 09.2006 — Post [I]test watermark[/I] and I will quickly convert it.
Copy linkTweet thisAlerts:
@popcopauthorAug 09.2006 — sorry i dont understand... what do u want me to post?
Copy linkTweet thisAlerts:
@bokehAug 09.2006 — Your code ? :mad: ?
×

Success!

Help @popcop 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...