Upload Image into imgbb.com

hunter_1225

Hardy Smith

Posted on August 7, 2024

Upload Image into imgbb.com
  $uploadedImageUrls = [
            $request->license_verification,
            $request->driver_photo,
            $request->car_photo
        ];

        $savedImgUrls = array(); // Initialize array to store the uploaded image URLs

        foreach ($uploadedImageUrls as $base64Image) {
            $ch = curl_init(); // Initialize cURL session for each image upload

            // Set the unique image name for upload
            $uniqueImageName = uniqid() . '_image.png';

            // Set the cURL options
            curl_setopt($ch, CURLOPT_URL, 'https://api.imgbb.com/1/upload');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, array(
                'key' => "cfdb0e89363c14687341dbc25d1e1d43",
                'image' => $base64Image, // Use the base64-encoded image data
                'name' => $uniqueImageName // Set the unique image name for upload
            ));

            // Execute the cURL request
            $response = curl_exec($ch);

            // Handle the API response and extract the image URL
            $responseData = json_decode(
                $response,
                true
            );
            if (isset($responseData['data']['url'])) {
                $uploadedImageUrl = $responseData['data']['url'];
                $savedImgUrls[] = $uploadedImageUrl;
            } else {
                // Handle the case where the 'data' key is undefined
                return response()->json([
                    'error' => 'Choose correct image.',
                    'statusCode' => 400
                ], 400);
            }

            curl_close($ch); // Close cURL session after each request
        }
Enter fullscreen mode Exit fullscreen mode

In this code $response has same url every loop. How can solve thi

đź’– đź’Ş đź™… đźš©
hunter_1225
Hardy Smith

Posted on August 7, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related