PHP裁剪图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$src_path = '1.jpg';
//创建源图的实例
$src = imagecreatefromstring(file_get_contents($src_path));
  
//裁剪开区域左上角的点的坐标
$x = 100;
$y = 12;
//裁剪区域的宽和高
$width = 200;
$height = 200;
//最终保存成图片的宽和高,和源要等比例,否则会变形
$final_width = 100;
$final_height = round($final_width * $height / $width);
  
//将裁剪区域复制到新图片上,并根据源和目标的宽高进行缩放或者拉升
$new_image = imagecreatetruecolor($final_width, $final_height);
imagecopyresampled($new_image, $src, 0, 0, $x, $y, $final_width, $final_height, $width, $height);
  
//输出图片
header('Content-Type: image/jpeg');
imagejpeg($new_image);
  
imagedestroy($src);
imagedestroy($new_image);

本代码转自http://www.01happy.com/php-crop-picture/

posted on 2016-11-06 16:18  顾古  阅读(58)  评论(0)    收藏  举报

导航