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

PHP batch watermarks all pictures in a folder

Jul 28, 2016 am 08:25 AM
image quot

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

<?php
	define("ROOTDIR", 'C:\Users\ADMIN\Pictures\20160715');
	define("DSDIR", "C:\Users\ADMIN\Pictures\watermarked-3");
	define("SEPARATER", '\\');
	$watermark = imagecreatefrompng('watermark.png'); //水印文件
	$wsx = imagesx($watermark); //水印宽度
	$wsy = imagesy($watermark); //水印高度
	$filenames = scandir(ROOTDIR); //读取文件夹下的所有文件
	$i = 0;
	//遍历所有文件
	foreach($filenames as $name){
		switch ($name) {
			case '.': //文件夹本身不处理
				break;
			case '..': //上级文件夹不处理
				break;
			default: // 读取图片文件(png,jpg)
				if('png'==strstr($name, 'png')){
					$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 '失败';
				}
		}
	}
	
?>
Copy after login

以上就介绍了 php 给文件夹下的所有图片批量打水印,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Bing Image Creator for free How to use Bing Image Creator for free Feb 27, 2024 am 11:04 AM

How to use Bing Image Creator for free

How to delete images from Xiaomi phones How to delete images from Xiaomi phones Mar 02, 2024 pm 05:34 PM

How to delete images from Xiaomi phones

Imagemagic installation Centos and Image installation tutorial Imagemagic installation Centos and Image installation tutorial Feb 12, 2024 pm 05:27 PM

Imagemagic installation Centos and Image installation tutorial

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

imagefilledrectangle() function in PHP imagefilledrectangle() function in PHP Aug 30, 2023 am 09:05 AM

imagefilledrectangle() function in PHP

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 Jun 13, 2016 am 10:15 AM

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题

为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 Jun 13, 2016 am 10:24 AM

为什么小弟我在php上写的这个代码,在浏览器上什么都不显示

图片消失怎么解决 图片消失怎么解决 Apr 07, 2024 pm 03:02 PM

图片消失怎么解决

See all articles