这篇是关于修改图片的效果,主要还是用到php中的gd库中的函数,没想到php还有这凶残能力,出乎我的预料。 先看代码upload_image.php,主要是一个上传控件,用来选择图片 html head title / title style type =text/css / style / head body form action =
这篇是关于修改图片的效果,主要还是用到php中的gd库中的函数,没想到php还有这凶残能力,出乎我的预料。
先看代码upload_image.php,主要是一个上传控件,用来选择图片
html> head> title>title> style type=text/css>style> head> body> form action=check_image.php method=post enctype=multipart/form-data> table> tr> td>your usernametd> td>input type=text name=username />td> tr> tr> td>upload image*td> td>input type=file name=uploadfile/>td> tr> tr> td colspan=2> small>em> * acceptable image formats include: gif, jpg/jpeg and png.em>small> td> tr> tr> td>image captiontd> td>input type=text name=caption/>td> tr> tr> td colspan=2 style=text-align:center;> input type=submit name=submit value=upload /> td> tr> table> form> body>html>
然后是上传和处理图片的逻辑check_image.php
php //修改图片效果$db = mysql_connect('localhost','root','ctrip07185419') or die('can not connect to database');mysql_select_db('moviesite',$db) or die(mysql_error($db));//上传文件的路径$dir = 'd:\serious\phpdev\test\images';//upload_image.php页面传递过来的参数,如果是上传图片if($_post['submit'] == 'upload'){ if($_files['uploadfile']['error'] != upload_err_ok) { switch($_files['uploadfiel']['error']) { case upload_err_ini_size: die('the uploaded file exceeds the upload_max_filesize directive'); break; case upload_err_form_size: die('the upload file exceeds the max_file_size directive that was specified in the html form'); break; case upload_err_partial: die('the uploaded file was only partially uploaded'); break; case upload_err_no_file: die('no file was uploaded'); break; case upload_err_no_tmp_dir: die('the server is missing a temporary folder'); break; case upload_err_cant_write: die('the server fail to write the uploaded file to the disk'); break; case upload_err_extension: die('the upload stopped by extension'); break; } } $image_caption = $_post['caption']; $image_username = $_post['username']; $image_date = date('y-m-d'); list($width,$height,$type,$attr) = getimagesize($_files['uploadfile']['tmp_name']); $error = 'the file you upload is not a supported filetype'; switch($type) { case imagetype_gif: $image = imagecreatefromgif($_files['uploadfile']['tmp_name']) or die($error); break; case imagetype_jpeg: $image = imagecreatefromjpeg($_files['uploadfile']['tmp_name']) or die($error); break; case imagetype_png: $image = imagecreatefrompng($_files['uploadfile']['tmp_name']) or die($error); break; default: break; } $query = 'insert into images(image_caption,image_username,image_date) values('.$image_caption.' , '.$image_username.','.$image_date.')'; $result = mysql_query($query,$db) or die(mysql_error($db)); $last_id = mysql_insert_id(); // $imagename = $last_id.'.jpg'; // imagejpeg($image,$dir.'/'.$imagename); // imagedestroy($image); $image_id = $last_id; imagejpeg($image , $dir.'/'.$image_id.'.jpg'); 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.'.jpg');}//如果是保存图片if($_post['submit'] == 'save'){ if(isset($_post['id']) && ctype_digit($_post['id']) && file_exists($dir.'/'.$_post['id'].'.jpg')) { $image = imagecreatefromjpeg($dir.'/'.$_post['id'].'.jpg'); } else { die('invalid image specified'); } $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; } imagejpeg($image , $dir.'/'.$_post['id'].'.jpg' , 100); ?> here is your pic!your image has been saved! %24_post%5b'id'%5d;?>.jpg alt=>php }else{?> here is your pic!so how does it feel to be famous? here is the picture you just uploaded to your servers:
php if($_post['submit'] == 'upload') { $imagename = 'images/'.$image_id.'.jpg'; } else { $imagename = 'image_effect.php?id='.$image_id.'&e='.$_post['effect']; } ?> %24imagename;?> style=max-width:90% alt=>
image save as:
height:
widht:
upload date:
you may apply a special effect to your image from the list of option below. note:saving an image with any of the filters applied can be undone
nonephp echo '; if(isset($_post['effect']) && $_post['effect'] == img_filter_grayscale) { echo 'selected=selected'; } echo ' >black and white'; echo '; if(isset($_post['effect']) && $_post['effect'] == img_filter_gaussian_blur) { echo ' selected=selected'; } echo '>blur'; echo '; if(isset($_post['effect']) && $_post['effect'] == img_filter_emboss) { echo 'selected=selected'; } echo '>emboss'; echo '; if(isset($_post['effect']) && $_post['effect'] == img_filter_negate) { echo 'selected=selected'; } echo '>negative'; ?>
php }?>最后是一个预览效果的页面image_effect.php
php $dir = 'd:\serious\phpdev\test\images';if(isset($_get['id']) && ctype_digit($_get['id']) && file_exists($dir.'/'.$_get['id'].'.jpg')){ $image = imagecreatefromjpeg($dir.'/'.$_get['id'].'.jpg');}else{ die('invalid image specified');}$effect = (isset($_get['e'])) ? $_get['e'] : -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; }header('content-type:image/jpeg');imagejpeg($image , '' , 100);?>
第二个image_check.php有点乱,在这个页面中有上传图片,处理图片,还有预览图片的部分逻辑,注意下面这段
php if($_post['submit'] == 'upload') { $imagename = 'images/'.$image_id.'.jpg'; } else { $imagename = 'image_effect.php?id='.$image_id.'&e='.$_post['effect']; } ?> %24imagename;?> style=max-width:90% alt=>
如果是上传直接访问图片,如果是预览则从image_effect.php中读取图片,这里是从内存中读取图片并根据选择的处理效果来展示图片。如下
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; }
当使用imagefilter方法处理图片之后会把图片输出到页面,这里要注意imagejpeg方法的第二个参数是空字符串,这样它就不会写入到硬盘中了,如果第二个参数设置了会覆盖原有的图片,这样可以让用户在保存图片之前随意的预览效果,如下:
header('content-type:image/jpeg');imagejpeg($image , '' , 100);
在check_image.php中有调用到类似的方法,但是这里指定了第二个参数,就是用来保存图片的:
imagejpeg($image , $dir.'/'.$_post['id'].'.jpg' , 100);
注意这里哦我们只写了三种处理效果,这个只是所有枚举中的一部分,我们来看所有的处理方式:
img_filter_negate:将图像中所有颜色反转。
img_filter_grayscale:将图像转换为灰度的。
img_filter_brightness:改变图像的亮度。用 arg1 设定亮度级别。
img_filter_contrast:改变图像的对比度。用 arg1 设定对比度级别。
img_filter_colorize:与 img_filter_grayscale 类似,不过可以指定颜色。用 arg1,arg2 和 arg3 分别指定 red,blue 和 green。每种颜色范围是 0 到 255。
img_filter_edgedetect:用边缘检测来突出图像的边缘。
img_filter_emboss:使图像浮雕化。
img_filter_gaussian_blur:用高斯算法模糊图像。
img_filter_selective_blur:模糊图像。
img_filter_mean_removal:用平均移除法来达到轮廓效果。
img_filter_smooth:使图像更柔滑。用 arg1 设定柔滑级别。
是不是很惊艳,php很强大的。