PHP修改图片代码,PHP修改图片

这篇是关于修改图片的效果,主要还是用到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,主要是一个上传控件,用来选择图片

title>

style>

head>

Your usernametd>td>

tr>

Upload image*td>td>

tr>

* Acceptable image formats include: GIF, JPG/JPEG and PNG.em>small>

td>

tr>

Image Captiontd>td>

tr>

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!

span>%5B'id'%5D;?>.jpg

php

}else{?>

Here is your pic!

So how does it feel to be famous?

Here is the picture you just uploaded to your servers:

phpif($_POST['submit'] == 'Upload')

{$imagename = 'images/'.$image_id.'.jpg';

}else{$imagename = 'image_effect.php?id='.$image_id.'&e='.$_POST['effect'];

}?>

span>;?>Image save as:

$image_id?>

Height:

echo $height;?>

Widht:

echo $width;?>

Upload date:

echo $image_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

None

phpecho ';

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有点乱,在这个页面中有上传图片,处理图片,还有预览图片的部分逻辑,注意下面这段

phpif($_POST['submit'] == 'Upload')

{$imagename = 'images/'.$image_id.'.jpg';

}else{$imagename = 'image_effect.php?id='.$image_id.'&e='.$_POST['effect'];

}?>

span>;?>

如果是上传直接访问图片,如果是预览则从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很强大的。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值