简体   繁体   English

如何使用php从mySQL DB显示经过压缩的Blob文件

[英]How to display a compessed Blob file from mySQL DB with php

I have written an Android app which sends images to my mySQL database. 我已经编写了一个Android应用程序,可以将图像发送到mySQL数据库。 Before sending the image to db i compress it for less traffic: 在将图像发送到数据库之前,我先压缩它以减少流量:

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

So now I want to display the image at my website using php. 所以现在我想使用php在我的网站上显示图像。 I tried it with not compressed images and it worked with this code: 我尝试使用未压缩的图像,并且可以使用以下代码:

<!DOCTYPE html>
<html>
<body>
<?php
    $id = $_GET['id'];

    $db = mysqli_connect("localhost","user","pass","db-name"); 
    $sql = "SELECT * FROM tbl_images WHERE id = $id";
    $sth = $db->query($sql);
    $result=mysqli_fetch_array($sth);
    echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['imgdata']     ).'"/>';
?>

</body>
</html>

Can you help me with the compressed image? 您能帮我压缩图像吗? Thx! 谢谢!

1) any gains you'll get from compressing your image will be lost by base64 encoding it. 1)通过base64编码,您压缩图像所获得的任何收益都将丢失。 Best to keep your image as binary and upload it. 最好将图像保留为二进制文件并上传。

2) It is preferable to store files on the file system instead of storing a base64 encoded string in the database. 2)最好将文件存储在文件系统上,而不是在数据库中存储base64编码的字符串。 So you'll store a filename or file path in the database record and the actual binary on the file system. 因此,您将在文件记录中存储文件名或文件路径,并在文件系统上存储实际的二进制文件。 Then in the image tag, you'll just make the source be the path to the uploaded file. 然后,在图片标签中,您只需将源作为上传文件的路径即可。

3) Your SQL query is open to SQL injection. 3)您的SQL查询可以进行SQL注入。 What would the query return if I typed my id parameter as "1 OR 1=1"? 如果我将id参数键入为“ 1 OR 1 = 1”,查询将返回什么?

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

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