PHP code to add watermark to images

WBOY
Release: 2016-07-25 08:46:10
Original
814 people have browsed it

When using PHP programming, it is often necessary to add a watermark to the uploaded image to determine the copyright and source of the image. However, generally the location of the watermark is the lower right corner of the image. However, the color levels of different images are different. Sometimes the watermark of our picture is the same as the color level of the picture itself, which will cause the watermark to be inconspicuous. The following code can automatically identify the color level of the picture and add the watermark of the picture based on the color level difference. This can avoid the watermark and the color level of the picture. The same disadvantages.

  1. */
  2. function add_wm($nmw_water, $src_file, $output_file, $x, $y) {
  3. if(file_exists($output_file))
  4. return;
  5. $w1 = MagickGetImageWidth( $nmw_water);
  6. $h1 = MagickGetImageHeight($nmw_water);
  7. $nmw =NewMagickWand();
  8. MagickReadImage($nmw, $src_file);
  9. // Default watermark position adjustment
  10. $lt_w = 50;
  11. $lt_h = 50;
  12. if($x == 0){
  13. $w = MagickGetImageWidth($nmw);
  14. $h = MagickGetImageHeight($nmw);
  15. $x = $w;
  16. $y = $h ;
  17. }else{
  18. // Adjust according to specific circumstances
  19. $lt_w = 30;
  20. $lt_h = 40;
  21. }
  22. MagickCompositeImage($nmw, $nmw_water, MW_OverCompositeOp, $x - $w1 - $lt_w, $y - $h1 - $lt_h);
  23. MagickWriteImage($nmw, $output_file);
  24. DestroyMagickWand($nmw);
  25. }
  26. // Groovy's eachFileRecurse is still easy to use
  27. function add_wm_recurse($nmw_water, $to_dir, $output_dir , $arr) {
  28. $dp = dir($to_dir);
  29. while($file=$dp->read()){
  30. if($file != '.' && $file != ' ..'){
  31. if(is_dir($to_dir . '/' . $file)){
  32. mkdir($output_dir . '/' . $file);
  33. add_wm_recurse($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $arr);
  34. }else{
  35. if(!array_key_exists($to_dir . '/' . $file, $arr)){
  36. continue;
  37. }
  38. $sub_arr = $arr[$to_dir . '/' . $file];
  39. if($sub_arr){
  40. $x = intval($sub_arr[0]);
  41. $y = intval($sub_arr[1]);
  42. add_wm($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $x, $y);
  43. }
  44. }
  45. }
  46. }
  47. $dp->close() ;
  48. }
  49. $to_dir = './resized';
  50. $output_dir = './output';
  51. // This is the coordinate array (posX , posY)
  52. $arr = array(
  53. array(50, 50)
  54. );
  55. $water = './water.png';
  56. $nmw_water =NewMagickWand();
  57. MagickReadImage($nmw_water, $water);
  58. add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr);
  59. DestroyMagickWand($nmw_water);
Copy code

php


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!