首页 php教程 php手册 功能强大的PHP图片处理类(水

功能强大的PHP图片处理类(水

Jun 06, 2016 pm 07:41 PM
p php图片处理

这篇文章主要汇总介绍了php图片处理类(水

非常强大的php图片处理类,可以自定义图片水印、透明度、图片缩放、图片锐化、图片旋转、图片翻转、图片剪切、图片反色。

 * 图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色
* 处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片,命名方式可以考虑在原图片的基础上加上步骤,例如:图片名称+__第几步

具体代码如下:

'没有找到相关图片!' ); /** * 构造函数:函数初始化 */ function __construct($PICTURE_URL){ $this -> get_info($PICTURE_URL); } function get_info($PICTURE_URL){ /** * 处理原图片的信息,先检测图片是否存在,不存在则给出相应的信息 */ @$SIZE = getimagesize($PICTURE_URL); if(!$SIZE){ exit($this -> ERROR['unalviable']); } // 得到原图片的信息类型、宽度、高度 $this -> PICTURE_MIME = $SIZE['mime']; $this -> PICTURE_WIDTH = $SIZE[0]; $this -> PICTURE_HEIGHT = $SIZE[1]; // 创建图片 switch($SIZE[2]){ case 1: $this -> PICTURE_CREATE = imagecreatefromgif($PICTURE_URL); $this -> PICTURE_TYPE = "imagejpeg"; $this -> PICTURE_EXT = "jpg"; break; case 2: $this -> PICTURE_CREATE = imagecreatefromjpeg($PICTURE_URL); $this -> PICTURE_TYPE = "imagegif"; $this -> PICTURE_EXT = "gif"; break; case 3: $this -> PICTURE_CREATE = imagecreatefrompng($PICTURE_URL); $this -> PICTURE_TYPE = "imagepng"; $this -> PICTURE_EXT = "png"; break; } /** * 文字颜色转换16进制转换成10进制 */ preg_match_all("/([0-f]){2,2}/i", $this -> FONT_COLOR, $MATCHES); if(count($MATCHES) == 3){ $this -> RED = hexdec($MATCHES[0][0]); $this -> GREEN = hexdec($MATCHES[0][1]); $this -> BLUE = hexdec($MATCHES[0][2]); } } # end of __construct /** * 将16进制的颜色转换成10进制的(R,G,,B) */ function hex2dec(){ preg_match_all("/([0-f]){2,2}/i", $this -> FONT_COLOR, $MATCHES); if(count($MATCHES) == 3){ $this -> RED = hexdec($MATCHES[0][0]); $this -> GREEN = hexdec($MATCHES[0][1]); $this -> BLUE = hexdec($MATCHES[0][2]); } } // 缩放类型 function zoom_type($ZOOM_TYPE){ $this -> ZOOM = $ZOOM_TYPE; } // 对图片进行缩放,如果不指定高度和宽度就进行缩放 function zoom(){ // 缩放的大小 if($this -> ZOOM == 0){ $this -> ZOOM_WIDTH = $this -> PICTURE_WIDTH * $this -> ZOOM_MULTIPLE; $this -> ZOOM_HEIGHT = $this -> PICTURE_HEIGHT * $this -> ZOOM_MULTIPLE; } // 新建一个真彩图象 $this -> TRUE_COLOR = imagecreatetruecolor($this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT); $WHITE = imagecolorallocate($this -> TRUE_COLOR, 255, 255, 255); imagefilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT, $WHITE); imagecopyresized($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); } # end of zoom // 裁切图片,按坐标或自动 function cut(){ $this -> TRUE_COLOR = imagecreatetruecolor($this -> CUT_WIDTH, $this -> CUT_WIDTH); imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, $this -> CUT_X, $this -> CUT_Y, $this -> CUT_WIDTH, $this -> CUT_HEIGHT); } # end of cut /** * 在图片上放文字或图片 * 水印文字 */ function _mark_text(){ $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); $this -> WORD = mb_convert_encoding($this -> FONT_WORD, 'utf-8', 'gb2312'); /** * 取得使用 TrueType 字体的文本的范围 */ $TEMP = imagettfbbox($this -> FONT_SIZE, 0, $this -> FONT_PATH, $this -> WORD); $WORD_LENGTH = strlen($this -> WORD); $WORD_WIDTH = $TEMP[2] - $TEMP[6]; $WORD_HEIGHT = $TEMP[3] - $TEMP[7]; /** * 文字水印的默认位置为右下角 */ if($this -> WORD_X == ""){ $this -> WORD_X = $this -> PICTURE_WIDTH - $WORD_WIDTH; } if($this -> WORD_Y == ""){ $this -> WORD_Y = $this -> PICTURE_HEIGHT - $WORD_HEIGHT; } imagesettile($this -> TRUE_COLOR, $this -> PICTURE_CREATE); imagefilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, IMG_COLOR_TILED); $TEXT2 = imagecolorallocate($this -> TRUE_COLOR, $this -> RED, $this -> GREEN, $this -> Blue); imagettftext($this -> TRUE_COLOR, $this -> FONT_SIZE, $this -> ANGLE, $this -> WORD_X, $this -> WORD_Y, $TEXT2, $this -> FONT_PATH, $this -> WORD); } /** * 水印图片 */ function _mark_picture(){ /** * 获取水印图片的信息 */ @$SIZE = getimagesize($this -> FORCE_URL); if(!$SIZE){ exit($this -> ERROR['unalviable']); } $FORCE_PICTURE_WIDTH = $SIZE[0]; $FORCE_PICTURE_HEIGHT = $SIZE[1]; // 创建水印图片 switch($SIZE[2]){ case 1: $FORCE_PICTURE_CREATE = imagecreatefromgif($this -> FORCE_URL); $FORCE_PICTURE_TYPE = "gif"; break; case 2: $FORCE_PICTURE_CREATE = imagecreatefromjpeg($this -> FORCE_URL); $FORCE_PICTURE_TYPE = "jpg"; break; case 3: $FORCE_PICTURE_CREATE = imagecreatefrompng($this -> FORCE_URL); $FORCE_PICTURE_TYPE = "png"; break; } /** * 判断水印图片的大小,并生成目标图片的大小,如果水印比图片大,则生成图片大小为水印图片的大小。否则生成的图片大小为原图片大小。 */ $this -> NEW_PICTURE = $this -> PICTURE_CREATE; if($FORCE_PICTURE_WIDTH > $this -> PICTURE_WIDTH){ $CREATE_WIDTH = $FORCE_PICTURE_WIDTH - $this -> FORCE_START_X; }else{ $CREATE_WIDTH = $this -> PICTURE_WIDTH; } if($FORCE_PICTURE_HEIGHT > $this -> PICTURE_HEIGHT){ $CREATE_HEIGHT = $FORCE_PICTURE_HEIGHT - $this -> FORCE_START_Y; }else{ $CREATE_HEIGHT = $this -> PICTURE_HEIGHT; } /** * 创建一个画布 */ $NEW_PICTURE_CREATE = imagecreatetruecolor($CREATE_WIDTH, $CREATE_HEIGHT); $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255); /** * 将背景图拷贝到画布中 */ imagecopy($NEW_PICTURE_CREATE, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); /** * 将目标图片拷贝到背景图片上 */ imagecopy($NEW_PICTURE_CREATE, $FORCE_PICTURE_CREATE, $this -> FORCE_X, $this -> FORCE_Y, $this -> FORCE_START_X, $this -> FORCE_START_Y, $FORCE_PICTURE_WIDTH, $FORCE_PICTURE_HEIGHT); $this -> TRUE_COLOR = $NEW_PICTURE_CREATE; } # end of mark function alpha_(){ $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); $rgb = "#CDCDCD"; $tran_color = "#000000"; for($j = 0;$j PICTURE_HEIGHT-1;$j++){ for ($i = 0;$i PICTURE_WIDTH-1;$i++) { $rgb = imagecolorat($this -> PICTURE_CREATE, $i, $j); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $now_color = imagecolorallocate($this -> PICTURE_CREATE, $r, $g, $b); if ($now_color == $tran_color) { continue; } else { $color = imagecolorallocatealpha($this -> PICTURE_CREATE, $r, $g, $b, $ALPHA); imagesetpixel($this -> PICTURE_CREATE, $ALPHA_X + $i, $ALPHA_Y + $j, $color); } $this -> TRUE_COLOR = $this -> PICTURE_CREATE; } } } /** * 图片旋转: * 沿y轴旋转 */ function turn_y(){ $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); for ($x = 0; $x PICTURE_WIDTH; $x++) { imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, $this -> PICTURE_WIDTH - $x - 1, 0, $x, 0, 1, $this -> PICTURE_HEIGHT); } } /** * 沿X轴旋转 */ function turn_x(){ $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); for ($y = 0; $y PICTURE_HEIGHT; $y++){ imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, $this -> PICTURE_HEIGHT - $y - 1, 0, $y, $this -> PICTURE_WIDTH, 1); } } /** * 任意角度旋转 */ function turn(){ $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); imageCopyResized($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); $WHITE = imagecolorallocate($this -> TRUE_COLOR, 255, 255, 255); $this -> TRUE_COLOR = imagerotate ($this -> TRUE_COLOR, $this -> CIRCUMROTATE, $WHITE); } /** * 图片锐化 */ function sharp(){ $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); $cnt = 0; for ($x = 0; $x PICTURE_WIDTH; $x++){ for ($y = 0; $y PICTURE_HEIGHT; $y++){ $src_clr1 = imagecolorsforindex($this -> TRUE_COLOR, imagecolorat($this -> PICTURE_CREATE, $x-1, $y-1)); $src_clr2 = imagecolorsforindex($this -> TRUE_COLOR, imagecolorat($this -> PICTURE_CREATE, $x, $y)); $r = intval($src_clr2["red"] + $this -> SHARP * ($src_clr2["red"] - $src_clr1["red"])); $g = intval($src_clr2["green"] + $this -> SHARP * ($src_clr2["green"] - $src_clr1["green"])); $b = intval($src_clr2["blue"] + $this -> SHARP * ($src_clr2["blue"] - $src_clr1["blue"])); $r = min(255, max($r, 0)); $g = min(255, max($g, 0)); $b = min(255, max($b, 0)); if (($DST_CLR = imagecolorexact($this -> PICTURE_CREATE, $r, $g, $b)) == -1) $DST_CLR = imagecolorallocate($this -> PICTURE_CREATE, $r, $g, $b); $cnt++; if ($DST_CLR == -1) die("color allocate faile at $x, $y ($cnt)."); imagesetpixel($this -> TRUE_COLOR, $x, $y, $DST_CLR); } } } /** * 将图片反色处理?? */ function return_color(){ /** * 创建一个画布 */ $NEW_PICTURE_CREATE = imagecreate($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255); /** * 将背景图拷贝到画布中 */ imagecopy($NEW_PICTURE_CREATE, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); $this -> TRUE_COLOR = $NEW_PICTURE_CREATE; } /** * 生成目标图片并显示 */ function show(){ // 判断浏览器,若是IE就不发送头 if(isset($_SERVER['HTTP_USER_AGENT'])) { $ua = strtoupper($_SERVER['HTTP_USER_AGENT']); if(!preg_match('/^.*MSIE.*\)$/i', $ua)) { header("Content-type:$this->PICTURE_MIME"); } } $OUT = $this -> PICTURE_TYPE; $OUT($this -> TRUE_COLOR); } /** * 生成目标图片并保存 */ function save_picture(){ // 以 JPEG 格式将图像输出到浏览器或文件 $OUT = $this -> PICTURE_TYPE; if(function_exists($OUT)){ // 判断浏览器,若是IE就不发送头 if(isset($_SERVER['HTTP_USER_AGENT'])) { $ua = strtoupper($_SERVER['HTTP_USER_AGENT']); if(!preg_match('/^.*MSIE.*\)$/i', $ua)) { header("Content-type:$this->PICTURE_MIME"); } } if(!$this -> TRUE_COLOR){ exit($this -> ERROR['unavilable']); }else{ $OUT($this -> TRUE_COLOR, $this -> DEST_URL); $OUT($this -> TRUE_COLOR); } } } /** * 析构函数:释放图片 */ function __destruct(){ /** * 释放图片 */ imagedestroy($this -> TRUE_COLOR); imagedestroy($this -> PICTURE_CREATE); } # end of class } ?>

这就是非常强大的php图片处理类,好好收藏,亲,相信以后一定会派上用场的。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

利用PHP实现图片压缩的方法 利用PHP实现图片压缩的方法 Sep 13, 2023 am 08:42 AM

利用PHP实现图片压缩的方法一、背景介绍在网站或移动应用开发中,经常会遇到需要对图片进行压缩的情况。图片压缩可以有效减小图片的文件大小,提升页面加载速度,同时节省存储空间。本文将介绍使用PHP语言实现图片压缩的方法,并给出具体的代码示例。二、方法介绍PHP提供了多种处理图片的扩展库,如GD、ImageMagick等。其中,GD扩展是PHP内置的对图片进行处理

如何使用PHP调整图片的亮度和对比度 如何使用PHP调整图片的亮度和对比度 Aug 26, 2023 pm 07:04 PM

如何使用PHP调整图片的亮度和对比度亮度和对比度是调整图片视觉效果的重要因素之一。在图像处理中,通过调整亮度可以使图片变得更明亮或更暗,而通过调整对比度可以增强或减弱图片中不同颜色之间的差异。PHP作为一种常用的服务器端脚本语言,提供了丰富的图像处理功能和库。本文将介绍如何使用PHP调整图片的亮度和对比度,并附上代码示例。调整图片亮度调整图片的亮度可以通过改

PHP图片处理快速入门指南:基础操作和常见问题解答 PHP图片处理快速入门指南:基础操作和常见问题解答 Aug 21, 2023 am 10:12 AM

PHP图片处理快速入门指南:基础操作和常见问题解答引言:在Web开发中,图片处理是一个非常常见和重要的任务。无论是在网站开发中用于图像的上传、裁剪、水印等操作,还是在移动应用中用于图像的压缩和处理,都需要对图片进行一些操作。而PHP作为一种流行的服务器端脚本语言,具备强大的图像处理能力。本文将带您快速入门PHP图片处理,包括基础操作和常见问题解答。一、基础操

华为P60是5G手机吗? 华为P60是5G手机吗? Feb 21, 2024 am 08:30 AM

随着5G技术的发展,人们对5G手机的关注度也越来越高。作为中国最知名的通信设备生产商之一,华为一直以来都在领导着5G技术的研发和应用。在2019年,华为推出了多款5G手机,其中包括华为P60系列。那么,华为P60到底是不是一款5G手机呢?现在就让我们来一起探讨一下。首先,我们需要明确一点,华为P60系列手机并不是5G手机。华为P60系列在2019年发布,它们

php如何使用PHP图片处理类库? php如何使用PHP图片处理类库? Jun 02, 2023 pm 05:21 PM

PHP图片处理类库是PHP开发者常用的工具之一,它可以帮助我们快速轻松地对图片进行编辑、剪裁、缩放等操作。本文将介绍如何使用PHP图片处理类库。一、什么是PHP图片处理类库?PHP图片处理类库是为了方便PHP开发者对图片进行快速处理而开发的一组工具包。它可以处理JPG、PNG、GIF和BMP等图片格式,实现图片缩放、旋转、裁剪等多

PHP如何处理图片上传和处理? PHP如何处理图片上传和处理? Jul 02, 2023 pm 03:07 PM

PHP(HypertextPreprocessor)是一种广泛用于服务器端开发的脚本语言,广泛用于网站开发。在网站开发中,处理图片上传是一个常见的需求。本文将介绍PHP如何处理图片上传和处理的方法。首先,图片上传需要使用HTML的表单元素来实现。在HTML中,可以使用来创建一个文件上传控

PHP图片处理技巧大揭秘 PHP图片处理技巧大揭秘 Sep 13, 2023 am 09:30 AM

PHP图片处理技巧大揭秘在现代互联网应用中,图片处理是非常常见的需求之一。无论是图片的压缩、裁剪、添加水印、生成缩略图等等,都需要使用到图片处理技巧。而PHP作为一门优秀的服务器端编程语言,提供了丰富的图像处理功能和扩展库,为我们实现这些需求提供了很好的支持。本文将为大家揭秘一些PHP图片处理的技巧,并提供具体的代码示例。图片压缩图片压缩是优化网站加载速度和

知名爆料人士重申,即将推出的 Apple Watch SE 可能会采用塑料外壳 知名爆料人士重申,即将推出的 Apple Watch SE 可能会采用塑料外壳 Aug 07, 2024 am 09:39 AM

上个月,经验丰富的分析师 Mark Gurman 透露,下一代 Apple Watch SE 最早可能在今年推出,可能会采用“硬质塑料”表壳,而放弃了 Apple Watch 自问世以来一直采用的铝制表壳。

See all articles