在PHP中如何对图片做缩放和裁剪的功能
Admin 2022-08-04 群英技术资讯 463 次浏览
php程序中改变图片大小的函数大多数人都想到用imagecopyresized(),不过经过测试比较发现,使用imagecopyresampled()改变的图片质量更高。
1、imagecopyresampled的使用
1、目标函数资源
2、源图像资源<要采样的图片资源>
3、x(0,0指图左上角)
4、y(x,y确定一个坐标,坐标确定了把采样的部分放到目标图像资源的位置)
5、源x(0,0指图右上角)
6、源y(源x与源y确定一个坐标,你要采用的原图像资源的某个部分的起始位置)
7、w
8、h(weight与height确定了放到目标图像资源上面的尺寸)
9、源w
10、源h(源w与源h确定了采样原图像资源的某个部分)
2、$height=$width/($imgWidth/$imgHeight);
使得图片整体不会被裁剪,缩放代码只需控制width即可
<?php header('Content-type:image/jpeg'); $width=300; $img=imagecreatefromjpeg('1/php1.jpg'); $imgWidth=imagesx($img); $imgHeight=imagesy($img); $height=$width/($imgWidth/$imgHeight); $img1=imagecreatetruecolor(500,500); imagecopyresampled($img1,$img,100,100,100,100,$width,$height,$imgWidth,$imgHeight); imagejpeg($img1); imagedestroy($img1); imagedestroy($img);
<?php header('Content-type:image/jpeg'); $width=200; $img=imagecreatefromjpeg('1/php1.jpg'); $imgWidth=imagesx($img); $imgHeight=imagesy($img); $height=$width/($imgWidth/$imgHeight); $img1=imagecreatetruecolor(500,500); imagecopyresampled($img1,$img,100,100,100,100,$width,$height,$imgWidth,$imgHeight); imagejpeg($img1); imagedestroy($img1); imagedestroy($img);
3、控制x、y与源x、源y可以进行裁剪
<?php header('Content-type:image/jpeg'); $width=500; $img=imagecreatefromjpeg('1/php1.jpg'); $imgWidth=imagesx($img); $imgHeight=imagesy($img); $height=$width/($imgWidth/$imgHeight); $img1=imagecreatetruecolor(500,500); imagecopyresampled($img1,$img,0,0,300,300,$width,$height,$imgWidth,$imgHeight); imagejpeg($img1); imagedestroy($img1); imagedestroy($img);
<?php header('Content-type:image/jpeg'); $width=500; $img=imagecreatefromjpeg('1/php1.jpg'); $imgWidth=imagesx($img); $imgHeight=imagesy($img); $height=$width/($imgWidth/$imgHeight); $img1=imagecreatetruecolor(500,500); imagecopyresampled($img1,$img,330,330,0,0,$width,$height,$imgWidth,$imgHeight); imagejpeg($img1); imagedestroy($img1); imagedestroy($img);
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
1、用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当...
这篇文章主要介绍了详解no input file specified 三种解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
冒泡排序、选择排序、插入排序、快速排序这四种PHP中经典的排序方式是经常能用到的,所以学习PHP一定要掌握这些。
swoole设置定时器可以使用自身提供JavaScript的setInterval/setTimeout异步高精度定时器,粒度为毫秒级。使用也非常简单。
本文主要给大家分享的是基于PHP的swoole 异步文件读写操作,小编觉得是比较实用的,因此分享给大家参考,感兴趣的朋友就跟随小编一起来了解一下吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008