【问题标题】:I will set an thumbnail in the database我将在数据库中设置缩略图
【发布时间】:2014-04-08 19:26:49
【问题描述】:

我有一个 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

【问题讨论】:

  • 解释它不起作用
  • thumb 的 BLOB 是 0b,因此他没有向数据库发送缩略图的信息。这个脚本的某个地方出了问题,但我没找到。
  • SQL 注入向量有很多。确保在将 $_POST 变量连接到 SQL 查询之前对其进行清理。
  • 好的,我会改变这个,我现在已经放置了错误并且我已经在第一篇文章中更新了我的代码。其他值在数据库中设置好

标签: php mysqli blob thumbnails gdlib


【解决方案1】:

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

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

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

【讨论】:

  • 我已经在我的脚本中编辑了这个,如果我从拇指下载 BLOB 并用我的文本编辑器打开它,我会看到 BLOB 的内容,但这是一个警告:Warning: imagejpeg() expects parameter 1 to be resource, boolean given in *MY_URL* on line 45 在我的第一篇文章中,我已编辑代码。
  • 这表明$tn 可能为假,这反过来又表明imagecreatetruecolor() 调用失败。我认为这是因为给定的参数不是整数,而是浮点数。请再次阅读documentation
  • 我已阅读此文档,我也收到此警告,但我不知道原因:警告:imagecreatefromstring():空字符串或无效图像
  • 我现在在我的第一篇文章中添加了一些新代码。现在我收到四个警告: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你知道我该如何解决这个问题吗?
  • 您当前的脚本有很多问题。 addslashes()base64_decode() 要求什么?我不会为您的问题提供完整的解决方案,因为您描述的错误很容易调试。只需使用常识和 RTFM。
猜你喜欢
  • 2012-07-09
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 2018-06-12
  • 2013-11-11
  • 2021-05-08
  • 2019-09-18
  • 2013-07-15
相关资源
最近更新 更多