-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_upload.php
More file actions
62 lines (49 loc) · 1.87 KB
/
php_upload.php
File metadata and controls
62 lines (49 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
// Custom error handler to suppress Cloudinary deprecation warnings
set_error_handler(function($severity, $message, $file, $line) {
// Suppress deprecation warnings from Cloudinary SDK
if ($severity === E_DEPRECATED && strpos($file, 'cloudinary') !== false) {
return true; // Don't display the error
}
return false; // Let other errors through
});
require_once __DIR__ . '/vendor/autoload.php';
use Dotenv\Dotenv;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Cloudinary;
use Cloudinary\Tag\ImageTag;
use Cloudinary\Transformation\Resize;
use Cloudinary\Api\Upload\UploadApi;
// Load .env file
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Initialize Configuration
$config = new Configuration($_ENV['CLOUDINARY_URL']);
$cld = new Cloudinary($config);
// Upload the image
$upload = new UploadApi($config);
try {
// Upload the first image
$response = $upload->upload('images/people-walking.jpg');
// Print the response as a pretty JSON
echo "<pre>" . json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "</pre>";
// Array of image paths
$imagePaths = [
'images/dress.jpg',
'images/drinks.jpg',
'images/fruit.jpg',
'images/shoes.jpg'
];
foreach ($imagePaths as $imagePath) {
// Upload the image
$response = $upload->upload($imagePath);
// Print the secure URL from the response
echo "<pre>***** Uploaded $imagePath: " . $response['secure_url'] . "</pre>";
}
$response = $upload->upload("https://raw.githubusercontent.com/cloudinary-devs/cld-docs-assets/refs/heads/main/assets/images/people-walking.jpg");
// Print the secure URL from the response
echo "<pre>***** Uploaded remote image: " . $response['secure_url'] . "</pre>";
} catch (Exception $e) {
echo "<pre>Error: " . $e->getMessage() . "</pre>";
}
?>