简体   繁体   English

如何从服务器发送png图像,通过ajax在浏览器中显示

[英]how to send png image from server to display in browser via ajax

I have been having a hard time with what must be an incredibly normal task. 我一直很难完成一项非常正常的任务。 I upload and save images to my web server and save the path to the file in MySQL data base (this is all working). 我将图像上传并保存到我的Web服务器并将路径保存到MySQL数据库中的文件(这一切都正常)。 The thing that doesn't work is fetching an image file from the server and displaying it on the page via ajax. 不起作用的是从服务器获取图像文件并通过ajax在页面上显示它。

Originally I was trying to just retrieve the path from the database, and update an tag's src attribute with the path to the image. 最初我试图从数据库中检索路径,并使用图像路径更新标签的src属性。 This was working, but this way all the images are in a folder on the server where people all have access to them. 这是有效的,但这样所有图像都在服务器上的文件夹中,人们都可以访问它们。 This is not good. 这个不好。 I can only have the pictures that belong to certain users accessible by these users. 我只能拥有这些用户可以访问的某些用户的图片。

In order to restrict access to these photos I added an Apache directive on that folder, which successfully restricted access. 为了限制对这些照片的访问,我在该文件夹上添加了一个Apache指令,该指令成功地限制了访问。 The problem then became that I could not display the images in the browser by setting the src attribute to that path. 然后问题变成了我无法通过将src属性设置为该路径来在浏览器中显示图像。 See my post: https://serverfault.com/questions/425974/apache-deny-access-to-images-folder-but-still-able-to-display-via-img-on-site 请参阅我的帖子: https//serverfault.com/questions/425974/apache-deny-access-to-images-folder-but-still-able-to-display-via-img-on-site

Finally I have learned that I have to use PHP to read the image data directly from the server and send this data to the browser. 最后我了解到我必须使用PHP直接从服务器读取图像数据并将此数据发送到浏览器。 I have used the file_get_contents() function, which works to convert the image file (PNG) on the server into a string. 我使用了file_get_contents()函数,该函数用于将服务器上的图像文件(PNG)转换为字符串。 I return this string to the browser in an ajax call. 我在ajax调用中将此字符串返回给浏​​览器。 The thing I can't get is: how to convert this string back into an image using JavaScript? 我无法得到的是: 如何使用JavaScript将此字符串转换回图像?

See this code: 看到这段代码:

$.ajax({
    url: get_image.php,
    success: function(image_string){
        //how to load this image string from file_get_contents to the browser??
    }
});

You could display a default "no access" image to users who are forbidden to access the image: 您可以向禁止访问该图像的用户显示默认的“禁止访问”图像:

<?php

$file = $_GET['file']; // don't forget to sanitize this!

header('Content-type: image/png');
if (user_is_allowed_to_access($file)) {
    readfile($file);
}
else {
    readfile('some/default/file.png');
}

And, on the client side: 而且,在客户端:

<img src="script.php?file=path/to/file.png" />

Alternatively, if you really really want or need to send the data via Ajax, you can Base64 encode it: 或者,如果您真的想要或需要通过Ajax发送数据,您可以对它进行Base64编码:

<?php

echo base64_encode(file_get_contents($file));

And place the response in an img tag using the Data URI scheme 并使用数据URI方案将响应放在img标记中

var img = '<img src="data:image/png;base64,'+ server_reponse +'"/>';

Given that the Base64 encoded data is significantly larger than the original, you could send the raw data and encode it in the browser using a library . 鉴于Base64编码数据明显大于原始数据,您可以使用发送原始数据并在浏览器中对其进行编码。


Does that make sense to you? 这对你有意义吗?

Instead of getting get_image.php through AJAX, why not just use: 而不是通过AJAX获取get_image.php,为什么不使用:

<img src="get_image.php" />

It's practically the same thing. 这几乎是一回事。 You can just use AJAX to update the <img> dynamically. 您可以使用AJAX动态更新<img>

You can't do it via ajax. 你不能通过ajax来做到这一点。

You could do something like this: 你可以这样做:

<img src="script.php?image=image_name" />

Then use JavaScript to change the query string. 然后使用JavaScript更改查询字符串。

You can actually embed image data inside the img tag in the browser, therefore ajax code could look like this: 您实际上可以在浏览器中的img标记内嵌入图像数据,因此ajax代码可能如下所示:

$.ajax({
    url: get_image.php,
    success: function(image_string){
        $(document.body).append("<img src='data:image/gif;base64," + base64_encode(image_string) + "' />);
    }
});

Note that you will need to write this base64_encode function. 请注意,您需要编写此base64_encode函数。 Have a look at this question - the function is given there. 看看这个问题 - 在那里给出了功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM