PHP 裁剪图像以固定宽度和高度而不丢失尺寸比例
我希望创建尺寸为 100 像素 x 100 像素的缩略图。我看过很多解释这些方法的文章,但如果要保持尺寸比,大多数文章最终都会有宽度!=高度。
例如,我有一个 450 像素 x 350 像素的图像。我想裁剪为 100px x 100px。如果我保持这个比例,我最终会得到 100 像素 x 77 像素。当我在行和列中列出这些图像时,这使得它变得很难看。然而,没有尺寸比例的图像看起来也会很糟糕。
我看过 flickr 上的图片,它们看起来棒极了。例如:
缩略图:http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
中等尺寸:http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg< /a>
大尺寸:http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg< /a>
tks
im looking to create thumbnails that has 100px by 100px dimension. i've seen many articles explaining the methods but most end up having the width!=height if the dimension ratio is to be kept.
for example, i have a 450px by 350px image. i would like to crop to 100px by 100px. if i were to keep the ratio, i would end up having 100px by 77px. this makes it ugly when im listing these images in a row and column. however, a image without dimension ratio will look terrible as well.
i've seen images from flickr and they look fantastic. for example:
thumbnail: http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
medium size: http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
large size: http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg
tks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是通过仅使用图像的一部分作为缩略图来完成的,该缩略图具有 1:1 的宽高比(主要是图像的中心)。如果你仔细观察,你可以在 flickr 缩略图中看到它。
因为你的问题中有“作物”,我不确定你是否已经知道这一点,但是你想知道什么呢?
要使用裁剪,下面是一个示例:
This is done by only using a part of the image as the thumbnail which has a 1:1 aspect ratio (mostly the center of the image). If you look closely you can see it in the flickr thumbnail.
Because you have "crop" in your question, I'm not sure if you didn't already know this, but what do you want to know then?
To use cropping, here is an example:
根据较小的宽度或高度将图像裁剪为正方形
$target_url - 是图像的名称。
Crop image with square based on lesser width or height
$target_url - is Name of image.
您可以使用此代码。
您需要传递源图像路径和缩略图大小(以像素为单位)以及可选的目标路径。如果通过,它将保存图像,否则将显示拇指。
只允许使用 jpg、jpeg 和 png。
You can use this code.
You need to pass source image path and thumbnail size in px, and optional destination path. if you pass it will save the image else the thumb will be shown.
Only jpg, jpeg and png are allowed.
我喜欢使用 GDLib 来处理图像,它也非常容易使用。那里有很多博客文章和教程。我建议使用一个类或类似的类,因为处理图像中的所有变化可能非常耗时。
I like using GDLib to fiddle with images, it's fantastically easy to work with also. There are lots of blog posts and tutorials out there. I would recommend using a class for it or similar though, as taking care of all the variations in the image can be very time consuming.
为了完成@SvenKoschnicke代码,这里有一个处理其他图像格式的小工具:
To complete @SvenKoschnicke code, here is little tool to process other image formats: