douya2006 2017-10-29 18:09
浏览 207
已采纳

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

How can I save a converted image file from pdf to jpg from local directory into a variable? Does it have any function that give such detail like $_FILES when we upload file in php?

$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();
  • 写回答

1条回答 默认 最新

  • douao3063 2017-10-29 19:11
    关注

    This could be helpful to start with:

    For saving the file, try instead to use file_put_contents (writeImage can be flakey).

    Then to use THAT converted image (from the loop) in variables and saving to database, you can get its full blob into an array to use after you are done 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
    }
    

    Note if you have a ton of images, and they are large, this can eat up php memory, so make sure you have some checks to prevent overrun.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法