Heim >php教程 >php手册 >PHP根据图片色阶确定位置打水印

PHP根据图片色阶确定位置打水印

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 10:41:32824Durchsuche

直接分享源码

<ol class="dp-c">
<li class="alt"><span><span><?php     </span></span></span></li>
<li>
<span>    </span><span class="keyword">function</span><span> add_wm(</span><span class="vars">$nmw_water</span><span>, </span><span class="vars">$src_file</span><span>, </span><span class="vars">$output_file</span><span>, </span><span class="vars">$x</span><span>, </span><span class="vars">$y</span><span>) {    </span>
</li>
<li class="alt">
<span>        </span><span class="keyword">if</span><span>(</span><span class="func">file_exists</span><span>(</span><span class="vars">$output_file</span><span>))    </span>
</li>
<li>
<span>            </span><span class="keyword">return</span><span>;    </span>
</li>
<li class="alt"><span>    </span></li>
<li>
<span>        </span><span class="vars">$w1</span><span> = MagickGetImageWidth(</span><span class="vars">$nmw_water</span><span>);    </span>
</li>
<li class="alt">
<span>        </span><span class="vars">$h1</span><span> = MagickGetImageHeight(</span><span class="vars">$nmw_water</span><span>);    </span>
</li>
<li><span>    </span></li>
<li class="alt">
<span>        </span><span class="vars">$nmw</span><span> =NewMagickWand();    </span>
</li>
<li>
<span>        MagickReadImage(</span><span class="vars">$nmw</span><span>, </span><span class="vars">$src_file</span><span>);    </span>
</li>
<li class="alt"><span>    </span></li>
<li>
<span>        </span><span class="comment">// 默认的加水印位置调整   </span><span> </span>
</li>
<li class="alt">
<span>        </span><span class="vars">$lt_w</span><span> = 50;    </span>
</li>
<li>
<span>        </span><span class="vars">$lt_h</span><span> = 50;    </span>
</li>
<li class="alt"><span>    </span></li>
<li>
<span>        </span><span class="keyword">if</span><span>(</span><span class="vars">$x</span><span> == 0){    </span>
</li>
<li class="alt">
<span>            </span><span class="vars">$w</span><span> = MagickGetImageWidth(</span><span class="vars">$nmw</span><span>);    </span>
</li>
<li>
<span>            </span><span class="vars">$h</span><span> = MagickGetImageHeight(</span><span class="vars">$nmw</span><span>);    </span>
</li>
<li class="alt"><span>    </span></li>
<li>
<span>            </span><span class="vars">$x</span><span> = </span><span class="vars">$w</span><span>;    </span>
</li>
<li class="alt">
<span>            </span><span class="vars">$y</span><span> = </span><span class="vars">$h</span><span>;    </span>
</li>
<li>
<span>        }</span><span class="keyword">else</span><span>{    </span>
</li>
<li class="alt">
<span>            </span><span class="comment">// 根据具体情况调整   </span><span> </span>
</li>
<li>
<span>            </span><span class="vars">$lt_w</span><span> = 30;    </span>
</li>
<li class="alt">
<span>            </span><span class="vars">$lt_h</span><span> = 40;    </span>
</li>
<li><span>        }    </span></li>
<li class="alt"><span>    </span></li>
<li>
<span>        MagickCompositeImage(</span><span class="vars">$nmw</span><span>, </span><span class="vars">$nmw_water</span><span>, MW_OverCompositeOp, </span><span class="vars">$x</span><span> - </span><span class="vars">$w1</span><span> - </span><span class="vars">$lt_w</span><span>, </span><span class="vars">$y</span><span> - </span><span class="vars">$h1</span><span> - </span><span class="vars">$lt_h</span><span>);    </span>
</li>
<li class="alt">
<span>        MagickWriteImage(</span><span class="vars">$nmw</span><span>, </span><span class="vars">$output_file</span><span>);    </span>
</li>
<li><span>    </span></li>
<li class="alt">
<span>        DestroyMagickWand(</span><span class="vars">$nmw</span><span>);            </span>
</li>
<li><span>    }    </span></li>
<li class="alt"><span>    </span></li>
<li>
<span>    </span><span class="comment">// 还是groovy的eachFileRecurse好用啊   </span><span> </span>
</li>
<li class="alt">
<span>    </span><span class="keyword">function</span><span> add_wm_recurse(</span><span class="vars">$nmw_water</span><span>, </span><span class="vars">$to_dir</span><span>, </span><span class="vars">$output_dir</span><span>, </span><span class="vars">$arr</span><span>) {    </span>
</li>
<li>
<span>        </span><span class="vars">$dp</span><span> = dir(</span><span class="vars">$to_dir</span><span>);    </span>
</li>
<li class="alt">
<span>        </span><span class="keyword">while</span><span>(</span><span class="vars">$file</span><span>=</span><span class="vars">$dp</span><span>->read()){    </span>
</li>
<li>
<span>            </span><span class="keyword">if</span><span>(</span><span class="vars">$file</span><span> != </span><span class="string">.</span><span> && </span><span class="vars">$file</span><span> != </span><span class="string">..</span><span>){    </span>
</li>
<li class="alt">
<span>                </span><span class="keyword">if</span><span>(</span><span class="func">is_dir</span><span>(</span><span class="vars">$to_dir</span><span> . </span><span class="string">/</span><span> . </span><span class="vars">$file</span><span>)){    </span>
</li>
<li>
<span>                    </span><span class="func">mkdir</span><span>(</span><span class="vars">$output_dir</span><span> . </span><span class="string">/</span><span> . </span><span class="vars">$file</span><span>);    </span>
</li>
<li class="alt">
<span>                    add_wm_recurse(</span><span class="vars">$nmw_water</span><span>, </span><span class="vars">$to_dir</span><span> . </span><span class="string">/</span><span> . </span><span c>
</span>
</li>
</ol>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn