Home > Backend Development > PHP Tutorial > PHP batch watermarks all pictures in a folder

PHP batch watermarks all pictures in a folder

WBOY
Release: 2016-07-28 08:25:51
Original
1112 people have browsed it

php 给指定文件夹下的所有图片批量打水印

<?php
	define("ROOTDIR", &#39;C:\Users\ADMIN\Pictures\20160715&#39;);
	define("DSDIR", "C:\Users\ADMIN\Pictures\watermarked-3");
	define("SEPARATER", &#39;\\&#39;);
	$watermark = imagecreatefrompng(&#39;watermark.png&#39;); //水印文件
	$wsx = imagesx($watermark); //水印宽度
	$wsy = imagesy($watermark); //水印高度
	$filenames = scandir(ROOTDIR); //读取文件夹下的所有文件
	$i = 0;
	//遍历所有文件
	foreach($filenames as $name){
		switch ($name) {
			case &#39;.&#39;: //文件夹本身不处理
				break;
			case &#39;..&#39;: //上级文件夹不处理
				break;
			default: // 读取图片文件(png,jpg)
				if(&#39;png&#39;==strstr($name, &#39;png&#39;)){
					$image = imagecreatefrompng(ROOTDIR.SEPARATER.$name);
				}else{
					$image = imagecreatefromjpeg(ROOTDIR.SEPARATER.$name);
				}
				
				$isx = imagesx($image);
				$isy = imagesy($image);
				$flag = imagecopy($image, $watermark, $isx-$wsx-20, $isy-$wsy-20, 0, 0, $wsx, $wsy);
				if($flag){
					imagejpeg($image,DSDIR.SEPARATER.$name); //保存文件
					imagedestroy($image);//内存回收
				}else{
					echo &#39;失败&#39;;
				}
		}
	}
	
?>
Copy after login

以上就介绍了 php 给文件夹下的所有图片批量打水印,包括了方面的内容,希望对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