douquejituan938904 2019-08-13 08:32
浏览 113

使用yandex磁盘将Spatie pdf加入图像转换器

I have a problem with Joining 2 functions, So I am use https://github.com/spatie/pdf-to-image - this function for convert uploaded pdf(s) to image(s). this is working fine, but this function uploads converted images to the local directory, but i want to change it and save converted images to the yandex disk not a local directory. my function with curl for upload files to yandex disk working fine too, but i dont know how to join this 2 functions,

I want to change $pdf->setPage($pageNumber)->saveImage($pageNumber . '.jpg'); to uploadtoyandexdisk("folder", "", $pageNumber . '.jpg'); for uploading to yandex disk, can you help ?

//edited code


    if(!empty($_FILES["fileToUpload"])){

error_reporting(E_ALL);
ini_set("display_errors", 1);

function uploadtoyandexdisk($folder, $file, $name) {

    if(empty($file)){
        $filename = $name;
        $filepath = $_FILES["fileToUpload"]["tmp_name"];
    } else{
        $filename = $name;
        $filepath = $file;
    }

        $yandexusername = "username";
        $yandexpassword = "password";

        $credentials = array($yandexusername,$yandexpassword);

        $filesize = filesize($filepath);
        $fh = fopen($filepath, 'r');
        $remoteUrl = 'https://webdav.yandex.ru/uploads/'.$folder.'/';
        $ch = curl_init($remoteUrl . $filename);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_USERPWD, implode(':', $credentials));
        curl_setopt($ch, CURLOPT_PUT, true);
        curl_setopt($ch, CURLOPT_INFILE, $fh);
        curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        $response=curl_exec($ch);
        fclose($fh);

        echo $response;

}

include "../inc/extensions/pdftoimage/Pdf.php";

                        $pdf = new Spatie\PdfToImage\Pdf($_FILES["fileToUpload"]["tmp_name"]);
                        foreach (range(1, $pdf->getNumberOfPages()) as $pageNumber) {
                            $pdf->setCompressionQuality(50);
                            $pdf->setPage($pageNumber)
                                ->saveImage($pageNumber . '.jpg');

                                $imgdata = $pdf->setPage($pageNumber)->getImageData($pageNumber . '.jpg');

                                uploadtoyandexdisk("uploads", $imgdata, $pageNumber . '.jpg');


                        }

}
  • 写回答

1条回答 默认 最新

  • dongzhan1383 2019-08-13 09:36
    关注

    An easy look at the source code helps to solve the problem: while saveImage is used to save the converted data to a file, you can call getImageData with the same argument to get the raw data. If you post that raw data to the Yandex server instead of reading data from a file, you should be done

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