在 PHP 中调整图像大小,保持比例、透明度和居中
如果文件是 gif
或 png
图像,如何在 PHP 中调整图像大小,保持宽高比和透明度,然后根据情况将图像垂直或水平居中最终尺寸?
how do I resize an image in PHP keeping the aspect ratio, the transparency if the file is a gif
or a png
image, and then center the image vertically or horizontally depending on the final size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有几个技巧。这是我的功能:
there are several tricks. here is my func:
如果
意味着您想在周围添加白色或黑色边框,我不会这样做。只需保存调整大小的图像并使用 html/css 在浏览器中居中即可。
If
means that you want to add a white or black border around, I wouldn't do that. Just save the resized image and do the centering in the browser using html/css.
供以后搜索
这是正确答案
我发现这个答案基于: http://php.net/manual/en /function.imagecopyresampled.php#112742
For later search
This is the correct answer
I found this answer base on : http://php.net/manual/en/function.imagecopyresampled.php#112742
首先使用 getimagesize() 读取要调整大小的图像的图像大小;
计算出您希望调整大小的图像的最大宽度和高度。
找出要调整大小的图像的宽度或高度中最大的一个。
找出您必须将该侧划分多少才能使其在调整大小的宽度或高度内。取该值,然后划分要调整大小的图像的另一侧。
现在您就得到了调整后的图像的宽度和高度。只需使用 imagecopyresampled() 即可。
Firstly read in the image size of the image you want to resize using getimagesize();
Work out what the max width and height you want your resized image to be.
Find out which is the largest, the width or the height of your image to be resized.
Find out how much you have to devide that side to make it within the width or height of the resized. Take that value then devide the other side of the image to be resized.
Now you have your resulting width and height of the resized image. just use imagecopyresampled().
我过去使用过一个名为 Image_Transform (http://pear.php.net/package/Image_Transform) 的 PEAR 包。它有很多用于操作图像的强大功能,并且非常易于使用(不必是高级 PHP 程序员)
There's a PEAR package called Image_Transform (http://pear.php.net/package/Image_Transform) that I've used in the past. It has a lot of great functions for manipulating images and it's very easy to use (don't have to be an advanced PHP programmer)