0

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


                        }

}
apokryfos
  • 38,771
  • 9
  • 70
  • 114

1 Answers1

0

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

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
  • i added this line to my code $imgdata = $pdf->setPage($pageNumber)->getImageData($pageNumber . '.jpg'); but not working too. i edited full source of the code above – Cabrail Kerimov Aug 13 '19 at 10:07
  • Well, obviously you need to perform some more calls: the given code still uses `fopen` to read something from a file instead of using the data provided in `$imgdata` – Nico Haase Aug 13 '19 at 13:17