将图像另存为blob类型

时间:2011-05-18 17:24:04

标签: php mysql

我使用MySQL将图像保存为blob类型。我正在通过PHP上传文件,当我得到图像时,我只恢复了它的一部分。如何改善最大尺寸? (我的图像文件大小小于300 KB)

PHP上传器......

if($_FILES!=null && $_POST!=null){
    $file = $_FILES["image"]["tmp_name"];   

    if(!isset($file)){
        echo "Please upload an image";
    }else{
        $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));

        $image_name = addslashes($_FILES['image']['name']);
        $type=$_POST['type'];

        $image_size = getimagesize($_FILES['image']['tmp_name']);

        if($image_size==FALSE)
            echo "That's not an image.";
        else
        {

            if(!(mysql_query("INSERT INTO store (name,image,type) values  ('$image_name','$image','$type')")))
                echo "Problem uploading image";
            else
            {
                $lastid = mysql_insert_id();
                echo "Image uploaded. <p /> Your image: <p /> <img id='imageId' src=get.php?id=$lastid>";
            }
        }
    }
  }

检索图片

$id = addslashes($_REQUEST['id']) ;

$imageRow = mysql_query("SELECT * FROM store WHERE id=$id");

$image = mysql_fetch_assoc($imageRow);
$image = $image['image'];

header("Content-type: image/jpg");


echo $image;

4 个答案:

答案 0 :(得分:2)

您可以使用不同类型的blob。 Blob,Mediumblob,longblob等

http://dev.mysql.com/doc/refman/5.0/en/blob.html

答案 1 :(得分:1)

使用mysql_real_escape_string()转义图像数据,而不是addslashes()addslashes()不适用于二进制文件。

答案 2 :(得分:0)

使用这样的图像:

<img src='file_display.php?id=<?php echo $row['id']; ?>' width='100' height='100'>

此处,$row['id']是记录主键 - id

file_display.php

// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
    //connect to the db
    $link = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());

    // select our database
    mysql_select_db($database) or die(mysql_error());

    // get the image from the db
    $sql = "SELECT image FROM tbl_images WHERE id=" .$_GET['id'] . ";";

    // the result of the query
    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

    // set the header for the image
    header("Content-type: image/jpeg");
    echo mysql_result($result, 0);

    // close the db link
    mysql_close($link);
}

适用于WAMP 2.x包

答案 3 :(得分:-2)

您可以使用以下代码::

 <?php
        mysql_connect("localhost","root","") or die(mysql_error());
        mysql_select_db("database") or die(mysql_error());   

        $id = $_GET['id'];
        $sql = mysql_query(" SELECT * FROM  store WHERE id=$id") or die(mysql_error()); 

        $row = mysql_fetch_array($sql);
        header('Content: image/jpeg');

        echo $row['image']; 
 ?>