php图片处理类(生成缩略图,增加水印,获取图片信息)
本文章提供这款图片处理类,他可以做的事情有把图片生成缩略图,可能给图片增加水印以及获取图片信息,算是比较实用代码又简洁的函数*/
代码如下 | 复制代码 |
class image function __construct() function image() function watermark($image,$pos=9,$watermarkimg='images/watermark.gif',$pct=65,$text='',$w_font=5,$w_color='#ff0000') function info($image) |
在php中给图片增加水印有imagecreatefromjpeg imagecreatefrompng imagecopymerge imagejpeg就成了,只要你设置原图与水印图片就成了,下面看实例。
*/
代码如下 | 复制代码 |
header("content-type: image/jpeg"); $filename='temp/www.111cn.net/111cn.net.jpg'; $im=imagecreatefromjpeg($filename); $s=imagecreatefrompng('mb.111cn.net/pic/water_template.png'); imagecopymerge($im,$s,0,200,0,0,365,27,20); imagejpeg($im); |
本文章提供一款php教程生成验证码详细教程,前面是讲关于生成验证核心代码,后面是一款生成与调用的方法。
代码如下 | 复制代码 |
<?php $numimage = imagecreate($imagewidth,$imageheight); for($i=0;$i<200;$i++){ |
来看一款生成验证码实例
代码如下 | 复制代码 |
<?php //生成验证码图片 session_start(); header("content-type: image/png"); srand((double)microtime()*1000000); $roundnum=rand(1000,9999); //把随机数存入session以便以后用 $_session["sessionround"]=$roundnum; $im = imagecreate(58,28); $red = imagecolorallocate($im, 255,0,0); $blue = imagecolorallocate($im, 0,255,0); //局域填充,相当于背景 imagefill($im,68,30,$red); //将四位整数验证码绘入图片 imagestring($im, 5, 10, 8, $roundnum, $blue); for($i=0;$i<50;$i++) //加入干扰象素 { imagesetpixel($im, rand()%70 , rand()%30 , $black); } imagepng($im); imagedestroy($im); ?> |
html调用 方法
代码如下 | 复制代码 |
<td>验证码</td><td width="20%"> <input name="validt" type="text" size="10" maxlength="4"/></td><td colspan="3" width="80%"> <img src="/www.111cn.net/validitpicture.php" name="validitpic" align="absmiddle" /></td> |
本文章提供一款php教程中文汉字验证码生成程序,如果在图片片生成汉字,需要font文件和imagettftext函数,用到的时候大家再网上搜索吧。你要产生随机数,那有mt_rand函数;你还要用到session保存这个随机数;如果需要转成utf-8,需要iconv函数。
<?php
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;
}
}
$newfile = upload_dir."/icons/".md5($_session['user']->email).".jpg";//上传文件保存的目录
$image = new simpleimage();
$image->load($_files['icons']['tmp_name']);//上传的临时文件名
$image->resizetowidth(80);设置宽度
$image->save($newfile);
?>
imagemagic官方去除图片背景的命令行模式:
代码如下 | 复制代码 |
convert -size 140x80 xc:none -fill grey -gravity northwest -draw "text 10,10 'copyright'" -gravity southeast -draw "text 5,15 'copyright'" miff:- | composite -tile - logo.jpg wmark_text_tiled.jpg |
imagick代码:
代码如下 | 复制代码 |
<?php $image = new imagick('logo.jpg'); $im = new imagick(); $im->newimage( 140, 80, new imagickpixel( "none" ) ); $draw = new imagickdraw(); $draw->setfillcolor(new imagickpixel( "grey" )); $draw->setgravity(imagick::gravity_northwest); $draw->annotation(10,10 ,'copyright'); $draw->setgravity(imagick::gravity_southeast); $draw->annotation(5,15 ,'copyright'); $im->drawimage( $draw); $image = $image->textureimage($im); $image->compositeimage($image,imagick::composite_copy,0,0); header( "content-type: image/{$image->getimageformat()}" ); $image->writeimage('wmark_text_tiled.jpg'); $image->clear(); $image->destroy(); ?> |
如果你的机型还没装php_imagick就下载吧,下载地址如下
http://pecl.php.net/package/imagick
相关文章
- 这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
- 这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
- 这篇文章主要介绍了C#实现为一张大尺寸图片创建缩略图的方法,涉及C#创建缩略图的相关图片操作技巧,需要的朋友可以参考下...2020-06-25
- if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25
- 这篇文章主要给大家介绍了在CentOS系统下编译安装nginx及配置缩略图插件的方法教程,文中给出了详细的安装步骤,对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。...2017-07-06
- 今天写了一个上传多张图片并生成缩略图的小程序。当然因为是菜鸟,所以写的一般。但还是学到了不少东西。现在上代码。...2021-09-22
- 这篇文章主要介绍了Nginx配合php实现生成实时缩略图功能,这在一些特殊场合可能会要用到,需要的朋友可以参考下...2016-01-27
- 对于生成缩略图一般做法是通过后端语言php等来生成,但是为了给服务器减压,我们或许可以从前端来着手,先生成好不同尺寸的缩略图,传给后端,而后端只需要将前端传过来的图片进行存储就好了...2020-10-03
- 这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。 代码如下 复制代码 ...2016-11-25
- 生成jpg缩略图字节,本人的小软件中需要用到的功能,所以自己做了一个函数,和大家分享 为什么要生成字节而不是文件,这是为了方便后续处理啦^_^...2020-06-25
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1...2016-11-25
- 这篇文章主要介绍了C#获取视频某一帧的缩略图的方法,涉及执行CMD命令及针对视频文件操作的技巧,具有一定的实用价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了python批量提取图片信息并保存的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-06
- 缩略图如果是图片我们直接使用php gD库就可实现了,本文章要介绍的是Imagick把pdf生成png缩略图方法,这里我们要利用一个插件了,下面我来给大家演示一个实例。 php_im...2016-11-25
- 这篇文章主要介绍了ASP.NET创建动态缩略图的方法,实例分析了asp.net动态操作图片的相关技巧,需要的朋友可以参考下...2021-09-22
- 这款图片上传代码可以把上传的图片增加水印,生成小图片,同时还可以生成文字水印。 代码如下 复制代码 class upimages { var $annexf...2016-11-25
- 代码如下 复制代码 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional....2016-11-25
- 使用.net的方法GetThumbnailImage生成的缩略图失真严重,这里推荐一种不失真生成缩略图的方法...2021-09-22
- 等比例生成缩略图的php程序 这个程序很实现,但只是用来进来等比例生成缩略图哦,你要把文件上传到服务器,然后再由此函数来操作,有需要的朋友参考一下。 等比例生成缩...2016-11-25
- 今天帮助一个客户做一上企业网站,发现企业网站做好之后它准备了几百张图片让我上传,这个对于我来讲非常的不想做了,但后来发现可以直接使用程序读取目录然后保存到mysql...2016-11-25