My second code snippet for the day.
Requirements:
- CURL
- PHP5
Usage should be fairly self explanitory
Ill add watermark support to the class soon as well. Just for wjunction :P
Requirements:
- CURL
- PHP5
PHP:
<?
/*
Name: Image Class
Splitice - thewarezscene.org
*/
class image{
var $login = array('user'=>false,'pass'=>false);
private $image = '';
function image($img){
$this->image=$img;
}
function upload_im4ge($image) {
$postParams = array ('v2'=>'1','image' => '@' . $image );
if($this->login['user']&&$this->login['pass']){
$postParams['login_user'] = $this->login['user'];
$postParams['login_pass'] = $this->login['pass'];
$postParams['login'] = true;
}
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, 'http://im4ge.info/api.php' );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_NOPROGRESS, false );
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt ( $ch, CURLOPT_TIMEOUT, 0 );
curl_setopt ( $ch, CURLOPT_VERBOSE, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postParams );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
$postResult = curl_exec ( $ch );
if (! $postResult){
curl_close($ch);
return false;
}
if (curl_errno ( $ch )) {
trigger_error ( 'Unable to upload file: ' . curl_error ( $ch ) );
curl_close($ch);
return false;
}
curl_close ( $ch );
$dd = unserialize($postResult);
return $dd;
}
function set_login($user, $pass){
$this->login['user'] = $user;
$this->login['pass'] = $pass;
}
function upload_img($nocache = false){
$i = $this->image;
if($nocache===false&&substr($i,0,7)=='http://'){
$res = mysql_query('SELECT * FROM image_cache WHERE url="'.mysql_real_escape_string($i).'"');
$row = mysql_fetch_assoc($res);
return unserialize($row['data']);
}
$size = array();
if (! strpos ( $i, '?' )) {
$p = pathinfo ( $i );
$t = tempnam ( '/tmp', 'img_' ) . '.' . $p ['extension'];
$d = file_get_contents ( trim ( $i ) );
if (! $d) {
return array(false,'image not available');
}
file_put_contents ( $t, $d );
$d = $this->upload_im4ge ( $t );
$size = getimagesize($t);
unlink($t);
if($d===false){
return array(false,'upload failed');
}
if($d['error']){
return array(false,'upload failed: '.$d['errormsg']);
}
$t = array ('url' => $i, 'bbcode' => $d );
$i = $t;
}else{
return array(false, 'dont upload');
}
if(!file_get_contents($i['bbcode']['thumbnail'])){
$i['bbcode']['thumbnail'] = $i['bbcode']['image'];
}
$ret = array('thumbnail'=>$i['bbcode']['thumbnail'],'image'=>$i['bbcode']['image'],'link'=>$i['bbcode']['url'],'width'=>$size[0],'height'=>$size[1]);
if($nocache===false&&substr($i,0,7)=='http://'){
mysql_query('INSERT INTO images_cache VALUES("'.mysql_real_escape_string($this->image).'","'.mysql_real_escape_string(serialize($ret)).'")');
}
return $ret;
}
function __toString(){
return $this->image;
}
}
?>
PHP:
$img = new image('local or remote image');
$img->upload_img();