Home Backend Development PHP Tutorial php生成缩略图,加水印种

php生成缩略图,加水印种

Jun 13, 2016 pm 01:02 PM
height return width

php生成缩略图,加水印类

这个一个简单的GD库操作

<?php /****
 燕十八 公益PHP讲堂

 论  坛: http://www.zixue.it
 微  博: http://weibo.com/Yshiba
 YY频道: 88354001
 ****/

/***
 想操作图片
 先得把图片的大小,类型信息得到

 水印:就是把指定的水印复制到目标上,并加透明效果

 缩略图:就是把大图片复制到小尺寸画面上
 ***/

class ImageTool {
	// imageInfo 分析图片的信息
	// return array()
	public static function imageInfo($image) {
		// 判断图片是否存在
		if (!file_exists($image)) {
			return false;
		}

		$info = getimagesize($image);

		if ($info == false) {
			return false;
		}

		// 此时info分析出来,是一个数组
		$img['width'] = $info[0];
		$img['height'] = $info[1];
		$img['ext'] = substr($info['mime'], strpos($info['mime'], '/') + 1);

		return $img;
	}

	/*
	 加水印功能
	 parm String $dst 等操作图片
	 parm String $water 水印小图
	 parm String $save,不填则默认替换原始图
	 */
	public static function water($dst, $water, $save = NULL, $pos = 2, $alpha = 50) {
		// 先保证2个图片存在
		if (!file_exists($dst) || !file_exists($water)) {
			return false;
		}

		// 首先保证水印不能比待操作图片还大
		$dinfo = self::imageInfo($dst);
		$winfo = self::imageInfo($water);

		if ($winfo['height'] > $dinfo['height'] || $winfo['width'] > $dinfo['width']) {
			return false;
		}

		// 两张图,读到画布上! 但是图片可能是png,可能是jpeg,用什么函数读?
		$dfunc = 'imagecreatefrom' . $dinfo['ext'];
		$wfunc = 'imagecreatefrom' . $winfo['ext'];

		if (!function_exists($dfunc) || !function_exists($wfunc)) {
			return false;
		}

		// 动态加载函数来创建画布
		$dim = $dfunc($dst);
		// 创建待操作的画布
		$wim = $wfunc($water);
		// 创建水印画布

		// 根据水印的位置 计算粘贴的坐标
		switch($pos) {
			case 0 :
				// 左上角
				$posx = 0;
				$posy = 0;
				break;

			case 1 :
				// 右上角
				$posx = $dinfo['width'] - $winfo['width'];
				$posy = 0;
				break;

			case 3 :
				// 左下角
				$posx = 0;
				$posy = $dinfo['height'] - $winfo['height'];
				break;

			default :
				$posx = $dinfo['width'] - $winfo['width'];
				$posy = $dinfo['height'] - $winfo['height'];
		}

		// 加水印
		imagecopymerge($dim, $wim, $posx, $posy, 0, 0, $winfo['width'], $winfo['height'], $alpha);

		// 保存
		if (!$save) {
			$save = $dst;
			unlink($dst);
			// 删除原图
		}

		$createfunc = 'image' . $dinfo['ext'];
		$createfunc($dim, $save);

		imagedestroy($dim);
		imagedestroy($wim);

		return true;
	}

