<?php
//剪裁图片
//1,打开图像
//2,创建一个空白的图像
$name="./img/1.jpg";
// cut($name,0,0,200,200);
$x1=289;
$y1=149;
$x2=438;
$y2=257;
cut($name,289,149,438,257);
function cut($name,$x1,$y1,$x2,$y2){
function getinfo($name){
//getimagesize($dname)函数可以动态的获取图片类型,大小,宽度和高度等
$info=getimagesize($name);
$width=$info[0];
$height=$info[1];
$mime=$info['mime'];
switch ($mime) {
case 'image/jpeg':
$res=imagecreatefromjpeg($name);
break;
case 'image/gif':
$res=imagecreatefromgif($name);
break;
case 'image/png':
$res=imagecreatefrompng($name);
break;
case 'image/wbmp':
$res=imagecreatefromwbmp($name);
break;
}
return array('width'=>$width,'height'=>$height,'res'=>$res);
}
$info=getinfo($name);
$img=imagecreatetruecolor(($x2-$x1),($y2-$y1));
imagecopymerge($img,$info['res'],0,0,$x1,$y1,($x2-$x1),($y2-$y1),100);
// header("content-type:image/png");
// imagepng($img);
$ext=pathinfo($name,PATHINFO_EXTENSION);
$rand_name=md5(mt_rand().time()).".".$ext;
switch($ext){
case 'jpg':
case 'jpeg':
case 'jpe':
imagejpeg($img,$rand_name);
break;
case 'png':
imagepng($img,$rand_name);
break;
case 'gif':
imagegif($img,$rand_name);
break;
case 'bmp':
case 'wbmp':
imagewbmp($img,$rand_name);
break;
}
//销毁资源
imagedestroy($info['res']);
imagedestroy($img);
}
PHP使用GD函数库剪裁图片+保存本地
最新推荐文章于 2025-04-25 12:28:58 发布