PHP 将从 imagecopy 创建的图像保存为 blob

PHP save image created from imagecopy as blob

我正在尝试合并两张 PNG 图片。我正在使用 imagecopy 函数,如下所示:

imagecopy($dest, $src, $dest_x / 2 - $src_x / 2, $dest_y / 2 - $src_y / 2, 0, 0, $src_x, $src_y);

现在,我想将这张合并后的照片作为 blob 保存在我的数据库中,但代码不起作用。我的图片保存为 14Bytes 对象,而普通图片大约有 100KB.

 try
  {
      $stmt = $user->get_db()->prepare("INSERT INTO photos(id_user, src, date) VALUES(:id_user, :src, :date)");


      $stmt->bindparam(":id_user", $_SESSION['user_id']);
      $stmt->bindparam(":src", $dest);
      $stmt->bindparam(":date", date("Y-m-d H:i:s"));
      $stmt->execute();

  }
   catch(PDOException $e)
   {
        echo $e->getMessage();
   }

第一个是保存成功的图片(普通png图片)。
第二张是合成图,上传错了
问题是当我上传合并的照片时,因为它没有正确上传。

我怀疑您的问题是传递的 $dest 参数不是实际图像。

imagecopy:

之后尝试这样的事情
ob_start();
imagepng($dest);
$image_blob = ob_get_clean();

这应该将图像创建到变量中。

此外,尝试将第 3 个参数 PDO::PARAM_LOB 添加到 bind,即:

$stmt->bindparam(":src", $image_blob, PDO::PARAM_LOB)

您可以阅读更多关于 PDO Large Objects 在这种情况下您应该使用它们:

Large typically means "around 4kb or more"

Large objects can be either textual or binary in nature.

希望对您有所帮助。