Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Cloudinary
==========

This repository is a fork from the actual Cloudinary PHP SDK

Cloudinary is a cloud service that offers a solution to a web application's entire image management pipeline.

Easily upload images to the cloud. Automatically perform smart image resizing, cropping and conversion without installing any complex software. Integrate Facebook or Twitter profile image extraction in a snap, in any dimension and style to match your website’s graphics requirements. Images are seamlessly delivered through a fast CDN, and much much more.
Expand All @@ -11,14 +13,16 @@ Cloudinary provides URL and HTTP based APIs that can be easily integrated with a

For PHP, Cloudinary provides an extension for simplifying the integration even further.

Improvements

1. Made code changes to implement cURL instead of HTTP Pecl for Upload.

## Setup ######################################################################

Download cloudinary_php from [here](https://github.com/downloads/cloudinary/cloudinary_php/cloudinary_php.tar.gz)
Download cloudinary_php from [here](https://github.com/abishekrsrikaanth/cloudinary_php/tarball/master)

*Note: cloudinary_php require PHP 5.3*

*Note: cloudinary_php depends on the HTTP pecl for upload.*

## Try it right away

Sign up for a [free account](https://cloudinary.com/users/register/free) so you can try out image transformations and seamless image delivery through CDN.
Expand Down
336 changes: 174 additions & 162 deletions uploader.php
Original file line number Diff line number Diff line change
@@ -1,169 +1,181 @@
<?php
namespace Cloudinary {

class Uploader {
public static function build_upload_params(&$options) {
$params = array("timestamp" => time(),
"transformation" => \Cloudinary::generate_transformation_string($options),
"public_id" => \Cloudinary::option_get($options, "public_id"),
"callback" => \Cloudinary::option_get($options, "callback"),
"format" => \Cloudinary::option_get($options, "format"),
"backup" => \Cloudinary::option_get($options, "backup"),
"type" => \Cloudinary::option_get($options, "type"),
"tags" => implode(",", \Cloudinary::build_array(\Cloudinary::option_get($options, "tags"))));
if (isset($options["eager"])) {
$eager = array();
foreach (\Cloudinary::build_array($options["eager"]) as $trans) {
$transformation = $trans;
if ($transformation) array_push($eager, \Cloudinary::generate_transformation_string($transformation));
}
$params["eager"] = implode("|", $eager);
}
return array_filter($params);
}

public static function upload($file, $options=array()) {
$params = Uploader::build_upload_params($options);
return Uploader::call_api("upload", $params, $options, $file);
}

public static function destroy($public_id, $options=array()) {
$params = array(
"timestamp" => time(),
"type" => \Cloudinary::option_get($options, "type"),
"public_id" => $public_id
);
return Uploader::call_api("destroy", $params, $options);
}

// options may include 'exclusive' (boolean) which causes clearing this tag from all other resources
public static function add_tag($tag, $public_ids = array(), $options=array()) {
$exclusive = \Cloudinary::option_get("exclusive");
$command = $exclusive ? "set_exclusive" : "add";
return Uploader::call_tags_api($tag, $command, $public_ids, $options);
}

public static function remove_tag($tag, $public_ids = array(), $options=array()) {
return Uploader::call_tags_api($tag, "remove", $public_ids, $options);
}

public static function replace_tag($tag, $public_ids = array(), $options=array()) {
return Uploader::call_tags_api($tag, "replace", $public_ids, $options);
}

public static function call_tags_api($tag, $command, $public_ids = array(), &$options=array()) {
$params = array(
"timestamp" => time(),
"tag" => $tag,
"public_ids" => \Cloudinary::build_array($public_ids),
"command" => $command
);
return Uploader::call_api("tags", $params, $options);
}

private static $TEXT_PARAMS = array("public_id", "font_family", "font_size", "font_color", "text_align", "font_weight", "font_style", "background", "opacity", "text_decoration");

public static function text($text, $options=array()) {
$params = array("timestamp" => time(), "text" => $text);
foreach (Uploader::$TEXT_PARAMS as $key) {
$params[$key] = \Cloudinary::option_get($options, $key);
}
return Uploader::call_api("text", $params, $options);
}

public static function call_api($action, $params, $options=array(), $file = NULL) {
$return_error = \Cloudinary::option_get($options, "return_error");
$api_key = \Cloudinary::option_get($options, "api_key", \Cloudinary::config_get("api_key"));
if (!$api_key) throw new \InvalidArgumentException("Must supply api_key");
$api_secret = \Cloudinary::option_get($options, "api_secret", \Cloudinary::config_get("api_secret"));
if (!$api_secret) throw new \InvalidArgumentException("Must supply api_secret");

$params["signature"] = \Cloudinary::api_sign_request($params, $api_secret);
$params["api_key"] = $api_key;

# Remove blank parameters
$params = array_filter($params);

$api_url = \Cloudinary::cloudinary_api_url($action, $options);

$request = new \HttpRequest($api_url, \HttpRequest::METH_POST);
if ($file) {
if (!preg_match('/^https?:/', $file)) {
$request->addPostFile("file", $file);
} else {
$params["file"] = $file;
}
}
$request->addPostFields($params);
$request->setOptions(array("timeout"=>60));
$request->send();
$code = $request->getResponseCode();
$response_data = $request->getResponseBody();
if ($code != 200 && $code != 400 && $code != 500) {
throw new \Exception("Server returned unexpected status code - " . $code . " - " . $response_data);
}
$result = json_decode($response_data, TRUE);
if ($result == NULL) {
throw new \Exception("Error parsing server response (" . $code . ") - " . $response_data);
}
if (isset($result["error"])) {
if ($return_error) {
$result["error"]["http_code"] = $code;
} else {
throw new \Exception($result["error"]["message"]);
}
}
return $result;
}
}
class Uploader {
public static function build_upload_params(&$options)
{
$params = array("timestamp" => time(),
"transformation" => \Cloudinary::generate_transformation_string($options),
"public_id" => \Cloudinary::option_get($options, "public_id"),
"callback" => \Cloudinary::option_get($options, "callback"),
"format" => \Cloudinary::option_get($options, "format"),
"backup" => \Cloudinary::option_get($options, "backup"),
"type" => \Cloudinary::option_get($options, "type"),
"tags" => implode(",", \Cloudinary::build_array(\Cloudinary::option_get($options, "tags"))));
if (isset($options["eager"])) {
$eager = array();
foreach (\Cloudinary::build_array($options["eager"]) as $trans) {
$transformation = $trans;
if ($transformation) array_push($eager, \Cloudinary::generate_transformation_string($transformation));
}
$params["eager"] = implode("|", $eager);
}
return array_filter($params);
}

public static function upload($file, $options = array())
{
$params = Uploader::build_upload_params($options);
return Uploader::call_api("upload", $params, $options, $file);
}

public static function destroy($public_id, $options = array())
{
$params = array(
"timestamp" => time(),
"type" => \Cloudinary::option_get($options, "type"),
"public_id" => $public_id
);
return Uploader::call_api("destroy", $params, $options);
}

// options may include 'exclusive' (boolean) which causes clearing this tag from all other resources
public static function add_tag($tag, $public_ids = array(), $options = array())
{
$exclusive = \Cloudinary::option_get("exclusive");
$command = $exclusive ? "set_exclusive" : "add";
return Uploader::call_tags_api($tag, $command, $public_ids, $options);
}

public static function remove_tag($tag, $public_ids = array(), $options = array())
{
return Uploader::call_tags_api($tag, "remove", $public_ids, $options);
}

public static function replace_tag($tag, $public_ids = array(), $options = array())
{
return Uploader::call_tags_api($tag, "replace", $public_ids, $options);
}

public static function call_tags_api($tag, $command, $public_ids = array(), &$options = array())
{
$params = array(
"timestamp" => time(),
"tag" => $tag,
"public_ids" => \Cloudinary::build_array($public_ids),
"command" => $command
);
return Uploader::call_api("tags", $params, $options);
}

private static $TEXT_PARAMS = array("public_id", "font_family", "font_size", "font_color", "text_align", "font_weight", "font_style", "background", "opacity", "text_decoration");

public static function text($text, $options = array())
{
$params = array("timestamp" => time(), "text" => $text);
foreach (Uploader::$TEXT_PARAMS as $key) {
$params[$key] = \Cloudinary::option_get($options, $key);
}
return Uploader::call_api("text", $params, $options);
}

public static function call_api($action, $params, $options = array(), $file = NULL)
{
$return_error = \Cloudinary::option_get($options, "return_error");
$api_key = \Cloudinary::option_get($options, "api_key", \Cloudinary::config_get("api_key"));
if (!$api_key) throw new \InvalidArgumentException("Must supply api_key");
$api_secret = \Cloudinary::option_get($options, "api_secret", \Cloudinary::config_get("api_secret"));
if (!$api_secret) throw new \InvalidArgumentException("Must supply api_secret");

$params["signature"] = \Cloudinary::api_sign_request($params, $api_secret);
$params["api_key"] = $api_key;

# Remove blank parameters
$params = array_filter($params);

$api_url = \Cloudinary::cloudinary_api_url($action, $options);
$ch = curl_init($api_url);

if ($file) {
$params["file"] = $file;
}

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response_data = $response;

curl_close($ch);
if ($code != 200 && $code != 400 && $code != 500) {
throw new \Exception("Server returned unexpected status code - " . $code . " - " . $response_data);
}
$result = json_decode($response_data, TRUE);
if ($result == NULL) {
throw new \Exception("Error parsing server response (" . $code . ") - " . $response_data);
}
if (isset($result["error"])) {
if ($return_error) {
$result["error"]["http_code"] = $code;
} else {
throw new \Exception($result["error"]["message"]);
}
}
return $result;
}
}
}

namespace {

function cl_image_upload_tag($field, $options=array()) {
$html_options = Cloudinary::option_get($options, "html", array());
if (!isset($options["resource_type"])) $options["resource_type"] = "auto";
$cloudinary_upload_url = Cloudinary::cloudinary_api_url("upload", $options);

$api_key = Cloudinary::option_get($options, "api_key", Cloudinary::config_get("api_key"));
if (!$api_key) throw new \InvalidArgumentException("Must supply api_key");
$api_secret = Cloudinary::option_get($options, "api_secret", Cloudinary::config_get("api_secret"));
if (!$api_secret) throw new \InvalidArgumentException("Must supply api_secret");

$params = Cloudinary\Uploader::build_upload_params($options);
$params["signature"] = Cloudinary::api_sign_request($params, $api_secret);
$params["api_key"] = $api_key;

# Remove blank parameters
$params = array_filter($params);

$tag_options = array_merge($html_options, array("type"=>"file", "name"=>"file",
"data-url"=>$cloudinary_upload_url,
"data-form-data"=>json_encode($params),
"data-cloudinary-field"=>$field,
"class" => implode(" ", array($html_options["class"], "cloudinary-fileupload"))
));
return '<input ' . Cloudinary::html_attrs($tag_options) . '/>';
}

function cl_form_tag($callback_url, $options=array()) {
$form_options = Cloudinary::option_get($options, "form", array());

$options["callback_url"] = $callback_url;
$params = Cloudinary\Uploader::build_upload_params($options);
$params["signature"] = Cloudinary::api_sign_request($params, Cloudinary::config_get("api_secret"));
$params["api_key"] = Cloudinary::config_get("api_key");

$api_url = Cloudinary::cloudinary_api_url("upload", $options);

$form = "<form enctype='multipart/form-data' action='" . $api_url . "' method='POST' " . Cloudinary::html_attrs($form_options) . ">\n";
foreach ($params as $key=>$value) {
$form .= "<input " . Cloudinary::html_attrs(array("name"=>$key, "value"=>$value, "type"=>"hidden")) . "/>\n";
}
$form .= "</form>\n";

return $form;
}
}

?>
function cl_image_upload_tag($field, $options = array())
{
$html_options = Cloudinary::option_get($options, "html", array());
if (!isset($options["resource_type"])) $options["resource_type"] = "auto";
$cloudinary_upload_url = Cloudinary::cloudinary_api_url("upload", $options);

$api_key = Cloudinary::option_get($options, "api_key", Cloudinary::config_get("api_key"));
if (!$api_key) throw new \InvalidArgumentException("Must supply api_key");
$api_secret = Cloudinary::option_get($options, "api_secret", Cloudinary::config_get("api_secret"));
if (!$api_secret) throw new \InvalidArgumentException("Must supply api_secret");

$params = Cloudinary\Uploader::build_upload_params($options);
$params["signature"] = Cloudinary::api_sign_request($params, $api_secret);
$params["api_key"] = $api_key;

# Remove blank parameters
$params = array_filter($params);

$tag_options = array_merge($html_options, array("type" => "file", "name" => "file",
"data-url" => $cloudinary_upload_url,
"data-form-data" => json_encode($params),
"data-cloudinary-field" => $field,
"class" => implode(" ", array($html_options["class"], "cloudinary-fileupload"))
));
return '<input ' . Cloudinary::html_attrs($tag_options) . '/>';
}

function cl_form_tag($callback_url, $options = array())
{
$form_options = Cloudinary::option_get($options, "form", array());

$options["callback_url"] = $callback_url;
$params = Cloudinary\Uploader::build_upload_params($options);
$params["signature"] = Cloudinary::api_sign_request($params, Cloudinary::config_get("api_secret"));
$params["api_key"] = Cloudinary::config_get("api_key");

$api_url = Cloudinary::cloudinary_api_url("upload", $options);

$form = "<form enctype='multipart/form-data' action='" . $api_url . "' method='POST' " . Cloudinary::html_attrs($form_options) . ">\n";
foreach ($params as $key => $value) {
$form .= "<input " . Cloudinary::html_attrs(array("name" => $key, "value" => $value, "type" => "hidden")) . "/>\n";
}
$form .= "</form>\n";

return $form;
}
}