BLOB图像返回破碎的图像

时间:2017-01-25 17:54:59

标签: php html mysql

我发现很多解决方案都适用于其他人,但不适合我!我们将非常感谢正确指导。

注意:我知道很多人可能不喜欢在blob中存储图像,但对于我的特定应用程序来说就足够了。

上传页面上的HTML代码片段(这是在包含其他数据的表单中):

<div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label>Summary Image</label>
                        <br /><input name="summaryImage" id="imgInp1" type="file" accept="image/*"  onchange="Prev1(this)" />
                        <br /><img id="imgPrev1" src="" alt="Summary Image Preview" style="width:99%; height: 400px; margin-top:10px;" />
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label>Article Image</label>
                        <br /><input name="articleImage" id="imgInp2" type="file" accept="image/*"  onchange="Prev2(this)" />
                        <br /><img id="imgPrev2" src="" alt="Article Image Preview" style="width:99%; height: 400px; margin-top:10px;" />
                </div>
            </div>
        </div>

上传页面上的PHP代码片段

//Summary Image
$summaryimage = $_FILES['summaryImage'];
if(is_uploaded_file($_FILES['summaryImage']['tmp_name']) && getimagesize($_FILES['summaryImage']['tmp_name']) != false)
{
    // Gather image info
    $size = getimagesize($_FILES['summaryImage']['tmp_name']);
    // Assign variables
    $type = $size['mime'];
    $imgfp = fopen($_FILES['summaryImage']['tmp_name'], 'rb');
    $size = $size[3];
    $name = $_FILES['summaryImage']['name'];
    $summaryimagesize = $_FILES['summaryImage']['size'];
    $maxsize = 15728640;


    // Check image file size
    if($summaryimagesize < $maxsize ) {
        //Image size checks out, insert blob into database
        mysql_query("UPDATE projects 
                        SET summaryimage='$summaryimage', summaryimagesize='$summaryimagesize' 
                    WHERE job='$job'") or die (mysql_error());;

        } else if($summaryimagesize > $maxsize) {
        // Image file size too big
        header("LOCATION: /projects.php?message=imagesize");
        }
    } else {
    // Any other error
    header("LOCATION: /projects.php?message=imageformat");
    }

    //Article Image
    $articleimage = $_FILES['articleImage'];
    if(is_uploaded_file($_FILES['articleImage']['tmp_name']) && 
        getimagesize($_FILES['articleImage']['tmp_name']) != false)
    {
        // Gather image info
        $size = getimagesize($_FILES['articleImage']['tmp_name']);
        // Assign variables
        $type = $size['mime'];
        $imgfp = fopen($_FILES['articleImage']['tmp_name'], 'rb');
        $size = $size[3];
        $name = $_FILES['articleImage']['name'];
        $articleimagesize = $_FILES['articleImage']['size'];
        $maxsize = 15728640;

        // Check image file size
        if($articleimagesize < $maxsize ) {
            //Image size checks out, insert blob into database
             mysql_query("UPDATE projects SET articleimage='$articleimage', articleimagesize='$articleimagesize' WHERE job='$job'") or die (mysql_error());;

            } else if($articleimagesize > $maxsize) {
            // Image file size too big
            header("LOCATION: /projects.php?message=imagesize");
            }
        } else {
        // Any other error
        header("LOCATION: /projects.php?message=imageformat");
        }

    header("LOCATION: /projects.php?message=success");

}

数据库将blob存储为 longblob 类型,没有问题: projects table

代码I用于编码图像并显示它:

<?php echo '<img src="data:image/jpeg;base64,' . base64_encode( $post['summaryimage'] ) . '" />'; ?>

页面显示的内容: Broken Image

右键单击&gt;复制图像位置:

data:image/jpeg;base64,QXJyYXk=

2 个答案:

答案 0 :(得分:1)

QXJyYXk=array的base64,因此您可以存储数组数据而不是图像。

修复是抓取图像的内容并存储它:

 $summaryimage = file_get_contents($_FILES['summaryImage']['tmp_name']);

由于您未使用预备语句,因此您应该使用引号:

 $summaryimage = addslashes(file_get_contents($_FILES['summaryImage']['tmp_name']));

显示时,您应该在对其进行编码之前将其删除:

base64_encode(stripslashes($post['summaryimage']))

答案 1 :(得分:0)

使用@ Xorifelse的答案,在玩了大约一个小时后,我能够自己找到一个有效的解决方案。

因为这是一个SQL查询,我不能在二进制文件中有任何',因为它会破坏它(谁知道它会有特殊字符?!)。所以我用过 $summaryimage = mysql_real_escape_string(file_get_contents($_FILES['summaryImage']['tmp_name']));

现在显示图像时一切正常:)