dimanche 19 janvier 2020

I keep getting "Unsupported image type. GD driver is only able to decode JPG, PNG, GIF or W ebP files" when uploading images to my site

Basically, I have a function that strips images from google drive and upload them into my server. However, I keep getting the error "Unsupported image type. GD driver is only able to decode JPG, PNG, GIF or W ebP files." on random. Is there something I'm doing wrong here? Below is the code;

public function addPhoto($pictureGoogleDriveUrl, $productImport) {

        //delete tmp pic

        $image_path = \Config::get('constants.ROOT_PATH') . '/public/product_uploads/tmp';
        if(File::exists($image_path)) {
            File::delete($image_path);
        }

        $fileId = str_replace("https://drive.google.com/open?id=", "", trim($pictureGoogleDriveUrl));
        $fileId = str_replace("/view?usp=sharing", "", trim($fileId));
        $fileId = str_replace("/view", "", trim($fileId));
        $fileId = str_replace("https://drive.google.com/file/d/", "", trim($fileId));

        $ch = curl_init("https://docs.google.com/uc?id=" . $fileId);
        $fp = fopen(\Config::get('constants.ROOT_PATH') . '/public/product_uploads/tmp/product_photo.jpeg', 'wb');

        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($ch);

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close($ch);
        fclose($fp);

        if ($httpCode != 404) {
            $photo = new UploadedFile(\Config::get('constants.ROOT_PATH') . '/public/product_uploads/tmp/product_photo.jpeg', 'product_photo.jpeg', 'image/jpeg', null, null, true);

            $img = Image::make(\Config::get('constants.ROOT_PATH') . '/public/product_uploads/tmp/product_photo.jpeg');
            $img->fit(400);
            $img->save(\Config::get('constants.ROOT_PATH') . '/public/product_uploads/tmp/product_photo.jpeg');

            (new \App\AddPhotoToProduct($productImport, $photo))->saveInConsole();
        }
        sleep(5);
    }

Note that, 1) around 30 images are uploaded at a time. 2) The error is random, it can break on the first upload, sometimes the second, sometimes the last.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2THf7Su
via IFTTT

Aucun commentaire:

Enregistrer un commentaire