This article mainly introduces PHP uses the Magickwand module operation picture to add the watermark method, the interest friend's reference, hoped to be helpful to everybody.
In the use of PHP programming, many times need to upload images and watermark, to determine the copyright and provenance of the image. However, in general, the location of the watermark is the lower right corner of the picture, but the different color levels of the picture, sometimes our image of the watermark and the picture itself is the same color scale, will cause the watermark is not obvious.
The following code can be implemented to automatically identify the color scale of the picture, more color difference to add a picture watermark, so as to avoid the same watermark and picture levels of the shortcomings.
<?php function Add_wm ($NMW _water, $src _file, $output _file, $x, $y) {if (file_exists ($output _file)) return; $w 1 = Magick Getimagewidth ($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{//Adjust $lt _w = 40 depending on the situation; $lt _h. Magickcompositeimage ($NMW, $NMW _water, Mw_overcompositeop, $x-$w 1-$lt _w, $y-$h 1-$lt _h); Magickwriteimage ($NMW, $output _file); Destroymagickwand ($NMW); }//or groovy Eachfilerecurse useful ah function add_wm_recurse ($NMW _water, $to _dir, $output _dir, $arr) {$dp = Dir ($to _dir); whi Le ($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 I used to traverse the image pixel of the ImageIO in Java to get the area of the trousers color. $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 used to access the ImageMagick image processing library. Here is a code snippet that uses Magicwand:
$magick _wand=newmagickwand (); Magickreadimage ($magick _wand, ' rose.jpg '); $drawing _wand=newdrawingwand ();D Rawsetfont ($drawing _wand, "/usr/share/ Fonts/bitstream-vera/vera.ttf ");D rawsetfontsize ($drawing _wand,20);D rawsetgravity ($drawing _wand,mw_ centergravity); $pixel _wand=newpixelwand (); Pixelsetcolor ($pixel _wand, "white");D Rawsetfillcolor ($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 PHP extensions directory
3. Total increase in php.ini files Extension=php_magickwand_q16_st.dll
4. Restart Apache
Summary: The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
PHP File upload class sharing _php instance
Example of PHP Page class
PHP implementation of the storage cycle for one day shopping cart class detailed