创建图像而不将其存储为本地文件
这是我的情况 - 我想从用户上传的图像创建一个调整大小的 jpeg 图像,然后将其发送到 S3 进行存储,但我希望避免将调整大小的 jpeg 写入磁盘,然后为 S3 请求重新加载它。
有没有办法完全在内存中完成此操作,并将图像数据 JPEG 格式保存在变量中?
Here's my situation - I want to create a resized jpeg image from a user uploaded image, and then send it to S3 for storage, but am looking to avoid writing the resized jpeg to the disk and then reloading it for the S3 request.
Is there a way to do this completely in memory, with the image data JPEG formatted, saved in a variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
大多数使用 PHP 的人都会选择 ImageMagick 或 Gd2
我从未使用过 Imagemagick; Gd2 方法:
希望这会有所帮助。
Most people using PHP choose either ImageMagick or Gd2
I've never used Imagemagick; the Gd2 method:
Hope this helps.
这可以使用 GD 库和输出缓冲来完成。 我不知道与其他方法相比效率如何,但它不需要显式创建文件。
This can be done using the GD library and output buffering. I don't know how efficient this is compared with other methods, but it doesn't require explicit creation of files.
将 JPEG 存入内存后(使用 ImageMagick,GD,或您选择的图形库),您需要将对象从内存上传到 S3。
许多 PHP S3 类似乎只支持文件上传,但是 UnDesigned 似乎做了我们在这里之后要做的事情 -
如果您使用 GD,则可以使用
imagecreatefromstring 从流中读取图像,但我不确定是否可以按照上面
s3->inputResource
的要求获取结果对象的大小 - getimagesize 返回高度、宽度等,但不返回图像资源的大小。Once you've got the JPEG in memory (using ImageMagick, GD, or your graphic library of choice), you'll need to upload the object from memory to S3.
Many PHP S3 classes seem to only support file uploads, but the one at Undesigned seems to do what we're after here -
If you're using GD instead, you can use
imagecreatefromstring to read an image in from a stream, but I'm not sure whether you can get the size of the resulting object, as required by
s3->inputResource
above - getimagesize returns the height, width, etc, but not the size of the image resource.这个游戏已经很晚了,但如果您使用 ConroyP 和 Imagick 提到的 S3 库,您应该使用 putObjectString() 方法而不是 putObject() ,因为 getImageBlob 返回一个字符串。 最终对我有用的例子:
我在这个问题上遇到了一点困难,希望它对其他人有帮助!
Pretty late to the game on this one, but if you are using the the S3 library mentioned by ConroyP and Imagick you should use the putObjectString() method instead of putObject() due the fact getImageBlob returns a string. Example that finally worked for me:
I struggled with this one a bit, hopefully it helps someone else!
意识到这是一个旧线程,但今天我花了一些时间在这个问题上用头撞墙,并认为我会在这里为下一个人捕获我的解决方案。
此方法使用 AWS SDK for PHP 2 和 GD 来调整图像大小(也可以轻松使用 Imagick)。
Realize this is an old thread, but I spent some time banging my head against the wall on this today, and thought I would capture my solution here for the next guy.
This method uses AWS SDK for PHP 2 and GD for the image resize (Imagick could also be easily used).
Imagemagick 库可以让您做到这一点。 有很多像 this 这样的 PHP 包装器(甚至还有 示例代码 说明您想在该页面上执行的操作 ;) )
The Imagemagick library will let you do that. There are plenty of PHP wrappers like this one around for it (there's even example code for what you want to do on that page ;) )
我遇到同样的问题,使用 openstack 对象存储和 php-opencloud 库。
这是我的解决方案,它不使用
ob_start
和ob_end_clean
函数,而是将图像存储在内存和临时文件中。 内存和临时文件的大小可以在运行时调整。关于
php://temp
(来自php的官方文档):I encounter the same problem, using openstack object store and php-opencloud library.
Here is my solution, which does not use the
ob_start
andob_end_clean
function, but store the image in memory and in temp file. The size of the memory and the temp file may be adapted at runtime.About
php://temp
(from the official documentation of php):Maye 使用 GD 库。
有一个功能可以复制图像的一部分并调整其大小。 当然,该部分可以是整个图像,这样您只需调整它的大小。
请参阅 imagecopyresampled
Maye by using the GD library.
There is a function to copy out a part of an image and resize it. Of course the part could be the whole image, that way you would only resize it.
see imagecopyresampled