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
4

1 回答 1

0

阅读函数的文档imagejpeg()您调用它的方式将直接将图像输出到输出缓冲区。该变量$tn仅包含资源标识符。您可以在调用周围使用ob_start()and ob_get_flush()文档imagejpg()来获取实际图像,而无需将其写入文件:

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

但是,建议将图像实际写入文件系统并将其路径存储在数据库中。这样,图像可以作为静态文件提供,这将在各个方面提高性能。

于 2014-04-08T19:34:06.850 回答