我已经成功地将原始图像添加到我的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>";
}
}
}
}希望有人能帮上忙。谢谢。
发布于 2012-07-10 19:37:02
如果您想在数据库中添加缩略图($thumb_image_location)的路径,只需在unlink()之前添加插入路径的代码。
如果您希望将整个图像存储到数据库中,则需要该列为MEDIUMBLOB类型,然后在unlink()之前读取包含该图像的文件的代码,例如:
$img = file_get_contents($thumb_image_location);然后,将存储在$img中的数据插入到数据库中。
发布于 2012-07-10 19:38:05
理想情况下,you don't want to be adding the thumbnail itself to the database,只是对文件的引用(文件路径)。因此,虽然我不知道您的数据库是什么样子的,但您需要完成以下步骤:
基本上就是这样。
发布于 2012-07-10 19:39:39
如果您只想将图像的路径存储到数据库中,那么只插入路径并将其与HTML一起提供是很好的。
否则,如果您想要将图像的原始数据存储到数据库中,则必须将图像编码为base64字符串。
Sending/Displaying a base64 encoded Image -以下是您在base64中编码和成像的方式。
并将这个巨大的字符串存储到数据库的Blob字段类型中。
https://stackoverflow.com/questions/11412333
复制相似问题