Skip to content

Instantly share code, notes, and snippets.

@icyleaf
Created May 6, 2010 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save icyleaf/391760 to your computer and use it in GitHub Desktop.
Save icyleaf/391760 to your computer and use it in GitHub Desktop.
img.ly api package for kohana v3
<?php
/**
* img.ly class for Kohana v3.0.x
*
* @author icyleaf <icyleaf.cn@gmail.com>
* @link http://icyleaf.com
* @version 0.1
* @license http://www.opensource.org/licenses/bsd-license.php
*/
class imgly {
// single instance
private static $_instance = array();
// twitter account
private $twitter = array();
// throw errors
private $errors = array();
/**
* Instance
* @param string $username - twitter username
* @param string $password - twitter password
* @return object imgly
*/
public static function instance($username = NULL, $password = NULL)
{
$twitter = array(
'username' => $username,
'password' => $password,
);
$checksum = md5(serialize($twitter));
if ( ! isset(imgly::$_instance[$checksum]))
{
imgly::$_instance[$checksum] = new imgly($twitter);
}
return imgly::$_instance[$checksum];
}
/**
* Init
* @param array $twitter - twitter account
* @return object imgly
*/
private function __construct(Array $twitter = NULL)
{
$this->twitter = $twitter;
return $this;
}
/**
* Setup Twitter account
* @param string $username - twitter username
* @param string $password - twitter password
* @return object imgly
*/
public function setup($username, $password)
{
$this->twitter = array(
'username' => $username,
'password' => $password,
);
return $this;
}
/**
* Update Photo with send optional message to twitter
* @param binary $media_data
* @param string $message - default is NULL
*/
public function upload($media_data, $message = NULL)
{
$data = array(
'media' => '@'.$media_data,
'message' => stripslashes($message),
);
if (empty($message))
{
$upload_url = 'http://img.ly/api/upload';
}
else
{
$upload_url = 'http://img.ly/api/uploadAndPost';
}
$data = array_merge($data, $this->twitter);
$result = $this->_http_post($upload_url, $data);
$element = new SimpleXMLElement($result);
$status = (string) $element['stat'];
if ($status == 'ok')
{
return $element->mediaurl;
}
else
{
$error = $element->err;
$this->errors = array(
'code' => (string) $error['code'],
'msg' => (string) $error['msg'],
);
return FALSE;
}
}
/**
* Show image with img.ly url
* @param string url - iml.ly url
* @param string $format - available in mini|thumb|medium|large|full, default is thumb
* @return string image url with HTML entry
*/
public function show($url, $format = 'thumb', $anchor = FALSE)
{
$original = 'http://img.ly/';
$imgly_id = substr($url, strlen($original));
$image_url = 'http://img.ly/show/'.$format.'/'.$imgly_id;
$output = HTML::image($image_url);
if ($anchor)
{
$output = HTML::anchor($url, $output);
}
return $output;
}
/**
* Response Errors Message
* @return array response errors
*/
public function errors()
{
return $this->errors;
}
/**
* HTTP Post Method
* @param string $url
* @param array $data
* @return string response content
* @throws Exception
*/
private function _http_post($url, Array $data = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$content = curl_exec($ch);
//Wrap the error reporting in an exception
if($content === FALSE)
{
throw new Exception("Curl Error: ".curl_error($ch));
}
curl_close($ch);
return $content;
}
}
@icyleaf
Copy link
Author

icyleaf commented May 6, 2010

// controller
public function action_index()
{
    $imgly = imgly::instance()->setup('username', 'p@ssword');

    if ($_FILES)
    {
        if (is_uploaded_file($_FILES['media_data']['tmp_name']))
        {
            $file_name = $_FILES['media_data']['name'];
            $file_size = $_FILES['media_data']['size'];
            $file_data = $_FILES['media_data']['tmp_name'];

            $result = $imgly->upload($file_data);
            // img.ly url. example: http://img.ly/3x45
            echo $result;
        }
    }

    echo Form::open(NULL, array('enctype' => 'multipart/form-data'));
    echo Form::file('media_data');
    echo Form::submit(NULL, 'Upload');
    echo Form::close();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment