![06c557558701c022dd9c6dca61b9e8b9.png](https://i-blog.csdnimg.cn/blog_migrate/c7366c5c873da7b9f41993dcdede8235.jpeg)
php中文网最新课程
每日17点准时技术干货分享
![e42b8aaba53c69b90f6705810c3c9ebe.png](https://i-blog.csdnimg.cn/blog_migrate/968a355deeea3310c01d4cc3ae893e36.jpeg)
php图像处理函数imagecopyresampled用法
语法
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
参数:
成功时返回 TRUE, 或者在失败时返回 FALSE。
案例
1、图像裁减
<?php $targ_w = $targ_h = 150; // 设置目标宽度与高度 $jpeg_quality = 90; // 图片质量90,满分为100 $src = 'demo_files/pool.jpg'; // 被处理的图片 $img_r = imagecreatefromjpeg($src); // 获取原图 $dst_r = ImageCreateTrueColor( $targ_w, $targ_h ); // 获取新图 imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], $targ_w,$targ_h,$_POST['w'],$_POST['h']); // 目标图 源图 目标X坐标点 目标Y坐标点 源的X坐标点 源的Y坐标点 目标宽度 目标高度 源图宽度 源图高度 header('Content-type: image/jpeg'); imagejpeg($dst_r,null,$jpeg_quality); // 输出图象到浏览器或文件?>
2、重新取样
<?php // 源文件$filename = '1.jpg';// 设置最大宽高$width = 400;$height = 400;// Content typeheader('Content-Type: image/jpeg');// 获取新尺寸list($width_orig, $height_orig) = getimagesize($filename);$ratio_orig = $width_orig/$height_orig;if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig;} else { $height = $width/$ratio_orig;}// 重新取样$image_p = imagecreatetruecolor($width, $height);$image = imagecreatefromjpeg($filename);imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);// 输出imagejpeg($image_p, null, 100);?>
附上上传图片的三种思路:
1.选择图片,提交表单,服务器统一处理上传,保存路径;
2.选择图片,上传,获取路径,提交表单,保存路径;
3.选择图片,上传到服务器,通过某种途径获取到服务器的图片,保存到本地。
![980aae1a610b9b6b2aa87da09a817af9.png](https://i-blog.csdnimg.cn/blog_migrate/c5585331e7a5fd514f646f8bb477227e.png)
▼