来源:未知 时间:2017-07-21 13:41 作者:xxadmin 阅读:次
[导读] /***裁剪+缩放图片*@paramarray$params包含x,y,width,height,path*@returnstring*/publicfunctiontailer_image($params){//$target_width,$target_height是缩放图片的大小$target_width=256;$target_height=256;$source_x=intval($params[x])...
/**
* 裁剪 + 缩放图片
* @param array $params 包含x,y,width,height,path
* @return string
*/
public function tailer_image($params){
//$target_width,$target_height是缩放图片的大小
$target_width = 256;
$target_height = 256;
$source_x = intval($params['x']);
$source_y = intval($params['y']);
$cropped_width = intval($params['width']);
$cropped_height = intval($params['height']);
$source_path = trim($params['path']);
$path = 'files/uploads/images/';
list($source_width,$source_height) = getimagesize($source_path);
//获取图片的扩展名
$extend_name = ltrim(strrchr(strtolower($source_path),'.'),'.');
if(!in_array($extend_name,array('jpg','jpeg','png'))){
return array(
array('message'=>"图片扩展名不正确,只允许'jpg','jpeg','png'"),
400
);
}
switch ($extend_name) {
case 'jpg':
$source_image = imagecreatefromjpeg($source_path);
break;
case 'jpeg':
$source_image = imagecreatefromjpeg($source_path);
break;
case 'png':
$source_image = imagecreatefrompng($source_path);
break;
}
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
// 裁剪
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 缩放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
$filename = md5(microtime()).'.'.$extend_name;
$dst_url = $path.$filename;
//输出图像到$dst_url文件
imagejpeg($target_image, $dst_url);
return $dst_url;
} |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com