您的位置:首页 > 编程语言 > PHP开发

PHP生成缩略图功能

2013-08-25 14:20 288 查看
直接上代码:

$source_pic = @$_POST['source_pic']; //源图像
if (!empty($source_pic))
{
$filename = ROOT_PATH.$source_pic;
$pathRes = pathinfo($filename);
$newname = "thumb_".substr($pathRes['filename'],strpos($pathRes['filename'],"_")).".".$pathRes['extension'];//生成新图像名称
$file_ext = $pathRes['extension'];
//header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $_POST['x2']-$_POST['x1']; //x2,x1 分别为横轴截取的两个点
$new_height = $_POST['y2']-$_POST['y1']; //y2,y1 分别为纵轴截取的两个点

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
if ($file_ext=="jpg" || $file_ext=="jpeg") //生成jpg图像
{
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, $_POST['x1'], $_POST['y1'], $new_width, $new_height, $new_width, $new_height);
// Output
imagejpeg($image_p,ROOT_PATH."images/user_face/".$newname, 100);
}
elseif ($file_ext=="png") //生成png图像
{
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, $_POST['x1'], $_POST['y1'], $new_width, $new_height, $new_width, $new_height);
// Output
imagepng($image_p,ROOT_PATH."images/user_face/".$newname);
}
elseif ($file_ext=="gif") //生成gif图像
{
$image = imagecreatefromgif($filename);
imagecopyresampled($image_p, $image, 0, 0, $_POST['x1'], $_POST['y1'], $new_width, $new_height, $new_width, $new_height);
// Output
imagegif($image_p,ROOT_PATH."images/user_face/".$newname);
}

$res  = $userInfoObj->updateItem($uid,array('user_face'=>$newname));
echo "OK";
exit();


其他说明:选择四个点的时候,可以使用js插件进行选择 具体的链接地址为:http://files.cnblogs.com/ypeih/imagecut_demo.rar 点击下载

注意:生成缩缩图错误的实例

在做的过程中,突然旁边的同事使用这个插件怎么上传都出错,可是上传其他的图片文件都没有问题,代码也没有更改过,这个事情折腾了我一个多小时,最后找资料才知道是文件类型被同事更改了

本来是gif的文件,他把后缀更改为了jpg
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: