服务器生成动态图像和长URL

时间:2022-05-15 04:39:33

I need to display an image that is generated by the server. It is generated based on parameters supplied via URL. Problem is, parameters can be very numerous and long and as we know there is a limit to GET request length.

我需要显示由服务器生成的图像。它是基于通过URL提供的参数生成的。问题是,参数可能非常多且很长,因为我们知道GET请求长度有限制。

Here is a simplified version of the code:

这是代码的简化版本:

View:

<img alt="Preview" src="/preview/getPreview/?id=1&page=1&values=%7B%22object_82%22%3A%22Test%22%2C%22object_83%22%3A%22Test2%22%2C%22object_84%22%3A%22Test3%22%7D&size=676">

服务器生成动态图像和长URL

This is a very short list of parameters there will be much more. As you can see values is actually a JSON stringified array.

这是一个非常简短的参数列表,会有更多。正如您所看到的,值实际上是一个JSON字符串化数组。

The controller part of the code is more or less like this (not sure if it's actually relevant to the question but still here it is):

代码的控制器部分或多或少是这样的(不确定它是否与问题实际相关,但仍然在这里):

function getPreview() {
    $buffer = createImageFromData($id, $page, $values, false);
    $img = new Imagick();
    $img->setResolution(300, 300);
    $img->readimageblob($buffer);

    header("Content-type: image/jpeg");
    header("Content-Length: $lenght");
    header("Content-Disposition: inline; filename=preview.jpg");

    echo $img;
}

So the question is - how to display server generated dynamic images without having to do the src='' part that will simply get too long for some images.

所以问题是 - 如何显示服务器生成的动态图像,而不必执行src =''部分,这对于某些图像来说会变得太长。

1 个解决方案

#1


1  

If you have a lot of information that you need to pass to the server. A get request is going to run out of space on older browsers fast: maximum length of HTTP GET request?

如果您有大量信息需要传递给服务器。 get请求将在旧浏览器上快速耗尽空间:HTTP GET请求的最大长度?

What you could do is using javascript, post the information to your server first before you show the image. If you would like to save the data to a database an return an ID that would be a good solution, you can then load the image with:

您可以做的是使用javascript,在显示图像之前先将信息发布到您的服务器。如果您想将数据保存到数据库并返回一个很好的解决方案ID,您可以使用以下命令加载图像:

<img src="/preview/getPreview/?id=312">

If you do not want to save the data you could return a base64 encoded image directly, this isn't supported in < IE8 very well though. A response could look like:

如果您不想保存数据,则可以直接返回base64编码图像,但 中不支持此功能。响应可能如下所示:

<img src="data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="alt="Base64 encoded image" width="150" height="150"/>

I mentioned that you should post via javascript so the generation wouldn't have to reload the page. This however is something you do not have to do.

我提到你应该通过javascript发布,这样生成就不必重新加载页面了。然而,这是你不必做的事情。

Also - If you are generating the same image over again. We have experienced large page loading times due to the image creation script - So would suggest to cache the image if you can.

此外 - 如果您再次生成相同的图像。由于图像创建脚本,我们经历了大量的页面加载时间 - 因此,如果可以的话,建议缓存图像。

Maybe if you update your question to specifically state what your intentions are and what images you are trying create, I could taylor this answer better.

也许如果你更新你的问题以明确说明你的意图是什么以及你正在尝试创建什么图像,我可以更好地回答这个问题。

#1


1  

If you have a lot of information that you need to pass to the server. A get request is going to run out of space on older browsers fast: maximum length of HTTP GET request?

如果您有大量信息需要传递给服务器。 get请求将在旧浏览器上快速耗尽空间:HTTP GET请求的最大长度?

What you could do is using javascript, post the information to your server first before you show the image. If you would like to save the data to a database an return an ID that would be a good solution, you can then load the image with:

您可以做的是使用javascript,在显示图像之前先将信息发布到您的服务器。如果您想将数据保存到数据库并返回一个很好的解决方案ID,您可以使用以下命令加载图像:

<img src="/preview/getPreview/?id=312">

If you do not want to save the data you could return a base64 encoded image directly, this isn't supported in < IE8 very well though. A response could look like:

如果您不想保存数据,则可以直接返回base64编码图像,但 中不支持此功能。响应可能如下所示:

<img src="data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="alt="Base64 encoded image" width="150" height="150"/>

I mentioned that you should post via javascript so the generation wouldn't have to reload the page. This however is something you do not have to do.

我提到你应该通过javascript发布,这样生成就不必重新加载页面了。然而,这是你不必做的事情。

Also - If you are generating the same image over again. We have experienced large page loading times due to the image creation script - So would suggest to cache the image if you can.

此外 - 如果您再次生成相同的图像。由于图像创建脚本,我们经历了大量的页面加载时间 - 因此,如果可以的话,建议缓存图像。

Maybe if you update your question to specifically state what your intentions are and what images you are trying create, I could taylor this answer better.

也许如果你更新你的问题以明确说明你的意图是什么以及你正在尝试创建什么图像,我可以更好地回答这个问题。