2014-04-08 4 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'; 
    ?> 

이제이 스크립트에 대해 4 가지 경고가 표시됩니다. 왜이 경고가 나오는 지 설명 할 수 있습니까? 경고는 다음과 같습니다

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 삽입 벡터는 아주 많이 있습니다. 변수를 SQL 쿼리로 연결하기 전에 $ _POST 변수를 살균하십시오. – circusdei

답변

0

imagejpeg() 함수의 documentation을 읽어보십시오. 당신이 그것을 호출하는 방법은 출력 버퍼에 이미지를 직접 출력합니다. 변수 $tn에는 자원 식별자 만 포함됩니다. , 실제로 파일 시스템에 이미지를 작성하는 것이 바람직 할 것이다 그러나

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

과 저장 : 당신은 파일로 작성하지 않고 실제 이미지를 가져 오도록 imagejpg() 호출 주위에 ob_start()ob_get_flush() (documentation)를 사용할 수 있습니다 데이터베이스의 경로. 이렇게하면 이미지를 정적 파일로 제공 할 수 있으므로 모든면에서 성능이 향상됩니다.

+0

엄지 손가락에서 BLOB을 다운로드하고 내 텍스트 편집기로 열면 BLOB 내용을 볼 수 있지만 경고입니다 : 'Warning : imagejpeg () 매개 변수 1 리소스로, boolean 주어진 * MY_URL * 45 줄에'' 내 첫 번째 게시물에서 나는 코드를 편집해야합니다. – aarnoo

+0

'$ tn'은 아마도 false이며, 이는 imagecreatetruecolor() 호출이 실패했음을 나타냅니다. 주어진 매개 변수가 정수가 아니라 부동 소수점 숫자이기 때문에 그것이라고 생각합니다. 다시, [documentation] (http://php.net/imagecreatetruecolor)에서 읽으십시오. – Plenka

+0

나는이 문서를 읽었으나 나는이 경고를 받지만 나는 그것을 발견 할 수 없다 : 경고 : imagecreatefromstring() : 빈 문자열 또는 잘못된 이미지 – aarnoo

관련 문제