【问题标题】:How to save an Imagick converted image from pdf into a variable如何将 Imagick 转换后的图像从 pdf 保存到变量中
【发布时间】:2017-10-29 18:09:08
【问题描述】:

如何将转换后的图像文件从本地目录从 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();

【问题讨论】:

  • 对不起,我对真正的问题感到困惑,因为标题询问保存到变量中?或者在变量中转换后保存pdf?或者从您刚刚保存的变量中获取文件详细信息?还是上述任何一种的混合?您能否进一步说明您实际尝试的失败之处以及您正在寻找的真正结果。
  • 现在它将pdf图像转换为jpg并保存在本地目录中。但我需要在转换后立即将转换后的 jpg 图像的副本保存到变量中。实际上我也想将转换后的 jpg 图像插入数据库。
  • 您可以为此使用 fopen/fread/fclose 函数集,因此您知道您正在提取已保存的实际数据。然后,您将在变量中拥有完整的二进制文件,以按照您的意愿使用。
  • 或者,尝试使用图像 blob:php.net/manual/en/imagick.getimageblob.php
  • 能否请您参考我的代码给我一个示例。对 php 来说很新,而且很难做到

标签: php imagemagick imagick


【解决方案1】:

这可能会有所帮助:

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

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

$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 内存,因此请确保进行一些检查以防止溢出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 2011-05-11
    • 2017-01-22
    • 2021-02-04
    • 2013-12-12
    • 2020-06-28
    • 2021-10-08
    相关资源
    最近更新 更多