php手把手教你做网站(三十)上传图片生成缩略图
qiyuwang 2024-11-20 20:45 9 浏览 0 评论
三种方法:按比例缩小、图片裁切、预览图片裁切
- 不管使用哪一个都是建立在图片已经上传的基础上;
- 预览裁切上传,如果预览的图片就是原始大小,可以预览裁切以后上传(这里是个假象,下边会说明);
1、上传以后按比例缩小
由于上传的图片宽度、高度比例和前台限制的不一定是一样的,这就可能出现前台div内无法铺满或者宽度高度有超出的情况,下面是代码:
缩小方法imageZoom:
//原图
$filename = "E:\phpweb\public\assets\skin\default\images/about.jpg";
//缩略图保存的图片
$savename="E:\phpweb\public\assets\skin\default\images/sabout.jpg";
/*
* 300是宽度
* 200 高度
*/
imageZoom($filename,300,200,$savename);
imageZoom方法执行缩小
function imageZoom($filename,$width,$height,$savename){
//获取原图信息
$sourceImageInfo = getimagesize($filename);
$sourceWidth = $sourceImageInfo[0];
$sourceHeight = $sourceImageInfo[1];
$sourceMine = $sourceImageInfo['mime'];
$sourceRatio = $sourceWidth/$sourceHeight;
$sourceX = 0;
$sourceY = 0;
/*
*原图宽度 小于目标图片(也就是缩小以后的图) 不做任何操作
*/
if($sourceWidth<$width&&$sourceHeight<$height){
return '';
}
/*
$zoom 计算缩小倍数
*哪个缩小的少 就用哪个比例
*选择缩小少的 在前台显示的时候可以填充满div, 但是会超出,需要用css隐藏
*选择缩小多的,可以不超出,需要css设置 左右 上下居中
*/
if(($width/$sourceWidth)<($height/$sourceHeight)){
$zoom=$height/$sourceHeight;
}else{
$zoom=$width/$sourceWidth;
}
//缩小以后的宽度高度
$targetWidth =$zoom>=1?$sourceWidth:round($sourceWidth*$zoom);
$targetHeight =$zoom>=1?$sourceHeight:round($sourceHeight*$zoom);
$sourceImage = null;
switch($sourceMine){
case 'image/gif':
$sourceImage = imagecreatefromgif($filename);
break;
case 'image/jpeg':
$sourceImage = imagecreatefromjpeg($filename);
break;
case 'image/png':
$sourceImage = imagecreatefrompng($filename);
break;
default:
return '图片信息发生错误!';
break;
}
$new_thumb = imagecreatetruecolor($sourceWidth, $sourceHeight);
$targetImage = imagecreatetruecolor($targetWidth,$targetHeight);
imagecopy($new_thumb, $sourceImage, 0, 0, $sourceX, $sourceY, $sourceWidth, $sourceHeight);
imagecopyresampled($targetImage, $new_thumb, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
//设置header 可以直接浏览器查看
//header('Content-Type: image/jpeg');
//imagejpeg($target_image); //直接预览
//保存图片
if(file_exists($savename)){
unlink($savename);
}
imagejpeg($targetImage,$savename);
//销毁
imagedestroy($new_thumb);
imagedestroy($sourceImage);
imagedestroy($targetImage);
return 'ok';
}
说明:
- $sourceX, $sourceY是指截图的起始位置,距离图片左上角的距离;
- $sourceWidth, $sourceHeight是指沿着X轴,Y轴截取的距离;
imagecopy($new_thumb, $sourceImage, 0, 0, $sourceX, $sourceY, $sourceWidth, $sourceHeight);
2、上传图片裁切
上传图片宽高比例不是前台显示的,通过裁切可以实现前台div内铺满
//原图
$filename = "E:\phpweb\单语html10.21.11\public\assets\skin\default\images/about.jpg";
//裁切以后保存的图片
$savename="E:\phpweb\单语html10.21.11\public\assets\skin\default\images/sabout.jpg";
//300 宽度 100 高度
imagecut($filename,300,100,$savename);
imagecut方法裁切
function imagecut($filename, $target_width, $target_height,$savename){
//首先获取原图的信息(getimagesize)
$source_info = getimagesize($filename);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
//当原图的宽度 高度 均小于要裁切的宽度高度的时候 不做任何操作
if($source_width<$target_width&&$source_height<$target_height){
return '';
}
//原图宽度 小于目标图宽度的时候
if($source_width<$target_width){
$target_width=$source_width;
}
//原图高度 小于目标图高度的时候
if($source_height<$target_height){
$target_height=$source_height;
}
//计算原图 和 目标图的宽高比例 原图按照目标比例载入
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width;
/*
source_x ,source_Y是指到图片左上角的距离
*/
if ($source_ratio > $target_ratio){
// 图片按照比例 高度超出 需要在Y轴 截取
//多出的高度 或者宽度 以图片的中心点为分界线,分在两边,
$scale_width = $source_width;
$scale_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $scale_height) / 2;
}elseif ($source_ratio < $target_ratio){
//图片按照比例 宽度超出 需要在X轴 截取
$scale_width = $source_height / $target_ratio;
$scale_height = $source_height;
$source_x = ($source_width - $scale_width) / 2;
$source_y = 0;
}else{
//比例一样 载入原图
$scale_width = $source_width;
$scale_height = $source_height;
$source_x = 0;
$source_y = 0;
}
switch ($source_mime){
case 'image/gif':
$source_image = imagecreatefromgif($filename);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($filename);
break;
case 'image/png':
$source_image = imagecreatefrompng($filename);
break;
default:
return ;
break;
}
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($scale_width, $scale_height);
// 在坐标轴 上截取图片载入
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $scale_width, $scale_height);
// zoom
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $scale_width, $scale_height);
//设置header 可以直接浏览器查看
//header('Content-Type: image/jpeg');
//imagejpeg($target_image); //直接预览
//保存图片
if(file_exists($savename)){
unlink($savename);
}
imagejpeg($target_image,$savename);
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
}
说明:
- 上下2个方法通过对比会发现几乎都是一样的,不同点主要在imagecopy()的参数也就是$sourceX, $sourceY,$sourceWidth, $sourceHeight;
- 比如图片宽度1000px,而我们只想要800px的图片,上边方法就是以图片中心点为原点,多出来的分到了2边,复制的时候是在100px开始, 复制800px的宽度($sourceX=100;$sourceWidth=800;),Y轴方向的是同样的道理;
- 如果就想以图片左上角为起点,开始复制或者理解为裁切也是可以的;
3、预览图片裁切上传
- 预览使用的是jquery插件做的效果,js不能裁切,只是做了选取框,通过提交给后台php去裁切,传递的参数就是$sourceX, $sourceY, $sourceWidth, $sourceHeight,还有图片名称(包含路径要不就是后台自动添加路径);
- 预览的图片很可能不是图片的原始大小,是经过缩小的,比如原始1000px,而我们设置了div容器宽度为500px;选取框没有经过缩小,那么裁切图片上边传递的几个参数就是不准确的;
- 如果原图比我们的容器要小,可以上传图片之前预览,把这几个参数同时传递到后台,上传完成以后使用上边的第2种情况裁切;
- 大的图片应该先上传使用第2种情况裁切(宽度和高度不能超过我们预览图片的容器),返回裁切以后的图片到预览的窗口;
加载对应的js,css
<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
<script src="js/jquery-migrate-1.2.1.js"></script>
<script src="js/jquery.imgareaselect.pack.js"></script>
<link rel="stylesheet" type="text/css" href="css/imgareaselect-default.css" />
说明:
使用了imgareaselect插件,实现预览裁切的效果,但是该插件在jquery1.9以后会出错,因为1.9以后取消了判断浏览器的方法,可以使用migrate插件使其继续生效,也可以直接使用jquery1.9以前的版本,也就不再需要migrate。
html代码
<div style="margin: 100px 0 0 100px;float:left;width:500px;height:400px;border:1px solid #DDD;"><img id="photo" src="/news.jpg" style="max-width:100%;max-height:100%;" /></div>
<div style='margin:100px 0 0 80px;float:left;'>
起点X:<input type='text' id='x1' /><br/>
起点Y:<input type='text' id='y1' /><br/>
X长度:<input type='text' id='xwidth' /><br/>
Y长度:<input type='text' id='yheight' /><br/>
<button class='baocun'>保存</button>
</div>
说明:
x1,y1,xwidth,yheight对应了$sourceX, $sourceY, $sourceWidth, $sourceHeight
js代码
$(document).ready(function() {
var picwidth=$("#photo").width();
var picheight=$("#photo").height();
var slkwidth=200;//选择器的宽度
var slkheight=150;//选择器的高度
//初始值
var x1=picwidth/2-slkwidth/2;
var y1=picheight/2-slkheight/2;
var x2=picwidth/2+slkwidth/2;
var y2=picheight/2+slkheight/2;
$("#x1").val(x1);//x轴
$("#y1").val(y1);//Y轴
$("#xwidth").val(slkwidth);//x轴长度
$("#yheight").val(slkheight);//Y轴长度
$('#photo').imgAreaSelect({
//aspectRatio: '4:1', //横竖比例
handles: true,
x1: x1,
y1: y1,
x2: x2,
y2: y2,
minWidth: slkwidth,
minHeight: slkheight,
//最大宽度 高度 maxwidth maxheight
fadeSpeed: 200,
onSelectChange: cutinfo //回调方法
});
//保存选择的信息
$(".baocun").click(function(){
//需要传递的参数
var logX=$(".xianshk").position().left;//截图的x坐标
var logY=$(".xianshk").position().top;//截图的Y坐标
var rqwidth=$(".xianshk").width();//沿X轴截取的长度
var rqheight=$(".xianshk").height();//沿Y轴截取的长度
var picpath='images/about.jpg';//要裁切的文件(包含路径)
console.log(logY);
$.ajax({
//传递数据到后台程序 通过php程序裁切图片
})
})
});
function cutinfo(img, selection) {
$("#x1").val(selection.x1);//x轴
$("#y1").val(selection.y1);//Y轴
$("#xwidth").val(selection.width);//x轴长度
$("#yheight").val(selection.height);//Y轴长度
console.log(selection.x1);
}
说明:
- 鼠标移动的时候触发cutinfo方法,实时获取坐标,赋值给对应的文本框;
- 最大宽度 和最小宽度设置一样,可以固定选择框的大小;
- 上边的初始值就是通过计算,让选择框居中;
- 可能会出现当前默认的就是用户要裁切的图,所以要有个默认位置的初始值;
总结:
1)(imagecreatetruecolor)创建图像,只需要宽高
$cropped_image = imagecreatetruecolor($scale_width, $scale_height);
2)(imagecopy)复制图片到目标图
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $scale_width, $scale_height);
$source_x, $source_y, $scale_width, $scale_height从哪个位置开始复制,复制的宽度,高度;
$source_image是调整比例以后的图片,如果不需要调整,那就是我们的原图;
3)(imagecopyresampled)复制过来的图片按比例缩小放到到目标图
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $scale_width, $scale_height);
- 因为已经调整过比例,直接以左上角为起点就可以,不必更改原点;
- 如果不经过前边的步骤直接使用imagecopyresampled,生成的缩略图比例就是错的,图片会走形;
如果按照上述步骤不好理解,可以换个角度,就像我们用软件制图一样。
- 创建宽高的空白图像;
- 调整原图比例,使其符合要目标图的比例,多出来的宽度或者高度分到2边,裁切(复制)中间部分到目标图;
- 调整新生成的这个图片,以左上角为起点,进行缩小,使其宽度高度,均达到目标图的要求;
- 上一篇:一篇文章带你搞懂流式布局
- 下一篇:前端CSS学习:旋转、缩放效果的实现(代码)
相关推荐
- 在Word中分栏设置页码一页两个页码的技巧!
-
施老师:在正常情况下,Word文档中一页只会出现一个页码。但在某种情况下,比如说:用了分栏后,我们希望一页中出现两个页码,那应该如何实现呢?今天,就由宁双学好网施老师来为大家讲一下,利用域来实现一页两...
- 如何在关键时刻向上自荐(如何在关键时刻做出正确选择)
-
抓住机会,挺身而出有种时刻叫“关键时刻”,关键时刻,作为一个认为自己有能力的、训练有素的人,应该考虑挺身而出,甚至应该不考虑就挺身而出。...
- WPS Word:跨页的文档表格,快速调整为一页。#Excel
-
如何快速将跨页的文档表格调整为一页?需要根据两种情况分别处理。如果表格所有行的行高相同,调整为一页的方法有两种。第一种方法是将光标移动到表格内,然后将鼠标移动到表格右下角的方框处,按住鼠标左键向上拖动...
- word文档插入下一页分节符(word下一页分页符)
-
在word文档中,对文档页面进行分页是特别常见的操作,其中的下一页分节符也是用得比较多的,但是一些人不太清楚在哪里设置,也不知道它具体能实现的功能是什么。接下来看看如何在word文档中插入下一页分节符...
- word文档如何设置某一页纸张的方向
-
word文档页面方向有横向和纵向,纵向是默认的纸张方向,有时我们需要将页面设置为横向,或只设置其中某一页方向,应该怎么操作呢?一起来看看下面的详细介绍第一步:...
- word怎么单独设置一页为横向(word2019怎样设置单独一页为横向)
-
word里面其中一页可以改为横向的吗?经过实际操作发现是完全可以的。...
- Word如何设置分栏,如何一页内容同时显示一栏和两栏
-
我们使用Word文档,有时需要用到两栏的排版,甚至一页内容同时包含一栏和两栏的排版,这种格式怎么设置呢?具体步骤如下:首先是两栏排版的设置,直接点击Word文件上方工具栏【布局】,选择【分栏】下面的【...
- Word怎么分页?这三个方法可以帮到你
-
我们不仅可以利用Word编辑文档,还可以编辑文集呢。但是有时候会出现两个部分的文章长短不一,我们需要对文档进行分页处理。这样可以方便我们对文档进行其他操作。那么Word怎么分页呢?大家可以采用下面这...
- Word内容稍超一页,如何优化至单页打印?
-
如何将两页纸的内容,缩到一页打印呢?有时候一页纸多一点内容,我们完全可以缩一下,放到一页来打印。...
- [word] word 表格如何跨行显示表头、标题
-
word表格如何跨行显示表头、标题在Word中的表格如果过长的话,会跨行显示在另一页,如果想要在其它页面上也显示表头,更直观的查看数据。难道要一个个复制表头吗?当然不是,教你简单的方法操作设置Wo...
- Word表格跨页如何续上表?(word如何让表格跨页不断掉)
-
长文档的表格跨页时,你会发现页末空白太多了,这时要怎么调整?选中整张表格,右击【表格属性】,点击【行】选项,之后勾选【允许跨页断行】,点击确定即可解决空白问题。...
- Word怎么连续自动生成页码,操作步骤来了!
-
Word怎么连续自动生成页码,操作步骤来了!...
- word文档怎么把两页合并成一页内容?教你4种方法
-
word怎么把两页合并成一页?word怎么把两页合并成一页?用四种方法演示一下。·方法一:把这一个文档合并成一页,按ctrl加a全选文档,然后右键点击段落,弹出的界面行距改成固定值,磅值可以改小一点,...
- 如何将Word中的一页的纸张方向设置为横向?这里提供详细步骤
-
默认情况下,MicrosoftWord将页面定向为纵向视图。虽然这在大多数情况下都很好,但你可能拥有在横向视图中看起来更好的页面或页面组。以下是实现这一目标的两种方法。无论使用哪种方法,请注意,如果...
- Word横竖混排你会玩吗?(word横排竖排混合)
-
我们在用Word排版的时候,一般都是竖版格式,但偶尔会需要到一些特殊的版式要求,比如文档中插入的一个表格,横向的内容比较多,这时就需要用到横版,否则表格显示不全。这种横竖版混排的要求,在Word20...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- navicat无法连接mysql服务器 (65)
- 下横线怎么打 (71)
- flash插件怎么安装 (60)
- lol体验服怎么进 (66)
- ae插件怎么安装 (62)
- yum卸载 (75)
- .key文件 (63)
- cad一打开就致命错误是怎么回事 (61)
- rpm文件怎么安装 (66)
- linux取消挂载 (81)
- ie代理配置错误 (61)
- ajax error (67)
- centos7 重启网络 (67)
- centos6下载 (58)
- mysql 外网访问权限 (69)
- centos查看内核版本 (61)
- ps错误16 (66)
- nodejs读取json文件 (64)
- centos7 1810 (59)
- 加载com加载项时运行错误 (67)
- php打乱数组顺序 (68)
- cad安装失败怎么解决 (58)
- 因文件头错误而不能打开怎么解决 (68)
- js判断字符串为空 (62)
- centos查看端口 (64)