将每种尺寸的图像调整为固定宽度和高度
我尝试了很多方法来做到这一点,但仍然遇到很多麻烦。
是否可以将所有图像调整为固定的宽度和高度?
我希望将每个上传的 width>=200px
和 height>=260px
图像的大小调整为 width=200px
和 height=260px
,但我想保持一点比例,如果图像大于200x260px
,则按比例调整其大小,然后捕获图像中心200x260px
。
我只需要知道从哪里开始以及做什么,但如果你有一个例子,我想看看。谢谢。
I tried a lot of methods to do that, but I still have a lot of troubles with that.
Is it possible to resize all images to fixed width and height?
I want every uploaded image with width>=200px
and height>=260px
to be resized to width=200px
and height=260px
, but I want to keep a bit of proportionality and if image is bigger than 200x260px
to resize it proportionally and then to capture center of image 200x260px
.
I just need idea to know where to start and what to do, but if you have an example I would like to see it. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想修剪图像,可以按以下方式进行:-
If you want to trim the image you can do in the following way:-
要开始编写函数,我们必须这样声明它......然后我们必须添加我们的属性。我们想要限制我们的图像,所以我们必须让函数知道我们想要限制它的尺寸,并且我们必须知道原始图像的大小是多少(我们将在一秒钟内讨论这部分) )。
在测试新功能之前,我们需要获取要显示的图像的宽度和高度。 PHP 中有一个神奇的命令,叫做 getimagesize()。如果使用得当,该命令将返回图像的宽度、高度、类型,甚至 HTML 图像标签格式的宽度和高度(宽度=“x”高度=“y”)。
现在,$mysock 是一个数组,它保存有关我们要显示的特定图像的重要信息。在索引 0 中,我们有宽度 ($mysock[0]),在索引 1 中,我们有高度 ($mysock[1])。为了完成我们想要完成的事情,这确实是我们所需要的。想看看这个函数……好吧,函数?开始了!
假设您想要显示漂亮袜子的列表,但您希望页面上有空间将它们整齐地排成一行,并且为此它们的高度或宽度不能大于 150 像素。
就是这样!现在,无论原始文件大小如何,宽度或高度(或您指定的任何内容)都将限制为不超过 150 像素。
To begin writing the function, we have to declare it as such… Then we have to throw in our attributes. We want to restrict our image, so we have to let the function know the dimensions to which we want to restrict it, and we have to know what the original image size is to begin with (we’ll get to that part in a second).
Before we take our new function on a test drive, we need to get the width and height of the image that we want to display. There is a magical command in PHP called getimagesize(). This command, used properly, will return the image width, height, type, and even the width and height in HTML image tag format (width=”x” height=”y”).
Now, $mysock is an array that holds vital information about the particular image we want to display. In index 0, we have the width ($mysock[0]), and in index 1, we have the height ($mysock[1]). That’s really all we need, in order to get what we want done. Want to see the function… well, function? Here we go!
Let’s say you want to display a list of your beautiful socks, but you want room on the page to show them neatly in a row, and to do that they cannot be larger than 150 pixels tall or wide.
That’s it! Now, no matter what the original file size, it will be restricted to no more than 150 pixels in width or height (or whatever you specify).