如何使用 CodeIgniter 填充图像?
我的用户正在上传任意尺寸 XX x YY 的图像。我想找到更大的尺寸,并将图像缩小到 250x250 的正方形,并添加透明填充来弥补差异。有什么办法可以用 CI 的 Image Lib 来完成这个任务吗?
My user is uploading an image of any size XX x YY. I want to find the larger dimension and shrink the image to a square of 250x250 with transparent padding being added to make up the difference. Is there any way to accomplish this with CI's Image Lib?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
详细信息,请参阅 CodeIgniter API 中的 图像操作类
有关 老实说,我觉得 CI 的图像处理库不够用。它不是很灵活,对于重复或组合操作(如上面的操作)来说不是很快,并且它使用了一种糟糕的语法(这对于某些类型的应用程序可能会派上用场,但我个人更喜欢像 < a href="http://phpthumb.gxdlabs.com/" rel="noreferrer">PHP Thumb):
事实上,我最近为 PHP Thumb 编写了一个简单的包装库(下载)与 CodeIgniter 一起使用,效果非常好。使用 PHP Thumb 库而不是 CodeIgniter 自己的库,您可以像这样完成工作:
有关 PHP Thumb 的 GitHub 页面。
只是这不会用透明像素填充图像(我认为这是一种不好的做法),它只是调整图像大小并裁剪图像以适合整个 250x250 像素区域,而不拉伸图像。
或者,您可以使其调整图像大小以适合 250x250 像素框(通过使用
resize
而不是adaptiveResize
),然后将250x250px DIV 容器内的图像。我猜这可能是您想要的解决方案。Try this:
For details, see the Image Manipulation Class from the CodeIgniter API
Although to be quite honest, I don't find CI's image manipulation library to be adequate. It isn't very flexible, it isn't very fast for repeated or combined actions (like the one above), and it uses an atrocious syntax (that may come in handy for certain types of applications, but I personally prefer something like PHP Thumb):
In fact, I recently put together a simple wrapper library for PHP Thumb (download) to use with CodeIgniter, and it works fabulously. Using the PHP Thumb library instead of CodeIgniter's own, you could get the job done like so:
More details on PHP Thumb's GitHub page.
Only this doesn't pad the image with transparent pixels (which I would consider a bad practice), it merely resizes and crops the image to fit the whole 250x250 pixel area without stretching the image.
Alternatively, you could make it resize the image to fit inside a 250x250 pixel box (by using
resize
rather thanadaptiveResize
), and then place the images inside 250x250px DIV containers. I'm guessing this is probably the solution you'll want.我不裁剪图像,而是将其放置在 div 的背景中。这是我的代码。
1)将其放置在助手内部。
2)使用它来调整大小。
I do not crop the image, rather place it in the background of a div. Here is my code.
1) Inside the helper place this.
2) Use this to resize.