Skip to content

Instantly share code, notes, and snippets.

@sergejmueller
Created February 16, 2019 19:48
Show Gist options
  • Save sergejmueller/aa15e7744cc1762fff5e3740bd3d33cd to your computer and use it in GitHub Desktop.
Save sergejmueller/aa15e7744cc1762fff5e3740bd3d33cd to your computer and use it in GitHub Desktop.
Picture upload to Cloudinary without bloated Cloudinary PHP-SDK.
<?php
// Cloudinary API
define('CLOUDINARY_API_KEY', 'XXXXXXXX');
define('CLOUDINARY_SECRET_KEY', 'XXXXXXXX');
define('CLOUDINARY_API_URL', 'https://api.cloudinary.com/v1_1/XXXXXXXX/image/upload');
// Image handling
$path = 'picture.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
// Cloudinary options
$args = array(
'invalidate' => true,
'public_id' => 'picture',
'timestamp' => time()
);
// Request signature
$signature = sha1(http_build_query($args) . CLOUDINARY_SECRET_KEY);
// Request attributes
$args = array_merge(
$args,
array(
'file' => $base64,
'api_key' => CLOUDINARY_API_KEY,
'signature' => $signature
)
);
$ch = curl_init(CLOUDINARY_API_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
die($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment