PHP图片大小裁剪imagecopy、缩放imagecopyresampled函
网站建设 2022-07-27 08:13www.1681989.com免费网站
图片裁剪、缩放函数有很多,我推荐这两个.有点小修改. //图片裁剪、缩放函数 1. //$filepath图片路径,$percent缩放百分比 function imagepress($filepath,$percent=0.5){ // 图片类型 header(Content-Type: image/jpeg); // 获得新的图片大小 list($width, $height) = getimagesize($filepath); $new_width = $width $percent; $ […]
图片裁剪、缩放函数有很多,我推荐这两个.有点小修改.
//图片裁剪、缩放函数
1.
//$filepath图片路径,$percent缩放百分比
function imagepress($filepath,$percent='0.5'){
// 图片类型
header('Content-Type: image/jpeg');
// 获得新的图片大小
list($width, $height) = getimagesize($filepath);
$new_width = $width $percent;
$new_height = $height $percent;
// 重新取样
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filepath);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// 输出
return imagejpeg($image_p, null, 100);
}
PHP图片大小裁剪、缩放函数原图:
PHP图片大小裁剪、缩放函数效果图:
2.
//$filepath图片路径,$new_width新的宽度,$new_height新的高度
function imagepress($filepath, $new_width, $new_height)
{
$source_fo = getimagesize($filepath);
$source_width = $source_fo[0];
$source_height = $source_fo[1];
$source_mime = $source_fo['mime'];
$source_ratio = $source_height / $source_width;
$target_ratio = $new_height / $new_width;
// 源图过高
if ($source_ratio > $target_ratio)
{
$cropped_width = $source_width;
$cropped_height = $source_width $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
}
// 源图过宽
elseif ($source_ratio < $target_ratio)
{
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
}
// 源图适中
else
{
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
switch ($source_mime)
{
case 'image/gif':
$source_image = imagecreatefromgif($filepath);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($filepath);
break;
case 'image/png':
$source_image = imagecreatefrompng($filepath);
break;
default:
return false;
break;
}
$target_image = imagecreatetruecolor($new_width, $new_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, $new_width, $new_height, $cropped_width, $cropped_height);
header('Content-Type: image/jpeg');
imagejpeg($target_image);
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
}
网站设计
- 静宁会SEO的网站建设公司:全面提升您的网络影
- 提升在线业务的关键:选择最佳的丽水网站建设
- 浙江网站优化发展潜力如何
- 井研专业的网站建设公司:打造您的在线品牌
- 灵山SEO网站建设公司:提升您的在线业务表现
- 蒙城网站建设优化公司:提升您网站表现的理想
- 阳谷企业网站优化:提升线上业务力的关键
- 樟树专业的网站建设公司:打造您在线业务的坚
- 通河百度SEO排名的策略与技巧
- 重庆百度快照排名如何进行精准的客户引流
- 重庆百度快照排名
- 常宁便宜的建站公司:助您轻松打造在线业务
- 巫溪百度网站优化:提升网站曝光率与流量的关
- 湖北整站优化怎么做才能放大客户需求
- 闸北网站建设多少钱?全面解析与预算规划
- 辽宁企业网站优化怎么做电话营销