PHP根據圖片色界在不同位置加浮水印

來源:互聯網
上載者:User

PHP根據圖片色界在不同位置加浮水印

  1. function add_wm($nmw_water, $src_file, $output_file, $x, $y) {
  2. if(file_exists($output_file))
  3. return;
  4. $w1 = MagickGetImageWidth($nmw_water);
  5. $h1 = MagickGetImageHeight($nmw_water);
  6. $nmw =NewMagickWand();
  7. MagickReadImage($nmw, $src_file);
  8. // 預設的加浮水印位置調整
  9. $lt_w = 50;
  10. $lt_h = 50;
  11. if($x == 0){
  12. $w = MagickGetImageWidth($nmw);
  13. $h = MagickGetImageHeight($nmw);
  14. $x = $w;
  15. $y = $h;
  16. }else{
  17. // 根據具體情況調整
  18. $lt_w = 30;
  19. $lt_h = 40;
  20. }
  21. MagickCompositeImage($nmw, $nmw_water, MW_OverCompositeOp, $x - $w1 - $lt_w, $y - $h1 - $lt_h);
  22. MagickWriteImage($nmw, $output_file);
  23. DestroyMagickWand($nmw);
  24. }
  25. // 還是groovy的eachFileRecurse好用啊
  26. function add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr) {
  27. $dp = dir($to_dir);
  28. while($file=$dp->read()){
  29. if($file != '.' && $file != '..'){
  30. if(is_dir($to_dir . '/' . $file)){
  31. mkdir($output_dir . '/' . $file);
  32. add_wm_recurse($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $arr);
  33. }else{
  34. if(!array_key_exists($to_dir . '/' . $file, $arr)){
  35. continue;
  36. }
  37. $sub_arr = $arr[$to_dir . '/' . $file];
  38. if($sub_arr){
  39. $x = intval($sub_arr[0]);
  40. $y = intval($sub_arr[1]);
  41. add_wm($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $x, $y);
  42. }
  43. }
  44. }
  45. }
  46. $dp->close();
  47. }
  48. $to_dir = './resized';
  49. $output_dir = './output';
  50. // 這個是我用java的ImageIO遍曆圖片像素擷取的符合褲子顏色的地區的座標array(posX, posY)
  51. $arr = array(
  52. array(50, 50)
  53. );
  54. $water = './water.png';
  55. $nmw_water =NewMagickWand();
  56. MagickReadImage($nmw_water, $water);
  57. add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr);
  58. DestroyMagickWand($nmw_water);
複製代碼
色界, PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.