This article describes the implementation of PHP based on the image color in different locations of the watermark method. Share to everyone for your reference. as follows:
In the use of PHP programming, often need to upload the image watermark, to determine the image copyright and provenance. However, in general, the location of the watermark is the lower right corner of the picture, but, different colors of the picture, sometimes our image watermark and the image itself, the same color, it will create a watermark is not obvious.
The following code can be used to automatically identify the color of the picture, more color gradient to add a picture of the watermark, so that you can avoid watermarks and picture levels of the same disadvantage.
<?php function Add_wm ($NMW _water, $src _file, $output _file, $x, $y) {if (file_exists ($output _file)) return;
$w 1 = magickgetimagewidth ($NMW _water);
$h 1 = magickgetimageheight ($NMW _water);
$NMW =newmagickwand ();
Magickreadimage ($NMW, $src _file);
The default watermark position adjustment $lt _w = 50;
$lt _h = 50;
if ($x = = 0) {$w = Magickgetimagewidth ($NMW);
$h = Magickgetimageheight ($NMW);
$x = $w;
$y = $h;
}else{//Adjusted $LT _w = 30 According to the specific conditions;
$lt _h = 40;
Magickcompositeimage ($NMW, $NMW _water, Mw_overcompositeop, $x-$w 1-$lt _w, $y-$h 1-$lt _h);
Magickwriteimage ($NMW, $output _file);
Destroymagickwand ($NMW);
//or Groovy's eachfilerecurse is handy. function Add_wm_recurse ($NMW _water, $to _dir, $output _dir, $arr) {$dp = Dir ($to _dir); while ($file = $DP->read ()) {if ($file!= '. ' && $file!= ' ... ') {if (Is_dir, $to _dir. '/' . $file)) {mkdir ($output _dir). '/' .
$file); Add_wm_recurse ($NMW _water, $to _dir. '/' . $file, $output _dir. '/' .
$file, $arr); }else{if (!array_key_exists ($to _diR. '/' .
$file, $arr)) {continue; $sub _arr = $arr [$to _dir. '/' .
$file];
if ($sub _arr) {$x = Intval ($sub _arr[0]);
$y = Intval ($sub _arr[1]); ADD_WM ($NMW _water, $to _dir. '/' . $file, $output _dir. '/' .
$file, $x, $y);
$DP->close ()}}};
} $to _dir = './resized ';
$output _dir = './output ';
This is the coordinate array (POSX, posy) $arr = Array (Array (50, 50)) that matches the color of the trousers with the Java ImageIO traversing the image pixel;
$water = './water.png ';
$NMW _water =newmagickwand ();
Magickreadimage ($NMW _water, $water);
Add_wm_recurse ($NMW _water, $to _dir, $output _dir, $arr);
Destroymagickwand ($NMW _water);?>
Add:
PHP image processing Module Magickwand usage
Magickwand is a PHP module that is used to access the ImageMagick image processing library. Here is a snippet of code using Magicwand:
$magick _wand=newmagickwand ();
Magickreadimage ($magick _wand, ' rose.jpg ');
$drawing _wand=newdrawingwand ();
Drawsetfont ($drawing _wand, "/usr/share/fonts/bitstream-vera/vera.ttf");
Drawsetfontsize ($drawing _wand,20);
Drawsetgravity ($drawing _wand,mw_centergravity);
$pixel _wand=newpixelwand ();
Pixelsetcolor ($pixel _wand, "white");
Drawsetfillcolor ($drawing _wand, $pixel _wand);
if (Magickannotateimage ($magick _wand, $drawing _wand,0,0,0, "Rose")!= 0)
{
Magickechoimageblob ($magick _ Wand);
}
else
{
echo magickgetexceptionstring ($magick _wand);
>
Installation method:
1. Download Php_magickwand_q16_st.dll for 5.2.x
2. Place it in the extended directory of PHP
3. Total increase in php.ini documents Extension=php_magickwand_q16_st.dll
4. Restart Apache
I hope this article will help you with your PHP program design.