Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将Imagick转换后的图像从pdf保存到变量中_Php_Imagemagick_Imagick - Fatal编程技术网

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

Php 如何将Imagick转换后的图像从pdf保存到变量中,php,imagemagick,imagick,Php,Imagemagick,Imagick,如何将转换后的图像文件从本地目录从pdf保存到jpg,并保存到变量中?当我们在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(); $

如何将转换后的图像文件从本地目录从pdf保存到jpg,并保存到变量中?当我们在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();

这可能有助于从以下方面开始:

要保存文件,请尝试使用文件内容(writeImage可以是片状的)

然后,要在变量中使用转换后的图像(来自循环)并保存到数据库中,可以将其完整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内存,因此请确保您进行了一些检查以防止溢出。

抱歉,我对真正的问题感到困惑,因为标题询问如何保存到变量中?或者在变量中转换后保存pdf?或者从刚刚保存的变量中获取文件详细信息?还是上述任何一种的混合物?请你进一步澄清一下你实际尝试的失败之处,以及你想要的真正结果。现在,它将pdf图像转换为jpg并保存在本地目录中。但我需要在转换后立即将转换后的jpg图像的副本保存到一个变量中。实际上,我还想将转换后的jpg图像插入数据库中。您可以使用fopen/fread/fclose函数集来实现这一点,这样您就知道您正在提取保存的实际数据。然后,你将在一个变量中使用完整的二进制文件,以实现你的愿望。或者,试着使用图像blob:你能给我举个例子,参考我的代码吗。对php来说非常陌生,而且有点难做