我有一個PHP腳本,用一些字段在數據庫中添加一張照片,這工作正常。我的數據庫中的圖像是一個BLOB。現在我將直接在數據庫中添加此圖像的縮略圖。我是用GD庫做的,但它不起作用。我會在數據庫中設置一個縮略圖
縮略圖中的BLOB是0b,任何人都可以幫我解決這個問題嗎?與數據庫的連接在top_bar中完成並工作。一切都在數據庫中設置,只有縮略圖沒有。
<?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';
?>
我現在有四個警告與此腳本,任何人都可以解釋爲什麼我得到這個警告? 的警告是:
Warning: imagecreatefromstring(): Empty string or invalid image in *MY URL* on line 24 Warning: imagesx() expects parameter 1 to be resource, boolean given in *MY URL* on line 25 Warning: imagesy() expects parameter 1 to be resource, boolean given in *MY URL* on line 26 Warning: imagecopyresized() expects parameter 2 to be resource, boolean given in *MY URL* on line 36
解釋_it不起作用_ –
拇指的BLOB是0b,因此他沒有向數據庫發送縮略圖信息。在這個腳本的某處發生了錯誤,但我找不到它。 – aarnoo
有很多SQL注入向量。確保在將$ _POST變量連接到SQL查詢之前對其進行清理。 – circusdei