简体   繁体   English

如何在jquerymobile模板上显示存储在MySql数据库中的blob类型图像

[英]how to display blob type images stored in MySql database on a jquerymobile template

I have a scenario where images should be displayed in my Jquerymobile template. 我有一个场景,图像应该显示在我的Jquerymobile模板中。 These images come from MySQL database. 这些图像来自MySQL数据库。 Image type is blob type, and I am using PHP to retrieve my data - as follows 图像类型为Blob类型,我正在使用PHP检索我的数据-如下

    <?php
include 'configure.php';
$qr = "SELECT * FROM food_beverage";
$res= mysql_query($qr);
$i=0;
while($row = mysql_fetch_array($res))
{
$restau_arr[$i]["fb_sno"] = $row["fb_sno"];
$restau_arr[$i]["fb_name"] = $row["fb_name"];
$restau_arr[$i]["fb_type"] = $row["fb_type"];
$restau_arr[$i]["fb_location"] = $row["fb_location"];
$restau_arr[$i]["fb_img"]= $row["fb_img"];
$restau_arr[$i]["fb_phno"]= $row["fb_phno"];
$restau_arr[$i]["fb_email"]= $row["fb_email"];
$i++;
}
 header('Content-type: application/json'); 
  echo json_encode($restau_arr);
?>

The values from database are stored in an array, and are being converted to JSON format so that I can call them using the following jquery lines - 来自数据库的值存储在一个数组中,并被转换为JSON格式,因此我可以使用以下jquery行来调用它们-

var url = "retrieve_all.php";   
     $.getJSON(url, function(json) {
                 .......
                  .......
                   ....... }

Once the values are received at this function, am appending them to respective HTML fields using - 在此函数中接收到值后,请使用-将它们附加到相应的HTML字段中

 $.each(json, function(i,v) {                                     
                    var restauName = v.fb_name;
                    var restauLoc = v.fb_location;
                    var restauNum = v.fb_phno;
                                    var restauImg = v.fb_img;
                    $("#restauName").html(restauName);
                    $("#restauLoc").html(restauLoc);
                    $("#restauNum").html(restauNum);
                    $("#restauImg").html(restauImg);
                    });

I am able to understand that the image should NOT be taken as a JSON format. 我能够理解,不应将图像视为JSON格式。 But how can this be dealt? 但是如何解决呢? What should I do in PHP to get my blob image displayed on my page/screen? 我该如何在PHP中将我的Blob图片显示在页面/屏幕上?

Is the approach correct? 方法正确吗?

For this, I checked here for help in the beginning, but my scenario seemed to be different. 为此,我一开始在这里检查了帮助,但情况似乎有所不同。

This is more of notes that will give you a solution, than "click here for results"; 与“单击此处获取结果”相比,更多的是可为您提供解决方案的注释; but try this: 但是尝试一下:

  • Aside from the images you have a functioning solution? 除了图像之外,您还有功能正常的解决方案吗?
  • Are the images large? 图像大吗?
    • If they are icons, I would embed the image data in the same JSON response (see end) 如果它们是图标,则将图像数据嵌入到相同的JSON响应中(请参阅结尾)
    • If they are photos, I would return an URL, and have a second HTTP request to download them. 如果它们是照片,我将返回一个URL,并有另一个HTTP请求来下载它们。 You can attach <img> elements to a document via AJAX response handlers. 您可以通过AJAX响应处理程序将<img>元素附加到文档。
    • The image serving script would take the ID, validate it, then return the DB field value. 图片投放脚本将获取ID,对其进行验证,然后返回DB字段值。
    • You will need to ensure there is no charset conversion on your binary data, or the images will be corrupted. 您将需要确保二进制数据上没有字符集转换,否则图像将被破坏。

To embed everything in a single response 将所有内容嵌入单个响应中

  • rfc2397 rfc2397
  • Make the image JSON item look like: 使图像JSON项目看起来像:
    • data:image/jpeg;base64,base_64_encoded_jpeg_goes_here
    • Obviously you can change the mime encoding to suit your needs. 显然,您可以更改mime编码以适合您的需求。
  • Add some JS to take this string, append an <img> element, with that data as the src attribute. 添加一些JS以使用此字符串,并添加一个<img>元素,并将该数据作为src属性。

I have a rough demo of JSON to img elements (with external files). 我有一个JSON到img元素 (带有外部文件)的粗略演示。

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

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