Php 我将在数据库中设置缩略图
我有一个PHP脚本,可以在数据库中添加一张照片和一些字段,效果很好。我的数据库中的图像是一个BLOB。现在我将直接在数据库中添加此图像的缩略图。我正在使用GD库进行此操作,但它不起作用 缩略图中的斑点是0b,有人能帮我解决这个问题吗?与数据库的连接在顶部的_栏中完成,可以正常工作。所有内容都在数据库中设置,只有缩略图没有设置Php 我将在数据库中设置缩略图,php,mysqli,blob,thumbnails,gdlib,Php,Mysqli,Blob,Thumbnails,Gdlib,我有一个PHP脚本,可以在数据库中添加一张照片和一些字段,效果很好。我的数据库中的图像是一个BLOB。现在我将直接在数据库中添加此图像的缩略图。我正在使用GD库进行此操作,但它不起作用 缩略图中的斑点是0b,有人能帮我解决这个问题吗?与数据库的连接在顶部的_栏中完成,可以正常工作。所有内容都在数据库中设置,只有缩略图没有设置 <?php include 'top_bar.php'; ?> <div id="content">
<?php
include 'top_bar.php';
?>
<div id="content">
<?php
if (isset($_FILES['image']) && $_FILES['image']['size'] < 60000000) { //kijken of de image is verstuurd
// Temporary file name stored on the server
$titel = $_POST["titel"];
$beschrijving = $_POST["beschrijving"];
$datum = date('Y/m/d', time());
$categorie = $_POST["categorie"];
$tmpName = $_FILES['image']['tmp_name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
$thumb = base64_decode($data);
$oSourceImage = imagecreatefromstring($thumb);
$nWidth = imagesx($oSourceImage); // get original source image width
$nHeight = imagesy($oSourceImage); // and height
// create small thumbnail
$nDestinationWidth = 200;
$nDestinationHeight = 200;
//$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
$oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
imagecopyresized($oDestinationImage, $oSourceImage, 0, 0, 0, 0, $nDestinationWidth, $nDestinationHeight, $nWidth, $nHeight); // resize the image
ob_start(); // Start capturing stdout.
imageJPEG($oDestinationImage); // As though output to browser.
$sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
ob_end_clean(); // Dump the result so it does not screw other output.
$sBinaryThumbnail = addslashes($sBinaryThumbnail);
// Create the query and insert
// into our database.
mysqli_query($db,"INSERT INTO images (id, titel, beschrijving, datum, categorie, image, thumb)
VALUES ('','$titel','$beschrijving','$datum','$categorie','$data', '$sBinaryThumbnail')");
// Print results
echo "<p>De image is opgeslagen in de database.</p>";
}
else {
echo "<p>Je hebt nog geen image gekozen, of het bestand is te groot</p>";
}
?>
<?php
if(isset($_POST['verstuur']) && ($_FILES["bestand"]["size"] > 60000000)) {// Bericht voor als het is verstuurd maar groter is dan 60kb
echo "het bestand is te groot, probeer het nogmaals";
}
?>
<div id="upload_form">
<form enctype="multipart/form-data" method="post" >
<p>Titel:</p> <input type="text" name="titel"><br />
<p>Beschrijving:</p> <textarea rows="4" cols="50" name="beschrijving" placeholder="Typ hier een beschrijving"></textarea><br />
<p class="info">De volgende categorieën bestaan op deze website:
<?php
$query = mysqli_query($db, "SELECT DISTINCT(categorie) FROM images");//DISTINCT laat alleen unieke waardes zien
while($rij = mysqli_fetch_array($query)){
echo $rij['categorie']. ", ";
}
?> <br/>U kunt deze overnemen, of een nieuwe categorie toevoegen. Dit doet u door gewoon een nieuwe categorie te typen.</p>
<p>Categorie:</p> <input type="text" name="categorie"><br/>
<p>Image:</p> <input name="image" accept="image/jpeg, image/png, image/gif" type="file"><br/>
<input value="Verzenden" type="submit" id="button">
</form>
</div>
<?php
include 'left_menu.php';
?>
阅读imagejpeg()
函数的详细信息。您调用它的方式将直接将图像输出到输出缓冲区。变量$tn
仅包含资源标识符。您可以在imagejpg()
调用周围使用ob\u start()
和ob\u get\u flush()
()来获取实际图像,而无需将其写入文件:
ob_start();
imagejpeg($tn);
$image_contents = ob_get_flush();
// Now write $image_contents to the database, instead of $tn
但是,建议实际将映像写入文件系统并将其路径存储在数据库中。这样,图像就可以作为静态文件使用,这将从各个方面提高性能。解释它不起作用。拇指的BLOB是0b,因此他不向数据库发送缩略图的信息。这个脚本中的某个地方出错了,但我没有找到它。有很多SQL注入向量。在将$\u POST变量连接到SQL查询之前,请确保对其进行清理。好的,我将对此进行更改,我现在已放置了错误,并已在第一篇文章中更新了代码。其他值在数据库中设置良好我在脚本中编辑了它,如果我从thumb下载BLOB并用texteditor打开它,我会看到BLOB的内容,但这是一个警告:
warning:imagejpeg()希望参数1是资源,在我的第一篇文章中,第45行的*MY_URL*中给出了布尔值。这表明$tn
可能为false,这反过来表明imageCreateTureColor()
调用失败。我认为这是因为给定的参数不是整数,而是浮点数。同样,请仔细阅读。我已经阅读了此文档,我也收到了此警告,但我找不到原因:警告:imagecreatefromstring():空字符串或无效图像我现在在第一篇文章中添加了一些新代码。现在我得到四个警告:警告:imagecreatefromstring():第24行的*MY URL*中的空字符串或无效图像警告:imagesx()期望参数1为资源,第25行的*MY URL*中的布尔值警告:imagesy()期望参数1为资源,第26行的*MY URL*中的布尔值警告:imagecopyresized()预期参数2是资源,布尔值在第36行的*MY URL*中给出。您知道如何解决此问题吗?您当前的脚本有很多问题。addslashes()
和base64\u decode()
调用是为了什么?我不会为您的问题提供完整的解决方案,因为您描述的错误很容易调试。只需使用常识和RTFM。
ob_start();
imagejpeg($tn);
$image_contents = ob_get_flush();
// Now write $image_contents to the database, instead of $tn