PHP實現圖片浮水印根據顏色環境的動態添加功能

來源:互聯網
上載者:User
本篇文章主要介紹PHP實現圖片浮水印根據顏色環境的動態添加功能,感興趣的朋友參考下,希望對大家有所協助。

本文執行個體講述了PHP根據圖片色界在不同位置加浮水印的方法,具體實現方法如下:

function add_wm($nmw_water, $src_file, $output_file, $x, $y) {        if(file_exists($output_file))          return;        $w1 = MagickGetImageWidth($nmw_water);        $h1 = MagickGetImageHeight($nmw_water);        $nmw =NewMagickWand();        MagickReadImage($nmw, $src_file);        // 預設的加浮水印位置調整        $lt_w = 50;        $lt_h = 50;        if($x == 0){          $w = MagickGetImageWidth($nmw);          $h = MagickGetImageHeight($nmw);          $x = $w;          $y = $h;        }else{          // 根據具體情況調整          $lt_w = 30;          $lt_h = 40;        }        MagickCompositeImage($nmw, $nmw_water, MW_OverCompositeOp, $x - $w1 - $lt_w, $y - $h1 - $lt_h);        MagickWriteImage($nmw, $output_file);        DestroyMagickWand($nmw);          }      // 還是groovy的eachFileRecurse好用啊      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';      // 這個是我用java的ImageIO遍曆圖片像素擷取的符合褲子顏色的地區的座標array(posX, posY)      $arr = array(        array(50, 50)      );      $water = './water.png';      $nmw_water =NewMagickWand();      MagickReadImage($nmw_water, $water);      add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr);      DestroyMagickWand($nmw_water);

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

相關推薦:

php針對事件機制的定義與實現

php針對檔案操作及字串加密的方法

php中mysql實現的增刪改查功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.