您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php漫笔1-php图片处理

2024/3/24 21:40:32发布42次查看
php随笔1-php图片处理
php图片处理的知识内容
upload_image.php:
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> style type=text/css>.file-box{ position:relative;width:340px} .txt{ height:22px; border:1px solid #cdcdcd; width:200px;border-right:none;} .btn{ background-color:#fff; border:1px solid #cdcdcd;height:26px; width:70px;} .file{ position:absolute; top:0; right:80px; height:24px; filter:alpha(opacity:0);opacity: 0;width:260px } style> script type=text/javascript > //js获取路径 ---成功 function change()
{ var pic=document.getelementbyid('pic'); var file=document.getelementbyid('filefield'); pic.src=file.value; if(file.files){ if(file.files.item(0)){ //src = window.url.createobjecturl(fileobj.files[0]); url=window.url.createobjecturl(file.files.item(0)); document.getelementbyid('textfield').value = pic.src ; pic.src=url; } }  } script> head>body>form action=check_image.php method=post enctype=multipart/form-data> table border=1> tr> td>上传人名称:td> td>input type=text class='txt' name=username />td> tr> tr> td>*上传图片:td> td>div class=file-box>input type='text' name='textfield' id='textfield' class='txt' />input type='button' class='btn' value='浏览...' />br /> input type=file name=uploadfile class=file id=filefield size=28 onchange=change(); /> input type=hidden id=thefilepath name=thefilepath value=> div> td> tr> tr> td colspan=2> small>em> * 可接受的图像格式:gif,jpg、jpeg和png。上传后图片会变为1280x960大小。em>small> td> tr> tr> td>图片标题:td> td>input type=text class='txt' name=caption/>td> tr> tr> td colspan=2 style=text-align:center;> input type=submit name=submit value=upload class='btn' /> td> tr> table> img src= name=pic id=pic /> br /> form>body>html>
check_image.php   集合图片上传,mysql数据库保存,图片放大至1280x960,图片加文字,图片ps处理,图片缩略图,图片打logo水印。功能集合强大。
php //session session_start(); if($_post['submit'] == 'upload') { if($_files['uploadfile']['error'] != upload_err_ok) { switch($_files['uploadfile']['error']) { case upload_err_ini_size: //其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值 die('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值'); break; case upload_err_form_size: //其值为 2,上传文件的大小超过了 html 表单中 max_file_size 选项指定的值 die('上传文件的大小超过了 html 表单中 max_file_size 选项指定的值'); break; case upload_err_partial: //其值为 3,文件只有部分被上传 die('文件只有部分被上传'); break; case upload_err_no_file: //其值为 4,没有文件被上传 die('没有文件被上传'); break; case upload_err_no_tmp_dir: //其值为 6,找不到临时文件夹 die('找不到临时文件夹'); break; case upload_err_cant_write: //其值为 7,文件写入失败 die('文件写入失败'); break; case upload_err_extension: //其他异常 die('其他异常'); break; } } //判断图片的后缀 switch($type) { case imagetype_gif: $ext = '.gif'; break; case imagetype_jpeg: $ext = '.jpg'; break; case imagetype_png: $ext = '.png'; break; default : die('您上传的文件不支持的文件类型。'); } //关键位置-取出文件后缀 $_session['ext'] = $ext; } /* //改变图像的大小为1280x960的方法 function resizepic($imgsrc,$imagename,$toimgwidth,$toimgheigth) { //$imgsrc:图片路径 $imgname: 图片保存的名称 $toimgwidth: 图片最终的宽度 $toimgheigth: 图片最终的高度 $arr = getimagesize($imgsrc); $imgwidth = $toimgwidth; $imgheigth = $toimgheigth; if($_session['ext'] == '.jpg') { header('content-type:image/jpeg'); $imgsrc = imagecreatefromjpeg($imgsrc); } if($_session['ext'] == '.png') { header('content-type:image/png'); $imgsrc = imagecreatefrompng($imgsrc); } if($_session['ext'] == '.gif') { header('content-type:image/gif'); $imgsrc = imagecreatefromgif($imgsrc); } $image = imagecreatetruecolor($imgwidth, $imgheigth); imagecopyresampled($image,$imgsrc,0,0,0,0,$imgwidth,$imgheigth,$arr[0],$arr[1]); if($_session['ext'] == '.jpg') { imagejpeg($image,$dir.'/'.$imagename); } if($_session['ext'] == '.png') { imagepng($image,$dir.'/'.$imagename); } if($_session['ext'] == '.gif') { imagegif($image,$dir.'/'.$imagename); } //销毁由url生成的图片 imagedestroy($image); } */ //创建moviesite数据库--mysql $coon = mysql_connect(localhost,root,lifu) or die('不能连接到数据库'); if (!$coon) { die('无法连接: ' . mysql_error()); } if (mysql_query(create database if not exists moviesite,$coon)) //如果没有就创建数据库 { echo 数据库创建成功
; } else { echo 数据库创建错误: . mysql_error(); } // 创建images表 mysql_select_db(moviesite, $coon); //如果没有就创建表 $sql = create table if not exists images ( image_id int not null auto_increment, primary key(image_id), image_caption varchar(15), image_username varchar(15), image_filename varchar(15), image_date datetime ); mysql_query($sql,$coon); mysql_close($coon); //修改图片效果 $db = mysql_connect('localhost','root','lifu') or die('不能连接到数据库'); mysql_select_db('moviesite',$db) or die(mysql_error($db)); //上传文件的路径 $dir = 'd:\phpstudy\www\loadimages\images'; //缩略图的路径 $thumbdir = 'd:\phpstudy\www\loadimages\images\thumbs'; //设置环境变量 putenv('gdfontpath='.'c:\windows\fonts'); $font = c:\windows\fonts\arial.ttf; //upload_image.php页面传递过来的参数,如果是上传图片 if($_post['submit'] == 'upload') { /*代码重复 if($_files['uploadfile']['error'] != upload_err_ok) { switch($_files['uploadfile']['error']) { case upload_err_ini_size: //其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值 die('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值'); break; case upload_err_form_size: //其值为 2,上传文件的大小超过了 html 表单中 max_file_size 选项指定的值 die('上传文件的大小超过了 html 表单中 max_file_size 选项指定的值'); break; case upload_err_partial: //其值为 3,文件只有部分被上传 die('文件只有部分被上传'); break; case upload_err_no_file: //其值为 4,没有文件被上传 die('没有文件被上传'); break; case upload_err_no_tmp_dir: //其值为 6,找不到临时文件夹 die('找不到临时文件夹'); break; case upload_err_cant_write: //其值为 7,文件写入失败 die('文件写入失败'); break; case upload_err_extension: //其他异常 die('其他异常'); break; } } */ $image_caption = $_post['caption']; $image_username = $_post['username']; $image_date = date('y-m-d'); /*getimagesize方法返回一个数组, $width : 索引 0 包含图像宽度的像素值, $height : 索引 1 包含图像高度的像素值, $type : 索引 2 是图像类型的标记: = gif,2 = jpg, 3 = png, 4 = swf, 5 = psd, 6 = bmp, = tiff(intel byte order),8 = tiff(motorola byte order), = jpc,10 = jp2,11 = jpx,12 = jb2,13 = swc,14 = iff,15 = wbmp,16 = xbm, $attr : 索引 3 是文本字符串,内容为“height=yyy width=xxx”,可直接用于 img 标记 */ list($width,$height,$type,$attr) = getimagesize($_files['uploadfile']['tmp_name']); //imagecreatefromgxxx方法从一个url路径中创建一个新的图片 switch($type) { case imagetype_gif: $image = imagecreatefromgif($_files['uploadfile']['tmp_name']) or die('你上传的文件是不支持的文件类型'); $ext = '.gif'; $array = getimagesize($_files['uploadfile']['tmp_name']); break; case imagetype_jpeg: $image = imagecreatefromjpeg($_files['uploadfile']['tmp_name']) or die('你上传的文件是不支持的文件类型'); $ext = '.jpg'; $array = getimagesize($_files['uploadfile']['tmp_name']); break; case imagetype_png: $image = imagecreatefrompng($_files['uploadfile']['tmp_name']) or die('你上传的文件是不支持的文件类型'); $ext = '.png'; $array = getimagesize($_files['uploadfile']['tmp_name']); break; default : die('您上传的文件不支持的文件类型。'); } //将图片改为1280x960 //新建一个指定大小的真彩色图像big $big = imagecreatetruecolor(1280, 960); //重采样拷贝部分图像并调整大小为1280x960 imagecopyresampled($big, $image, 0, 0, 0, 0, 1280, 960, $array[0], $array[1]); $query = 'insert into images(image_caption,image_username,image_date) values ('.$image_caption.','.$image_username.',now())'; mysql_query($query , $db) or die(mysql_error($db)); $last_id = mysql_insert_id(); //用写入的id作为图片的名字,避免同名的文件存放在同一目录中 $imagename = $last_id.$ext; $image_id = $last_id; $query = 'update images set image_filename='.$imagename.' where image_id='.$last_id; mysql_query($query , $db) or die(mysql_error($db)); //有url指定的图片创建图片并保存到指定目录 switch($type) { case imagetype_gif: imagegif($big,$dir.'/'.$imagename); break; case imagetype_jpeg: imagejpeg($big,$dir.'/'.$imagename); break; case imagetype_png: imagepng($big,$dir.'/'.$imagename); break; } //销毁由url生成的图片 imagedestroy($big); imagedestroy($image); } else //如果图片已经上传,则从数据库中取图片名字 { $query = 'select image_id,image_caption,image_username,image_date from images where image_id='.$_post['id']; $result = mysql_query($query,$db) or die(mysql_error($db)); //将结果集转换成关联数据,再将关联数组的键名当作变量名,值作为变量的值。 extract(mysql_fetch_assoc($result)); list($width,$height,$type,$attr) = getimagesize($dir.'/'.$image_id.$_session['ext']); } //如果是保存图片 if($_post['submit'] == 'save') { if(isset($_post['id']) && ctype_digit($_post['id']) && file_exists($dir.'/'.$_post['id'].$_session['ext'])) { if($_session['ext'] == '.jpg') { $image = imagecreatefromjpeg($dir.'/'.$_post['id'].$_session['ext']); } if($_session['ext'] == '.png') { $image = imagecreatefrompng($dir.'/'.$_post['id'].$_session['ext']); } if($_session['ext'] == '.gif') { $image = imagecreatefromgif($dir.'/'.$_post['id'].$_session['ext']); } } else { die('指定的无效图像'); } $effect = (isset($_post['effect'])) ? $_post['effect'] : -1; switch($effect) { case img_filter_negate: imagefilter($image , img_filter_negate); //将图像中所有颜色反转 break; case img_filter_grayscale: imagefilter($image , img_filter_grayscale); //将图像转换为灰度的 break; case img_filter_emboss: imagefilter($image , img_filter_emboss); //使图像浮雕化 break; case img_filter_gaussian_blur: imagefilter($image , img_filter_gaussian_blur); //用高斯算法模糊图像 break; } //图片指定位置打印文字 if(isset($_post['emb_caption'])) { /* array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text ) $image 图像源 $size 字体大小 $angle 逆时针旋转角度 $fontfile 字体类型 $text 文本内容 */ imagettftext($image , 12 , 0 , 20 , 20 , 0 , $font , $image_caption); } //图片指定位置打logo if(isset($_post['emb_logo'])) { //获取水印图片的尺寸并创建水印 list($wmk_width , $wmk_height) = getimagesize('images/logo.png'); //$x = ($width-$wmk_width) / 2; //$y = ($height-$wmk_height)/2; $x = $width-$wmk_width; $y = $height-$wmk_height; $wmk = imagecreatefrompng('images/logo.png'); //把水印图片和原图片合并在一起 /* 将 wmk 图像中坐标从 0,0 开始,宽度为 $wmk_width,高度为 $wmk_height 的一部分拷贝到 image 图像中坐标为 $x 和 $y 的位置上。 两图像将根据 pct=20 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。 */ imagecopymerge($image , $wmk , $x , $y , 0 , 0 , $wmk_width , $wmk_height , 20); //清除水印图片 imagedestroy($wmk); } //缩略图大小 $thumb_width = $width * 0.10; $thumb_height = $height * 0.10; //创建一个缩略图 $thumb = imagecreatetruecolor($thumb_width, $thumb_height); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); //保存缩略图 if($_session['ext'] == '.jpg') { imagejpeg($thumb, $thumbdir . '/' . $_post['id'] . $_session['ext'], 100); } if($_session['ext'] == '.png') { imagepng($thumb, $thumbdir . '/' . $_post['id'] . $_session['ext']); } if($_session['ext'] == '.gif') { imagegif($thumb, $thumbdir . '/' . $_post['id'] . $_session['ext']); } echo 修改好的缩略图:%24_post%5b id . style=max-width:90%>$_session['ext'].' alt='' />
; //清除缩略图 imagedestroy($thumb); //保存原图 if($_session['ext'] == '.jpg') { imagejpeg($image , $dir.'/'.$_post['id'].$_session['ext'] , 100); } if($_session['ext'] == '.png') { //压缩等级必须是0-9的png文件 imagepng($image , $dir.'/'.$_post['id'].$_session['ext']); } if($_session['ext'] == '.gif') { imagegif($image , $dir.'/'.$_post['id'].$_session['ext']); } echo 修改好的大图:%24_post%5b id . style=max-width:90%>$_session['ext'].' alt='' />
; ?> 你的图片在这!你的图片被保存! $_post['id'];?>.jpg alt= /> php } else { ?> 你的图片在这!这里是你上传到服务器上的图片:
php if($_post['submit'] == 'upload') { $imagename = 'images/'.$image_id.$_session['ext']; } else { $imagename = 'image_effect.php?id='.$image_id.'&e='.$_post['effect']; if(isset($_post['emb_caption'])) { $imagename .= '&capt='.urlencode($image_caption); } if(isset($_post['emb_logo'])) { $imagename .= '&logo=1'; } } ?> $imagename;?> style=float:left; alt= /> image save as:
height:
widht:
upload date:
你可以从下面的选项列表中对你的图像应用一个特殊的效果。注:使用任何一个过滤器,保存一个图像 可以撤消
保持原状php echo '; if(isset($_post['effect']) && $_post['effect'] == img_filter_grayscale) { echo 'selected=selected'; } echo ' >灰度图像'; echo '; if(isset($_post['effect']) && $_post['effect'] == img_filter_gaussian_blur) { echo ' selected=selected'; } echo '>高斯算法模糊图像'; echo '; if(isset($_post['effect']) && $_post['effect'] == img_filter_emboss) { echo 'selected=selected'; } echo '>图像浮雕化'; echo '; if(isset($_post['effect']) && $_post['effect'] == img_filter_negate) { echo 'selected=selected'; } echo '>图像中所有颜色反转'; ?>
php echo '; if(isset($_post['emb_caption'])) { echo ' checked=checked'; } echo ' />是否嵌入在图像的标题?'; echo '
'; //添加水印选项 echo '; if(isset($_post['emb_logo'])) { echo 'checked=checked'

该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product