在PHP中如何对图片做缩放和裁剪的功能
Admin 2022-08-04 群英技术资讯 510 次浏览
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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
方法:首先打开phpstorm,依次点击顶部导航栏的“File”-“Setting”-“Plugins”-“Browse repositories”;然后在搜索框中输入“PHP”,搜索需要的PHP插件,点击“install”进行安装即可。
我们在本篇文章里给大家整理了关于PHP中fclose函数的详细用法和知识点,需要的朋友们学习下。
在本篇文章里小编给大家分享了关于php中访问修饰符的知识点总结,兴趣的朋友们可以学习参考下。
在开发中我们经常需要自定义错误和异常处理函数,来提供不同场景下更为友好的处理提示。今天我们就来介绍一下用set_exception_handler/set_error_handler解决异常/错误的方法,有需要的可以参考参考。
这篇文章给大家分享是关于session创建和销毁的方法,小编认为是比较实用的,因此分享给大家,下面介绍是使用PHP7创建与销毁session的方法,有这方面学习需求的朋友可以看看。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008