php常用图片处理类
代码如下 | 复制代码 |
<?php /*已知问题:1.在图片缩放功能中,使用imagecreatetruecolor函数创建画布,并使用透明处理算法,但PNG格式的图片无法透明。用imagecreate函数创建画布可以解决这个问题,但是缩放出来的图片色数太少了 * * *type值: * (1):代表使用图片缩放功能,此时,$value1代表缩放后图片的宽度,$value2代表缩放后图片的高度 * (2):代表使用图片裁剪功能,此时,$value1代表裁剪开始点的坐标,例:从原点开始即是“0,0”前面是x轴后面是y轴,中间用,分隔,$value2代表裁剪的宽度和高度,同样也是“20,20”的形式使用 * (3):代表使用加图片水印功能,此时,$value1代表水印图片的文件名,$value2代表水印在图片中的位置,有10值个可以选,1代表左上,2代表左中,3代表左右,4代表中左,5代表中中,6代表中右,7代表下做,8代表下中,9代表下右,0代表随机位置 * */ class image{ private $types; //使用的功能编号,1为图片缩放功能 2为图片裁剪功能 3,为图片加图片水印功能 private $imgtype;//图片的格式 private $image; //图片资源 private $width;//图片宽度 private $height;//图片高度 private $value1;//根据所传type值的不同,$value1分别代表不同的值 private $value2;//根据所传type值的不同,$value2分别代表不同的值 private $endaddress;//输出后的地址+文件名 function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){ $this->types=$types; $this->image=$this->imagesources($imageaddress); $this->width=$this->imagesizex(); $this->height=$this->imagesizey(); $this->value1=$value1; $this->value2=$value2; $this->endaddress=$endaddress; } function outimage(){ //根据传入type值的不同,输出不同的功能 switch($this->types){ case 1: $this->scaling(); break; case 2: $this->clipping(); break; case 3: $this->imagewater(); break; default: return false; } } private function imagewater(){ //加图片水印功能 //用函数获取水印文件的长和宽 $imagearrs=$this->getimagearr($this->value1); //调用函数计算出水印加载的位置 $positionarr=$this->position($this->value2, $imagearrs[0], $imagearrs[1]); //加水印 imagecopy($this->image, $this->imagesources($this->value1), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]); //调用输出方法保存 $this->output($this->image); } private function clipping(){ //图片裁剪功能 //将传进来的值分别赋给变量 list($src_x, $src_y)=explode(",", $this->value1); list($dst_w, $dst_h)=explode(",", $this->value2); if($this->width < $src_x+$dst_w || $this->height < $src_y+$dst_h){ //这个判断就是限制不能截取到图片外面去 return false; } //创建新的画布资源 $newimg=imagecreatetruecolor($dst_w, $dst_h); //进行裁剪 imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h); //调用输出方法保存 $this->output($newimg); } private function scaling(){ //图片缩放功能 //获取等比缩放的宽和高 $this-> proimagesize(); //根据参数进行缩放,并调用输出函数保存处理后的文件 $this->output($this->imagescaling()); } private function imagesources($imgad){ //获取图片类型并打开图像资源 $imagearray=$this->getimagearr($imgad); switch($imagearray[2]){ case 1://gif $this->imgtype=1; $img=imagecreatefromgif($imgad); break; case 2://jpeg $this->imgtype=2; $img=imagecreatefromjpeg($imgad); break; case 3://png $this->imgtype=3; $img=imagecreatefrompng($imgad); break; default: return false; } return $img; } private function imagesizex(){ //获得图片宽度 return imagesx($this->image); } private function imagesizey(){ //获取图片高度 return imagesy($this->image); } private function proimagesize(){ //计算等比缩放的图片的宽和高 if($this->value1 && ($this->width < $this->height)) { //等比缩放算法 $this->value1=round(($this->value2/ $this->height)*$this->width); }else{ $this->value2=round(($this->value1/ $this->width) * $this->height); } } private function imagescaling(){//图像缩放功能,返回处理后的图像资源 $newimg=imagecreatetruecolor($this->value1, $this->value2); $tran=imagecolortransparent($this->image);//处理透明算法 if($tran >= 0 && $tran < imagecolorstotal($this->image)){ $tranarr=imagecolorsforindex($this->image, $tran); $newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']); imagefill($newimg, 0, 0, $newcolor); imagecolortransparent($newimg, $newcolor); } imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this->height); return $newimg; } private function output($image){//输出图像 switch($this->imgtype){ case 1: imagegif($image, $this->endaddress); break; case 2: imagejpeg($image, $this->endaddress); break; case 3: imagepng($image, $this->endaddress); break; default: return false; } } private function getimagearr($imagesou){//返回图像属性数组方法 return getimagesize($imagesou); } private function position($num, $width, $height){//根据传入的数字返回一个位置的坐标,$width和$height分别代表插入图像的宽和高 switch($num){ case 1: $positionarr[0]=0; $positionarr[1]=0; break; case 2: $positionarr[0]=($this->width-$width)/2; $positionarr[1]=0; break; case 3: $positionarr[0]=$this->width-$width; $positionarr[1]=0; break; case 4: $positionarr[0]=0; $positionarr[1]=($this->height-$height)/2; break; case 5: $positionarr[0]=($this->width-$width)/2; $positionarr[1]=($this->height-$height)/2; break; case 6: $positionarr[0]=$this->width-$width; $positionarr[1]=($this->height-$height)/2; break; case 7: $positionarr[0]=0; $positionarr[1]=$this->height-$height; break; case 8: $positionarr[0]=($this->width-$width)/2; $positionarr[1]=$this->height-$height; break; case 9: $positionarr[0]=$this->width-$width; $positionarr[1]=$this->height-$height; break; case 0: $positionarr[0]=rand(0, $this->width-$width); $positionarr[1]=rand(0, $this->height-$height); break; } return $positionarr; } function __destruct(){ imagedestroy($this->image); } } ?> |
代码如下 | 复制代码 |
header("Content-type:image/png"); |
获取远程验证码到本地
代码如下 | 复制代码 |
header("Content-type:image/png"); |
要注意的内容:
1. 邮件的字符集设置, $mail->CharSet = "GB2312"; // 这里指定字符集!在这里我只指定为GB2312因为这样Outlook能正常显示邮件主题,我尝试过设为utf-8但在Outlook下显示乱码。
2. 如果是发送html格式的邮件,那么记得也指定<meta ... charset=GB2312">
3. 如果你想用它来群发邮件的话,记得修改包含文件函数,如:
require("phpmailer/class.phpmailer.php");
改为
require_once("phpmailer/class.phpmailer.php");
否则的话会产生类的重定义。
代码如下 | 复制代码 |
<?php $mail->CharSet = "GB2312"; // 这里指定字符集! |
需要下载PHPMailer文件包phpmailer-1.73.tar.gz 来自开源社区: http://phpmailer.sourceforge.net/
从国外网站找到的一款php生成缩略图代码,有需要的朋友可以参考一下。代码如下 | 复制代码 |
<?php /* * File: SimpleImage.php * Author: Simon Jarvis * Copyright: 2006 Simon Jarvis * Date: 08/11/06 * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details: * http://www.gnu.org/licenses/gpl.html * */ class SimpleImage { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } ?> |
Usage
Save the above file as SimpleImage.php and take a look at the following examples of how to use the script.
The first example below will load a file named picture.jpg resize it to 250 pixels wide and 400 pixels high and resave it as picture2.jpg
代码如下 | 复制代码 |
<?php |
If you want to resize to a specifed width but keep the dimensions ratio the same then the script can work out the required height for you, just use the resizeToWidth function
代码如下 | 复制代码 |
<?php |
代码如下 | 复制代码 |
<form action="uploads.php" method="post" enctype="multipart/form-data"> uploads.php文件 <?php }
|
相关文章
- 这篇文章主要介绍了Windows批量搜索并复制/剪切文件的批处理程序实例,需要的朋友可以参考下...2020-06-30
- 这篇文章主要介绍了C# 10个常用特性,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-09
BAT批处理判断服务是否正常运行的方法(批处理命令综合应用)
批处理就是对某对象进行批量的处理,通常被认为是一种简化的脚本语言,它应用于DOS和Windows系统中。这篇文章主要介绍了BAT批处理判断服务是否正常运行(批处理命令综合应用),需要的朋友可以参考下...2020-06-30- file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
- 这篇文章主要介绍了C#多线程中的异常处理操作,涉及C#多线程及异常的捕获、处理等相关操作技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了postgresql 中的时间处理小技巧(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 这篇文章主要介绍了Python同时处理多个异常的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-29
C#异常处理中try和catch语句及finally语句的用法示例
这篇文章主要介绍了C#异常处理中try和catch语句及finally语句的用法示例,finally语句的使用涉及到了C#的垃圾回收特性,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了python如何用moviepy对视频进行简单的处理,帮助大家更好的利用python处理视频,感兴趣的朋友可以了解下...2021-03-11
- 这篇文章介绍了C#异常处理,有需要的朋友可以参考一下...2020-06-25
- 这篇文章主要给大家介绍了关于sql server日志处理不当造成的隐患的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用sql server具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-07-11
- Spring MVC是Spring系列框架中使用频率最高的部分。不管是Spring Boot还是传统的Spring项目,只要是Web项目都会使用到Spring MVC部分。因此程序员一定要熟练掌握MVC部分。本篇博客简要分析Spring MVC处理一个请求的流程。...2021-02-06
- 这篇文章主要介绍了go语言中的Carbon库时间处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-05
- C++ 提供了异常机制,让我们能够捕获运行时错误,本文就详细的介绍了C++异常处理入门,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
C#事务处理(Execute Transaction)实例解析
这篇文章主要介绍了C#事务处理(Execute Transaction)实例解析,对于理解和学习事务处理有一定的帮助,需要的朋友可以参考下...2020-06-25- 本文章来给各同学总结了一些常用的图像处理函数,包括有缩放、剪裁、缩放、翻转、旋转、透明、锐化功能,大家可参考参考。 注意事项:如果要使用php gd处理我们需要...2016-11-25
Python编程OpenCV和Numpy图像处理库实现图片去水印
这篇文章主要介绍了Python编程中如何实现图片去水印本文采用了OpenCV和Numpy的图像处理的方法来实现,文中附含详细示例代码,有需要的朋友可以借鉴参考下...2021-09-26javascript表单处理具体实现代码(表单、链接、按钮)
这篇文章主要介绍了javascript表单处理具体实现代码,包括各种表单、链接、按钮控件介绍,感兴趣的朋友可以参考一下...2016-05-09- JS与HTML之间的交互通过事件实现。事件就是文档或浏览器窗口中发生的一些特定的交互瞬间。可以使用监听器(或处理程序)来预定事件,以便事件发生时执行相应的代码。本文将介绍JS事件相关的基础知识。...2017-04-03
- C++中处理异常的过程是这样的:在执行程序发生异常,可以不在本函数中处理,而是抛出一个错误信息,把它传递给上一级的函数来解决,上一级解决不了,再传给其上一级,由其上一级处理...2020-04-25