class watermark3{
# 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 ); // получение
/*
$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
# walk through main image
{ // расчет коэфициентов масштабаци водяного знака $outKof
$kofW = 1; // коэфициент масштаба по ширине
$kofH = 1; // коэфициент масштаба по высоте
if ($main_img_obj_w!=0 && $main_img_obj_h==0) // подгоняем ширену под W
{
$kofW = $watermark_img_obj_w / $main_img_obj_w; // коэфициент масштаба
$kofH = $kofW;
}
if ($main_img_obj_w==0 && $main_img_obj_h!=0) // подгоняем ширену под W
{
$kofH = $watermark_img_obj_h / $main_img_obj_h; // коэфициент масштаба
$kofW = $kofH;
}
if ($main_img_obj_w!=0 && $main_img_obj_h!=0) // автоматическое подгонка
{
$kofW = $watermark_img_obj_w / $main_img_obj_w; // коэфициент масштаба
$kofH = $watermark_img_obj_h / $main_img_obj_h; // коэфициент масштаба
}
$outKof = max($kofW,$kofH); // высота вотермарки
$img_yO = floor( $watermark_img_obj_h / $outKof ); // ширина вотермарки
$img_xO = floor( $watermark_img_obj_w / $outKof );
// echo "$main_img_obj_w - $main_img_obj_h <br>";
// echo "$img_xO - $img_yO";
}
$main_img_2_w = floor($main_img_obj_w / 2
); $main_img_2_h = floor($main_img_obj_h / 2
); $w_img_2_w = floor($img_xO / 2
); $w_img_2_h = floor($img_yO / 2
);
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;
$watermark_x_ = $x - $main_img_obj_min_x*$outKof;
$watermark_y_ = $y - $main_img_obj_min_y*$outKof;
# 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 ) */
if (($w_img_2_w - abs($x - $main_img_2_w))>=0 and
($w_img_2_h - abs($y - $main_img_2_h))>=0
) {
$watermark_x = floor((($x - $main_img_2_w) + $w_img_2_w) * $outKof); $watermark_y = floor((($y - $main_img_2_h) + $w_img_2_h) * $outKof); # 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
// echo "$x - $y = $watermark_x - $watermark_y <br>";
}
else
{
} # END if watermark
# draw the appropriate color onto the return image
} # 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)
{
if ($c!=-1) return $c;
if ($c!=-1)
return $c;
} # EBD _get_image_color()
}