简体   繁体   中英

PHP save image created from imagecopy as blob

I'm trying to merge two PNG images. I'm using the imagecopy function like this:

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

Now, I want to save this merged photo as a blob in my DB, but the code doesn't work. My image is saved as a 14Bytes object when a normal pic has about 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();
   }

DB中的图像

The first one is a successfully saved imaged (normal png image).
The second one is the merged imaged, wrongly uploaded.
The problem is when I upload the merged photo because it is not correctly uploaded.

I suspect your issue is that passing the $dest parameter is not an actual image.

Try something like this after your imagecopy :

ob_start();
imagepng($dest);
$image_blob = ob_get_clean();

This should create the image into the variable.

Also, try adding 3rd parameter PDO::PARAM_LOB to bind , ie:

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

You can read more about PDO Large Objects you should use them in this case:

Large typically means "around 4kb or more"

Large objects can be either textual or binary in nature.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM