0

我在加入 2 个函数时遇到问题,所以我使用https://github.com/spatie/pdf-to-image - 此函数用于将上传的 pdf 转换为图像。这工作正常,但是此功能将转换后的图像上传到本地目录,但我想更改它并将转换后的图像保存到 yandex 磁盘而不是本地目录。我的 curl 用于将文件上传到 yandex 磁盘的功能也可以正常工作,但我不知道如何加入这两个功能,

我想改成$pdf->setPage($pageNumber)->saveImage($pageNumber . '.jpg');上传uploadtoyandexdisk("folder", "", $pageNumber . '.jpg');到yandex磁盘,你能帮忙吗?

//修改后的代码


    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');


                        }

}
4

1 回答 1

0

简单查看源代码有助于解决问题:虽然saveImage用于将转换后的数据保存到文件中,但您可以getImageData使用相同的参数调用以获取原始数据。如果您将原始数据发布到 Yandex 服务器而不是从文件中读取数据,您应该完成

于 2019-08-13T09:36:57.000 回答