【问题标题】:Inserting Thumbnail into Database将缩略图插入数据库
【发布时间】:2012-07-09 20:55:06
【问题描述】:

我已成功将原始图像添加到我的 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>";
            }

    }
}
}

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

【问题讨论】:

  • 我在这里根本看不到任何 INSERT 语句。你试过什么?你能描述一下你的架构吗?
  • 用 INSERT 更新了问题

标签: php mysql thumbnails


【解决方案1】:

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

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

$img = file_get_contents($thumb_image_location);

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

【讨论】:

  • 它必须是BLOB 类型、SMALLMEDIUMLARGE 取决于缩略图的大小。
  • 在存储到数据库之前,他可能必须将addslashes() 应用于$img 变量,例如$img = addslashes($img);
  • 像这样,这就是我的主图像在准备插入时的外观。 $img =addslashes (file_get_contents($thumb_image_location));
  • 对我来说看起来不错。请参阅我的答案,在您更新代码之前发布它,我使用了read() fct。您可以轻松适应它。
  • 谢谢大家。我的缩略图现在成功地在我的数据库以及我的文件夹中。一个简单的 $img =addslashes(file_get_contents($thumb_image_location))
【解决方案2】:

理想情况下,you don't want to be adding the thumbnail itself to the database 只是对文件的引用(文件路径)。因此,虽然我不知道您的数据库是什么样的,但您需要执行以下步骤:

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

基本上就是这样。

【讨论】:

  • 是的,它只是文件路径。抱歉,我没有明确说明。现在尝试几个答案。 :)
【解决方案3】:

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

否则,如果您想将图像的原始数据存储到数据库中,则必须将图像编码为 base64 字符串。

Sending/Displaying a base64 encoded Image - 这是您在 base64 编码和图像的方式。

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

【讨论】:

    【解决方案4】:

    你可以用这个:

    // 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);
    

    【讨论】:

      猜你喜欢
      • 2011-09-03
      • 2017-05-15
      • 2020-06-24
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多