	/**
	 thumb 生成缩略图
	 等比例缩放,两边留白
	 **/
	public static function thumb($dst, $save = NULL, $width = 200, $height = 200) {
		// 首先判断待处理的图片存不存在
		$dinfo = self::imageInfo($dst);
		if ($dinfo == false) {
			return false;
		}

		// 计算缩放比例
		$calc = min($width / $dinfo['width'], $height / $dinfo['height']);

		// 创建原始图的画布
		$dfunc = 'imagecreatefrom' . $dinfo['ext'];
		$dim = $dfunc($dst);

		// 创建缩略画布
		$tim = imagecreatetruecolor($width, $height);

		// 创建白色填充缩略画布
		$white = imagecolorallocate($tim, 255, 255, 255);

		// 填充缩略画布
		imagefill($tim, 0, 0, $white);

		// 复制并缩略
		$dwidth = (int)$dinfo['width'] * $calc;
		$dheight = (int)$dinfo['height'] * $calc;

		$paddingx = (int)($width - $dwidth) / 2;
		$paddingy = (int)($height - $dheight) / 2;

		imagecopyresampled($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $dinfo['width'], $dinfo['height']);

		// 保存图片
		if (!$save) {
			$save = $dst;
			unlink($dst);
		}

		$createfunc = 'image' . $dinfo['ext'];
		$createfunc($tim, $save);

		imagedestroy($dim);
		imagedestroy($tim);

		return true;

	}

}

// print_r(ImageTool::imageInfo('./home.jpg'));
/*
 echo ImageTool::water('./home.jpg','./smallfeng.png','home1.jpg',0)?'OK':'FAIL';
 echo ImageTool::water('./home.jpg','./smallfeng.png','home2.jpg',1)?'OK':'FAIL';
 echo ImageTool::water('./home.jpg','./smallfeng.png','home3.jpg',2)?'OK':'FAIL';
 echo ImageTool::water('./home.jpg','./smallfeng.png','home4.jpg',3)?'OK':'FAIL';
 */

Copy after login


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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

What does the width of html mean? What does the width of html mean? Jun 03, 2021 pm 02:15 PM

In HTML5, width means width. The width attribute defines the width of the element's content area. You can add inner margins, borders, and outer margins outside the content area. You only need to set "element {width: value}" to the element.

How does Vue3 use setup syntax sugar to refuse to write return How does Vue3 use setup syntax sugar to refuse to write return May 12, 2023 pm 06:34 PM

Vue3.2 setup syntax sugar is a compile-time syntax sugar that uses the combined API in a single file component (SFC) to solve the cumbersome setup in Vue3.0. The declared variables, functions, and content introduced by import are exposed through return, so that they can be used in Vue3.0. Problems in use 1. There is no need to return declared variables, functions and content introduced by import during use. You can use syntactic sugar //import the content introduced import{getToday}from'./utils'//variable constmsg='Hello !'//function func

Detailed explanation of CSS dimension properties: height and width Detailed explanation of CSS dimension properties: height and width Oct 21, 2023 pm 12:42 PM

Detailed explanation of CSS dimension properties: height and width In front-end development, CSS is a powerful style definition language. Among them, height and width are the two most basic dimension attributes, used to define the height and width of the element. This article will analyze these two properties in detail and provide specific code examples. 1. Height attribute The height attribute is used to define the height of an element. You can use pixel, percentage or

Use the return keyword in JavaScript Use the return keyword in JavaScript Feb 18, 2024 pm 12:45 PM

Usage of return in JavaScript requires specific code examples In JavaScript, the return statement is used to specify the value returned from a function. Not only can it be used to end the execution of a function, it can also return a value to the place where the function was called. The return statement has the following common uses: Return a value The return statement can be used to return a value to the place where the function is called. Here is a simple example: functionadd(a,b){

Detailed explanation of JavaScript function return values ​​and return statements Detailed explanation of JavaScript function return values ​​and return statements Aug 04, 2022 am 09:46 AM

JavaScript functions provide two interfaces to interact with the outside world. The parameters serve as the entrance to receive external information; the return value serves as the outlet to feed back the operation results to the outside world. The following article will take you to understand the JavaScript function return value and briefly analyze the usage of the return statement. I hope it will be helpful to you!

How to use return statement in JavaScript How to use return statement in JavaScript Feb 26, 2024 am 09:21 AM

How to use return in JavaScript requires specific code examples. In JavaScript, return is a very important keyword. It is usually used to return a value in a function or end the execution of a function. The return statement is used to return a value to the caller of the function and terminate the execution of the function. The return statement can be used anywhere in a function and can return any JavaScript data type, including numbers, strings, booleans,

See all articles