$q = 95;
$margin = 7;
$watermark_image_light = 'img/white.png';
$watermark_image_dark = 'img/black.png';
list($image_width, $image_height)
= getimagesize($ifn);
list($watermark_width, $watermark_height)
= getimagesize($watermark_image_light);
$watermark_x = $image_width - $margin - $watermark_width;
$watermark_y = $image_height - $margin - $watermark_height;
$watermark_x2 = $watermark_x + $watermark_width;
$watermark_y2 = $watermark_y + $watermark_height;
if ($watermark_x < 0 OR $watermark_y < 0 OR
$watermark_x2 > $image_width OR $watermark_y2 > $image_height OR
$image_width < $min_image OR $image_height < $min_image)
{
return;
}
$pict = imagecreatetruecolor(1, 1);
if ($ext == "jpg")
$creimg = imagecreatefromjpeg($ifn);
elseif ($ext == "gif")
$creimg = imagecreatefromgif($ifn);
elseif ($ext == "png")
$creimg = imagecreatefrompng($ifn);
imagecopyresampled($pict, $creimg, 0, 0, $watermark_x, $watermark_y, 1, 1, $watermark_width, $watermark_height);
$rgb = imagecolorat($pict, 0, 0);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$max = min($r, $g, $b);
$min = max($r, $g, $b);
$lightness = (double)(($max + $min) / 510.0);
imagedestroy($pict);
$watermark_image = ($lightness < 0.5) ? $watermark_image_light : $watermark_image_dark;
$watermark = imagecreatefrompng($watermark_image);
imagealphablending($creimg, TRUE);
imagealphablending($watermark, TRUE);
imagecopy($creimg, $watermark, $watermark_x, $watermark_y, 0, 0,$watermark_width, $watermark_height);
imagedestroy($watermark);
if ($ext == "jpg")
imagejpeg($creimg,$pictdest,$q);
elseif ($ext == "gif")
imagegif($creimg,$pictdest,$q);
elseif ($ext == "png")
imagepng($creimg,$pictdest,9,PNG_ALL_FILTERS);
imagedestroy($creimg);