2014-04-08 64 views
1

我有一個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 
+0

解釋_it不起作用_ –

+0

拇指的BLOB是0b,因此他沒有向數據庫發送縮略圖信息。在這個腳本的某處發生了錯誤,但我找不到它。 – aarnoo

+0

有很多SQL注入向量。確保在將$ _POST變量連接到SQL查詢之前對其進行清理。 – circusdei

回答

0

閱讀imagejpeg()功能的documentation。您調用它的方式將直接將圖像輸出到輸出緩衝區。變量$tn只包含資源標識符。你可以使用ob_start()ob_get_flush()documentation)圍繞imagejpg()呼叫,而不將其寫入到文件,以獲取實際的圖像:

ob_start(); 
imagejpeg($tn); 
$image_contents = ob_get_flush(); 
// Now write $image_contents to the database, instead of $tn 

不過,這將是可取的實際寫入圖像文件系統和存儲數據庫中的路徑。這樣,圖像可以作爲靜態文件提供,從而可以提高各方面的性能。

+0

我已經在腳本中編輯了這個值,如果我從拇指下載BLOB並用我的texteditor打開它,我會看到BLOB的內容,但這是一個警告: 'Warning:imagejpeg( )期望參數1是資源,布爾在*第45行給出的* MY_URL *中 在我的第一篇文章中,我編輯了代碼。 – aarnoo

+0

這表明'$ tn'可能是錯誤的,這反過來表明'imagecreatetruecolor()'調用失敗。我認爲這是因爲給定的參數不是整數,而是浮點數。再次請閱讀[文檔](http://php.net/imagecreatetruecolor)。 – Plenka

+0

我已閱讀此文檔,我也得到這個警告,但我找不到它爲什麼:警告:imagecreatefromstring():空字符串或無效圖像 – aarnoo