直接从 Rackspace 云文件“对象”调整图像大小;无需下载?

发布于 2024-12-03 05:26:12 字数 559 浏览 1 评论 0 原文

我已将图像移至 Rackspace Cloud Files 并使用他们的 PHP API。我正在尝试执行以下操作:

  1. 从“原始”容器中获取图像
  2. 调整其大小、锐化等。
  3. 将调整大小的图像保存到“拇指”容器

我的问题是#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:

  1. Get an image from my "originals" container
  2. Resize it, sharpen it, etc.
  3. 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏蝶舞 2024-12-10 05:26:12

首先,如果不将图像加载到内存中,则无法调整图像大小。除非远程服务器提供一些“为我调整图像大小,这是参数”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 into gd to manipulate it, you can use imagecreatefromstring. That's analogous to using, for example, imagecreatefrompng, only that imagecreatefrompng wants to read a file from the file system by itself, while imagecreatefromstring just accepts the data that you have already loaded into memory.

开始看清了 2024-12-10 05:26:12

您可以尝试将 $img 变量的内容转储到可写文件中,如下所示:

<?php
$filename = 'modifiedImage.jpg';

/* 
 * 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate 
 * the file to zero length. If the file does not exist, attempt to create it. 
 */
$handle = fopen($filename, 'w+');

// Write $img to the opened\created file.
if (fwrite($handle, $img) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
}

echo "Success, wrote to file ($filename)";

fclose($handle);
?>

更多详细信息:

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:

<?php
$filename = 'modifiedImage.jpg';

/* 
 * 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate 
 * the file to zero length. If the file does not exist, attempt to create it. 
 */
$handle = fopen($filename, 'w+');

// Write $img to the opened\created file.
if (fwrite($handle, $img) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
}

echo "Success, wrote to file ($filename)";

fclose($handle);
?>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文