我已将图像移至 Rackspace Cloud Files 并使用他们的 PHP API。我正在尝试执行以下操作:
- 从“原始”容器中获取图像
- 调整其大小、锐化等。
- 将调整大小的图像保存到“拇指”容器
我的问题是#2。我希望调整大小而不必先将原始文件复制到我的服务器(因为图像很大并且我想动态调整大小),但不知道如何调整。这是我到目前为止所拥有的(不多):
$container = $conn->get_container("originals");
$obj = $container->get_object("example.jpg");
$img = $obj->read();
部分问题是我不完全理解 read() 函数返回的内容。我知道 $img 包含对象的“数据”(我能够将其打印为乱码),但它既不是文件,也不是网址,也不是图像资源,所以我不知道如何处理它。是否有可能以某种方式将 $img 转换为图像资源?我尝试了 imagecreatefromjpeg($img) 但这不起作用。
谢谢!
I have moved my images to Rackspace Cloud Files and am using their PHP API. I am trying to do the following:
- Get an image from my "originals" container
- Resize it, sharpen it, etc.
- Save the resized image to the "thumbs" container
My problem is with #2. I was hoping to resize without having to copy the original to my server first (since the images are large and I'd like to resize dynamically), but can't figure out how. This is what I have so far (not much):
$container = $conn->get_container("originals");
$obj = $container->get_object("example.jpg");
$img = $obj->read();
Part of the problem is I don't fully understand what is being returned by the read() function. I know $img contains the object's "data" (which I was able to print out as gibberish), but it is neither a file nor a url nor an image resource, so I don't know how to deal with it. Is it possible to convert $img into an image resource somehow? I tried imagecreatefromjpeg($img) but that didn't work.
Thanks!
发布评论
评论(2)
首先,如果不将图像加载到内存中,则无法调整图像大小。除非远程服务器提供一些“为我调整图像大小,这是参数”API,否则您必须在脚本中加载图像才能对其进行操作。因此,您必须将文件从 CloudFiles 容器复制到服务器,对其进行操作,然后将其发送回存储中。
您从
$obj->read()
收到的数据是图像数据。这就是文件。它没有文件名,也没有保存在硬盘上,但它是整个文件。要将其加载到gd
中进行操作,您可以使用imagecreatefromstring
。这类似于使用imagecreatefrompng
,只是imagecreatefrompng
想要自己从文件系统读取文件,而imagecreatefromstring
仅接受已加载到内存中的数据。First, you cannot resize an image without loading it into memory. Unless the remote server offers some "resize my image for me, here are the parameters" API, you have to load the image in your script to manipulate it. So you'll have to copy the file from the CloudFiles container to your server, manipulate it, then send it back into storage.
The data you receive from
$obj->read()
is the image data. That is the file. It doesn't have a file name and it's not saved on the hard disk, but it is the entire file. To load this intogd
to manipulate it, you can useimagecreatefromstring
. That's analogous to using, for example,imagecreatefrompng
, only thatimagecreatefrompng
wants to read a file from the file system by itself, whileimagecreatefromstring
just accepts the data that you have already loaded into memory.您可以尝试将 $img 变量的内容转储到可写文件中,如下所示:
更多详细信息:
http://www.php.net/manual/en/function.fopen.php
http://www.php.net/manual/en/function.fwrite.php
编辑:
您可能还想仔细检查 read() 函数返回的数据类型,因为如果数据不是 jpg 图像,例如 png,则需要相应更改文件的扩展名。
You can try to dump the content of the $img variable into a writable file as per the below:
More details:
http://www.php.net/manual/en/function.fopen.php
http://www.php.net/manual/en/function.fwrite.php
Edit:
You might also want to double check the type of data returned by the read() function, because if the data is not a jpg image, if it's for example a png, the extension of the file needs to be changed accordingly.