0

我已成功将原始图像添加到我的 imgs/ 文件夹以及服务器上。但我想将缩略图添加到数据库中。我已将其添加到 imgs/ 文件夹中,但似乎无法将其插入数据库。

这是用于裁剪 img 并将其插入文件夹的最后一段代码。

我还需要将它插入数据库,以便我可以为 $_SESSION 用户和用户朋友调用它,因为我有个人资料。

if (isset($_POST["upload_thumbnail"]) && strlen($large_photo_exists)>0) {
    //Get the new coordinates to crop the image.
    $x1 = $_POST["x1"];
    $y1 = $_POST["y1"];
    $x2 = $_POST["x2"];
    $y2 = $_POST["y2"];
    $w = $_POST["w"];
    $h = $_POST["h"];
    //Scale the image to the thumb_width set above
    $scale = $thumb_width/$w;
    $cropped = resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
    //Reload the page again to view the thumbnail
    header("location:".$_SERVER["PHP_SELF"]);
    exit();
}
if(isset($_GET['a'])){
if ($_GET['a']=="delete"){
if (file_exists($large_image_location)) {
        unlink($large_image_location);
    }
    if (file_exists($thumb_image_location)) {
        unlink($thumb_image_location);

        $creator_id     =   $_SESSION['id'];
            $sql = "UPDATE users SET user_pic_small='".$img."' WHERE id=$creator_id";
            $sql2 = "INSERT INTO userphotos(photo_ownerid,photo_ispublic, photo_name, photo_caption, photo_imagedata) VALUES ($creator_id,1,'Profile Picture','Profile Picture','$img')";

            // insert the image
            if(!mysql_query($sql)) {
                echo "Fail. It broke.";
            }else{
            $c=mysql_query($sql2);

                echo "<script> parent.alert('Image Uploaded','',1000);</script>";
            }

    }
}
}

希望有人可以提供帮助。谢谢。

4

4 回答 4

2

如果要在数据库中添加缩略图的路径 ($thumb_image_location),只需在 unlink() 之前添加插入路径的代码。

如果要将整个图像存储到数据库中,则需要列为 MEDIUMBLOB 类型,然后,在 unlink() 之前读取包含图像的文件的代码,例如:

$img = file_get_contents($thumb_image_location);

然后,将存储在 $img 中的数据插入到您的数据库中。

于 2012-07-10T11:37:01.773 回答
1

理想情况下,您不想将缩略图本身添加到数据库中,而只是对文件的引用(文件路径)。因此,虽然我不知道您的数据库是什么样的,但您需要执行以下步骤:

  1. 在您的表格中创建一个名为“缩略图”或类似名称的字段。这将保存缩略图文件的名称。
  2. 裁剪大图像后立即将文件路径添加到数据库(即在代码中的 '$cropped = ...' 和 'header("location....' 行之间)
  3. 每当用户或用户的朋友登录时,检查此字段并提取表中引用的任何缩略图。

基本上就是这样。

于 2012-07-10T11:38:05.297 回答
0

你可以使用这个:

// Read the file 
$fp = fopen($file, 'r');
$data = fread($fp, filesize($file));
$data = addslashes($data);
fclose($fp);

// Create the query and insert into our database.
// image is an BLOB field type
$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$data')";
$results = mysql_query($query, $link);
于 2012-07-10T11:50:08.980 回答
0

如果您只想将图像的路径存储到数据库中,那么只插入路径并使用 HTML 提供它就可以了。

否则,如果要将图像的原始数据存储到数据库中,则必须将图像编码为 base64 String 。

发送/显示 base64 编码图像- 以下是在 base64 编码和图像的方式。

并将这个巨大的字符串存储到数据库中的 Blob 字段类型中。

于 2012-07-10T11:39:39.493 回答