php图像操作类(1/3)
代码如下 | 复制代码 |
class image{ //构造 // 保存 |
<!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/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<?php
class upfileclass {
var $upfile, $upfile_name, $upfile_size;
# $upfile 临时文件名 $_files['tmp_name'] ,$upfile_name 文件名 $_files['name'] ,$upfile_size 文件大小$_files['size'];
var $new_upfile_name; # 上传后的文件名称 ;
var $fleth, $fileextent; # 文件扩展名(类型) ;
var $f1, $f2, $f3; # 文件保存路径(多级) upfiles/2008-01/08/;
var $filename; # 文件(带路径) ;
var $filepath; #相对路径用来删除文件;
var $maxsize, $file_type; # 允许上传文件的大小 允许上传文件的类型 ;
var $buildfile,$newfile,$file_width,$file_height,$rate;
function upfileclass($upfile,$upfile_name,$upfile_size){
$this->upfile = $upfile;
$this->upfile_name = $upfile_name;
$this->upfile_size = $upfile_size;
$this->new_upfile_name = $this->createnewfilename($this->upfile_name);
$this->f1 = "upfiles";
$this->f2 = $this->f1."/".date('y')."-".date('m');
$this->f3 = $this->f2."/".date('d');
$this->filename = $this->f3 . "/" . $this->new_upfile_name;
$this->maxsize = 500*1024; # 文件大小 500kb
$this->file_type = "gif/jpg/jpeg/png/bmp"; # 允许上传的文件类型
}
# 创建新文件名 (原文件名)
function createnewfilename($file_name){
$this->fleth = explode(".",$file_name);
$this->fileextent = $this->fleth[(int)count($this->fleth)-1]; # 获取文件后缀;
$tmps教程tr = date('ymd').rand(0,time()) . "." .$this->fileextent; # 创建新文件名;
return $tmpstr;
}
# 检测文件类型是否正确
function chk_fileextent(){
$iwtrue = 0;
$fle = explode("/",$this->file_type);
for($i=0; $i < count($fle); $i++){
if($this->fileextent == $fle[$i]){
$iwtrue = (int) $iwtrue + 1;
}
}
if( $iwtrue == 0 ){
$this->msg("文件不符合 ".$this->file_type." 格式!");
}
}
# 提示错误信息并终止操作
function msg($error){
echo "<script language="网页特效"> ";
echo " alert('".$error."'); ";
echo " window.history.back(); ";
echo "</script> ";
die();
}
# 保存文件
function savefile(){
$this->chk_fileextent();
$this->chk_filesize();
$this->createfolder( "../".$this->f1 );
$this->createfolder( "../".$this->f2 );
$this->createfolder( "../".$this->f3 );
return $this->chk_savefile();
}
# 检测上传结果是否成功
function chk_savefile(){
$copymsg = copy($this->upfile,"../".$this->filename);
if( $copymsg ){
return $this->filename;
}
else{
$this->msg("文件上传失败! 请重新上传! ");
}
}
# 创建文件夹
function createfolder($foldername){
if( !is_dir($foldername) ){
mkdir($foldername,0777);
}
}
# 检测文件大小
function chk_filesize(){
if( $this->upfile_size > $this->maxsize ){
$this->msg("目标文件不能大于". $this->maxsize/1024 ." kb");
}
}
# 删除文件($filepath 文件相对路径)
function deletefile($filepath){
if( !is_file($filepath) ){
return false;
}
else{
$ending = @unlink($filepath);
return $ending;
}
}
/*
函数:生成缩略图
makebuild("/www.111cn.net/a.jpg","news/b.jpg","100");
参数:
echo $buildfile; 原图 带路径
echo $newfile; 生成的缩略图 带路径
echo $file_width; 缩略图宽度值
echo $file_height; 缩略图高度值 (默认为宽度的比例值)
echo $rate; 缩略图象品质;
*/
function makebuild($buildfile,$newfile,$file_width,$file_height=0,$rate=100) {
if(!is_file($buildfile)){
$this->msg("文件 ".$buildfile." 不是一个有效的图形文件! 系统无法生成该文件的缩略图!");
return false;
}
$data = getimagesize($buildfile);
switch($data[2]){
case 1:
$im = @imagecreatefromgif($buildfile);
break;
case 2:
$im = @imagecreatefromjpeg($buildfile);
break;
case 3:
$im = @imagecreatefrompng($buildfile);
break;
}
if(!$im){
return false;
}
else{
$srcw = imagesx($im); # 取得原图宽度;
$srch = imagesy($im); # 取得原图高度;
$dstx = 0;
$dsty = 0;
if($file_height==0){
$file_height = $file_width/$srcw*$srch;
}
if ($srcw*$file_height>$srch*$file_width){
$ffile_height = round($srch*$file_width/$srcw);
$dsty = floor(($file_height-$ffile_height)/2);
$ffile_width = $file_width;
}
else {
$ffile_width = round($srcw*$file_height/$srch);
$dstx = floor(($file_width-$ffile_width)/2);
$ffile_height = $file_height;
}
$ni = imagecreatetruecolor($file_width,$file_height);
$dstx = ($dstx<0)?0:$dstx;
$dsty = ($dstx<0)?0:$dsty;
$dstx = ($dstx>($file_width/2))?floor($file_width/2):$dstx;
$dsty = ($dsty>($file_height/2))?floor($file_height/s):$dsty;
imagecopyresized($ni,$im,$dstx,$dsty,0,0,$ffile_width,$ffile_height,$srcw,$srch);
imagejpeg($ni,$newfile,$rate); # 生成缩略图;
imagedestroy($im); # imagedestroy(resource) 释放image关联的内存
}
}
}
?>
</body>
</html>
*/
代码如下 | 复制代码 |
class cd_barra ?> |
//调用 方法
代码如下 | 复制代码 |
<?php |
代码如下 | 复制代码 |
class upimages { var $annexfolder = "upload";//附件存放点,默认为:annex var $smallfolder = "small";//缩略图存放路径,注:必须是放在 $annexfolder下的子目录,默认为:smallimg var $markfolder = "mark";//水印图片存放处 var $upfiletype = "jpg gif png";//上传的类型,默认为:jpg gif png rar zip var $upfilemax = 1024;//上传大小限制,单位是"kb",默认为:1024kb var $fonttype;//字体 var $maxwidth = 500; //图片最大宽度 var $maxheight = 600; //图片最大高度 function upimages($annexfolder,$smallfolder,$includefolder) { $this->annexfolder = $annexfolder; $this->smallfolder = $smallfolder; $this->fonttype = $includefolder."/04b_08__.ttf"; } function upload($inputname) { $imagename = time();//设定当前时间为图片名称 if(@empty($_files[$inputname]["name"])) die("没有上传图片信息,请确认"); $name = explode(".",$_files[$inputname]["name"]);//将上传前的文件以"."分开取得文件类型 $imgcount = count($name);//获得截取的数量 $imgtype = $name[$imgcount-1];//取得文件的类型 if(strpos($this->upfiletype,$imgtype) === false) die(error("上传文件类型仅支持 ".$this->upfiletype." 不支持 ".$imgtype)); $photo = $imagename.".".$imgtype;//写入数据库教程的文件名 $uploadfile = $this->annexfolder."/".$photo;//上传后的文件名称 $upfileok = move_uploaded_file($_files[$inputname]["tmp_name"],$uploadfile); if($upfileok) { $imgsize = $_files[$inputname]["size"]; $ksize = round($imgsize/1024); if($ksize > ($this->upfilemax*1024)) { @unlink($uploadfile); die(error("上传文件超过 ".$this->upfilemax."kb")); } } else { die(error("上传图片失败,请确认你的上传文件不超过 $upfilemax kb 或上传时间超时")); } return $photo; } function getinfo($photo) { $photo = $this->annexfolder."/".$photo; $imageinfo = getimagesize($photo); $imginfo["width"] = $imageinfo[0]; $imginfo["height"] = $imageinfo[1]; $imginfo["type"] = $imageinfo[2]; $imginfo["name"] = basename($photo); return $imginfo; } function smallimg($photo,$width=128,$height=128) { $imginfo = $this->getinfo($photo); $photo = $this->annexfolder."/".$photo;//获得图片源 $newname = substr($imginfo["name"],0,strrpos($imginfo["name"], "."))."_thumb.jpg";//新图片名称 if($imginfo["type"] == 1) { $img = imagecreatefromgif($photo); } elseif($imginfo["type"] == 2) { $img = imagecreatefromjpeg($photo); } elseif($imginfo["type"] == 3) { $img = imagecreatefrompng($photo); } else { $img = ""; } if(empty($img)) return false; $width = ($width > $imginfo["width"]) ? $imginfo["width"] : $width; $height = ($height > $imginfo["height"]) ? $imginfo["height"] : $height; $srcw = $imginfo["width"]; $srch = $imginfo["height"]; if ($srcw * $width > $srch * $height) { $height = round($srch * $width / $srcw); } else { $width = round($srcw * $height / $srch); } if (function_exists("imagecreatetruecolor")) { $newimg = imagecreatetruecolor($width, $height); imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]); } else { $newimg = imagecreate($width, $height); imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]); } if ($this->tofile) { if (file_exists($this->annexfolder."/".$this->smallfolder."/".$newname)) @unlink($this->annexfolder."/".$this->smallfolder."/".$newname); imagejpeg($newimg,$this->annexfolder."/".$this->smallfolder."/".$newname); return $this->annexfolder."/".$this->smallfolder."/".$newname; } else { imagejpeg($newimg); } imagedestroy($newimg); imagedestroy($img); return $newname; } function watermark($photo,$text) { $imginfo = $this->getinfo($photo); $photo = $this->annexfolder."/".$photo; $newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . "_mark.jpg"; switch ($imginfo["type"]) { case 1: $img = imagecreatefromgif($photo); break; case 2: $img = imagecreatefromjpeg($photo); break; case 3: $img = imagecreatefrompng($photo); break; default: return false; } if (empty($img)) return false; $width = ($this->maxwidth > $imginfo["width"]) ? $imginfo["width"] : $this->maxwidth; $height = ($this->maxheight > $imginfo["height"]) ? $imginfo["height"] : $this->maxheight; $srcw = $imginfo["width"]; $srch = $imginfo["height"]; if ($srcw * $width > $srch * $height) { $height = round($srch * $width / $srcw); } else { $width = round($srcw * $height / $srch); } if (function_exists("imagecreatetruecolor")) { $newimg = imagecreatetruecolor($width, $height); imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]); } else { $newimg = imagecreate($width, $height); imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]); } $white = imagecolorallocate($newimg, 255, 255, 255); $black = imagecolorallocate($newimg, 0, 0, 0); $alpha = imagecolorallocatealpha($newimg, 230, 230, 230, 40); imagefilledrectangle($newimg, 0, $height-26, $width, $height, $alpha); imagefilledrectangle($newimg, 13, $height-20, 15, $height-7, $black); imagettftext($newimg, 4.9, 0, 20, $height-14, $black, $this->fonttype, $text[0]); imagettftext($newimg, 4.9, 0, 20, $height-6, $black, $this->fonttype, $text[1]); if($this->tofile) { if (file_exists($this->annexfolder."/".$this->markfolder."/".$newname)) @unlink($this->annexfolder."/".$this->markfolder."/".$newname); imagejpeg($newimg,$this->annexfolder."/".$this->markfolder."/".$newname); return $this->annexfolder."/".$this->markfolder."/".$newname; } else { imagejpeg($newimg); } imagedestroy($newimg); imagedestroy($img); return $newname; } } |
代码如下 | 复制代码 |
function createimage($data,$twidth,$tspace,$height){ $dataname = array(); $datavalue = array(); $i = 0; $j = 0; $k = 0; $num = sizeof($data); foreach($data as $key => $val){ $dataname[] = $key; $datavalue[] = $val; } $maxnum = max($data); $width = ($twidth + $tspace) * $num + 4;//image's width $im = imagecreate($width + 40,$height+20); $linecolor = imagecolorallocate($im,12,12,12); $bgcolor = imagecolorallocate($im,235,233,233); $tcolor = imagecolorallocate($im,123,200,56); imagefill($im,0,0,$bgcolor); imageline ( $im, 30, 0, 30, $height - 2, $linecolor); imageline ( $im, 30, $height - 2, $width + 30 -2 , $height - 2,$linecolor); while($i < $num){ imagefilledrectangle ( $im, $i * ($tspace+$twidth) + 40, $height - $datavalue[$i], $i * ($tspace+$twidth) + 40 + $twidth, $height - 3, $tcolor); imagestringup ( $im, 4, $i * ($tspace+$twidth) + $twidth/2 + 30, $height - 10, $dataname[$i]."(".$datavalue[$i].")", $linecolor); $i++; } while($j <= (500/10)){ imagestringup ( $im, 4, 2, $height - $j * 10 + 10, $j * 10, $linecolor); $j = $j + 10; } while($k <= (500/10)){ if($k != 0) imageline ( $im, 28, $height - $k * 10, 32 , $height - $k * 10,$linecolor); $k = $k + 10; } imagepng($im); } |
//调用方法:
代码如下 | 复制代码 |
header("content-type:image/png"); $data = array("yahoo" => 120, "google" => 260,"microsoft" => 320,"ibm" => 290,"sun system" => 150,"inter" => 260); createimage($data,38,25,460); |
相关文章
- 以前我们开发大型项目时都会用到svn来同步,因为开发产品的人过多,所以我们会利用软件来管理,今天发有一居然可以利用php来管理svn哦,好了看看吧。 代码如下 ...2016-11-25
- 操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
- 这篇文章介绍了在C#中对config文件的操作,有需要的朋友可以参考一下...2020-06-25
- 这篇文章主要介绍了python自动化办公操作PPT的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-05
nodejs文件操作模块FS(File System)常用函数简明总结
件系统操作相关的函数挺多的。首先可以分为两大类。一类是异步+回调的。 一类是同步的。在这里只对异步的进行整理,同步的只需要在函数名称后面加上Sync即可1. 首先是一类最常规的读写函数,函数名称和形式,应该是起源于C...2014-06-07- 这篇文章主要介绍了C#模拟window操作鼠标的方法,可实现模拟鼠标移动到固定位置后点击右键的功能,涉及鼠标常用事件的操作技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了微信小程序手势操作之单触摸点与多触摸点的相关资料,需要的朋友可以参考下...2017-03-13
- 今天小编就为大家分享一篇python 实现将Numpy数组保存为图像,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
- 这篇文章主要给大家介绍了关于python中字符串最常用的13个处理操作的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-09
- 下面小编就为大家带来一篇js操作XML文件的实现方法兼容IE与FireFox。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-07-01
- 这篇文章主要介绍了C#图像透明度调整的方法,涉及C#操作图像透明度的相关技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C#图像亮度调整的方法,涉及C#操作图像亮度的相关技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C# 模拟浏览器并自动操作的实例代码,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-11-03
- c#对字符串操作的技巧小结,需要的朋友可以参考一下...2020-06-25
- 这篇文章主要介绍了C#操作PowerPoint的方法,涉及C#针对PowerPoint的打开、读取、播放等技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
- 今天小编就为大家分享一篇Python-numpy实现灰度图像的分块和合并方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
- 比较操作是很简单的基础知识,不过Pandas中的比较操作有一些特殊的点,本文介绍的非常详细,对正在学习python的小伙伴们很有帮助.需要的朋友可以参考下...2021-05-20
- 在C#中位操作同C的位操作没有什么区别,位操作的速度相对较快,而且如果熟练的话,处理起来也相对方便,特别是在一些权限等相关的设置中...2020-06-25
- 这篇文章主要介绍了OpenCvSharp实现Mat对象简单的像素操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-17
- 这篇文章主要为大家详细介绍了python opencv实现图像配准与比较,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09