如何将 Imagick 转换后的图像从 pdf 保存到变量中

How to save an Imagick converted image from pdf into a variable

如何将本地目录中从pdf转换为jpg的图像文件保存到变量中?当我们在php上传文件时,它有没有提供像$_FILES这样的细节的功能?

$target_dir="$media_dir/";
$target_file= $target_dir .  basename($_FILES["file"]["name"]);

move_uploaded_file($_FILES['file']['tmp_name'], $target_file);
$image= new imagick();
$image->readImage($target_file);
$image->setImageFormat('jpg');

$imgname= basename($target_file,".pdf");

//converting pdf to jpg image and saving in local directory

foreach($imagick as $i=>$imagick) 
{ 
  $image->writeImage($target_dir . $imgname . ($i+1) ." of ". ".jpg"); 
} 

$image->clear();

这可能有助于开始:

为了保存文件,请尝试使用 file_put_contents(writeImage 可以是 flakey)。

然后在变量中使用转换后的图像(来自循环)并保存到数据库中,您可以将其完整的 blob 放入数组中以在完成后使用 saving/clearing。

$imageblobs = array();
foreach( ... ) {
    file_put_contents($target_dir . $imgname . ($i+1) .'_of_'. $page .'.jpg', $image);
    // $image is the Imagick object for the image you are saving

    $imageblobs[] = $image->getImageBlob();
    // OR you can skip putting it into an array, and just save direct to db right here
}

// now you can use each 'blob' variable you setup before you cleared it
foreach($imageblobs as $imageblob) {
    // save $imageblob to db blob column
    // or do other things with it
}

请注意,如果您有大量图像,而且它们很大,这会占用 php 内存,因此请确保进行一些检查以防止溢出。