• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP制作图片缩略图、加水印、加字体 php网站制作教程 php网站后台制作教程 phpcms模板制

php 搞代码 3年前 (2022-01-23) 20次浏览 已收录 0个评论

下面是我提供的一个类,下面封装了这三种功能:

<?php /*** */class ImageHelper{	private $imgSrc;	private $info;	private $type;	private $image;	private $showOrSaveFunc;	public function __construct($imgSrc) {		$this->imgSrc = $imgSrc;		$this->init();	}	/**	* 初始化操作	*/	private function init() {		//获取图片信息(通过GD库提供的方法,得到你想要处理的图片的基本信息)		$this->info = getimagesize($this->imgSrc);		//通过图像的编号来获取图像的类型		$this->type = image_type_to_extension($this->info[2], false);		//在内存中创建一个和我们图像类型一样的图像		$fun = "imagecreatefrom{$this->type}";		//把图片复制到内存中		$this->image = $fun($this->imgSrc);		$this->showOrSaveFunc = "image{$this->type}";	}	/**	* 给图片添加字体	* @param $fontfile 字体文件路径	* @param $text 文字内容	* @param $red 红色分量	* @param $green 绿色分量	* @param $blue 蓝色分量	* @param $alpha 透明度	* @param $angle 偏移角度	* @param $size 字体大小	* @param $x 距离左边的距离	* @param $y 距离右边的距离	*/	public function addFont($fontfile, $text, $red, $green, $blue, $alpha		, $angle, $size, $x, $y) {		$color = imagecolorallocatealpha($this->image, $red, $green, $blue, $alpha);		imagettftext($this->image, $size, $angle, $x, $y, $color, $fontfile, $text);	}	/**	* 浏览器输出图片类型	*/	private function setOutputHeader() {		header("Content-Type:".$this->info['mime']);	}	/**	* 输出图片到浏览器	*/	public function outputImageToBrowser() {		$this->setOutputHeader();		$this->excShowOrSaveFunc($this->showOrSaveFunc);	}	/**	* 输出或者保存图片	*/	private function excShowOrSaveFunc($type, $file='') {		if ($file == '') {			$type($this->image);		} else {			$type($this->image, $file);		}	}	/**	* 保存文件到本地	* @param 本地路径	* @param 文件名	*/	public function outputImageToStorage($filepath, $filename) {		$this->setOutputHeader();		$this->excShowOrSaveFunc($this->showOrSaveFunc, $filepath.$filename.'.'.$this->type);	}	/**	* 为图片添加水印	* @param $waterMarkPath 水印图片路径	* @param $dst_x 水印距离原图左侧的距离	* @param $dst_y 水印距离原图上侧的距离	* @param $pct 水印的透明度	*/	public function addWaterMark($waterMarkPath,	 			$dst_x, $dst_y, $pct) {		$waterInfo = getimagesize($waterMarkPath);		$waterType = image_type_to_extension($waterInfo[2], false);		$waterFun = "imagecreatefrom{$waterType}";		$waterMarkImage = $waterFun($waterMarkPath);		//imagecopymerge(dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h, pct)		//pct透明度		imagecopymerge($this->image, $waterMarkImage, $dst_x, $dst_y,			 0, 0, $waterInfo[0], $waterInfo[1], $pct);		imagedestroy($waterMarkImage);	}	/**	* 生成图片缩略图	* @param $mWidth 想要压缩的宽度	*/	public function createThumbnail($mWidth) {		$width = $this->info[0];		$height = $this->info[1];		if ($mWidth >= $width) {			return ;		}			$mHeight = $height * $mWidth / $width;		//1.在内存中建立一个宽300、高200的真色彩图片		$imageThumb = imagecreatetruecolor($mWidth, $mHeight);		//2.核心步骤、将原图复制到新建的正色彩图片上,并按照一定比例压缩		//imagecopyresampled(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)		imagecopyresampled($imageThumb, $this->image, 0, 0,			 0, 0, $mWidth, $mHeight, $width, $height);			$this->image = $imageThumb;	}	/*	* 销毁图片资源	*/	public function destroy() {		i本文来源gaodai$ma#com搞$$代**码)网8搞代gaodaima码magedestroy($this->image);	}	} ?>

使用方法如下:

<?php 	require_once 'ImageHelper.class.php';	$imageHelper = new ImageHelper('P60219-211631.jpg');	/*$imageHelper->addFont("consola.ttf", "raid", 0, 0, 0, 50		, 0, 45, 20, 20);	$imageHelper->addWaterMark('pic2.jpg',	 			0, 0, 50);*/	$imageHelper->createThumbnail(300);	// $imageHelper->outputImageToBrowser();	$imageHelper->outputImageToStorage('','testImageHelperAddFont');	$imageHelper->destroy(); ?>

以上就介绍了PHP制作图片缩略图、加水印、加字体,包括了制作图片,php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP制作图片缩略图、加水印、加字体 php网站制作教程 php网站后台制作教程 phpcms模板制
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址