-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageshack.class.php
More file actions
192 lines (173 loc) · 5.6 KB
/
imageshack.class.php
File metadata and controls
192 lines (173 loc) · 5.6 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
require_once('multipost.class.php');
class ImageShackUploader {
/**
* API endpoints
*/
const IMAGESHACK_API_URL = 'http://imageshack.us/upload_api.php';
const IMAGESHACK_VIDEO_API_URL = 'http://render.imageshack.us/upload_api.php';
/**
* Default connection and response timeout
*/
const IMAGESHACK_API_TIMEOUT = 10;
protected $timeout = self::IMAGESHACK_API_TIMEOUT;
protected $developer_key;
protected $cookie;
/**
* Constructs new uploader using specified developer key and optional cookies
* @param string developer_key developer key to use
* @param cookie optional ImageShack cookies
*/
function __construct($developer_key, $cookie = null, $timeout = self::IMAGESHACK_API_TIMEOUT) {
$this->developer_key = $developer_key;
$this->cookie = $cookie;
$this->timeout = $timeout;
}
/**
* Uploads specified file
* @param string file file to upload
* @param string optsize optional resize options, WIDTHxHEIGHT or 'resample'
* @param boolean removeBar optional, if set to true, no information bar is available on generated thumbnail
* @param string tags optional, comma-separated tags
* @param boolean public optional, if specified then image visibility will be set to public or private
* @param string contentType optional, file content type, if not specified we'll try to guess it by extension
* @param string frameFilename optional video frame JPEG image
*/
function upload($file,
$optsize = null,
$removeBar = true,
$tags = null,
$public = null,
$contentType = null,
$frameFilename = null) {
if (!$contentType)
$contentType = $this->detectFileContentType($file["name"]);
if (strpos($contentType, 'image/') === 0)
$endpoint = self::IMAGESHACK_API_URL;
else
$endpoint = self::IMAGESHACK_VIDEO_API_URL;
$params = array();
$params[] = new filepart('fileupload', $file["tmp_name"], basename($file["tmp_name"]), $contentType, 'iso-8859-1');
if ($frameFilename)
$params[] = new filepart('frmupload', $frameFilename, basename($frameFilename), 'image/jpeg', 'iso-8859-1');
if ($optsize) {
$params[] = new stringpart('optimage', 1);
$params[] = new stringpart('optsize', $optsize);
}
if ($tags)
$params[] = new stringpart('tags', $tags);
$params[] = new stringpart('rembar', $removeBar ? 'yes' : 'no');
if ($public !== null)
$params[] = new stringpart('public', $public ? 'yes' : 'no');
if ($this->cookie)
$params[] = new stringpart('cookie', $this->cookie);
$params[] = new stringpart('key', $this->developer_key);
return $this->exec($endpoint, $params);
}
/**
* Transloads URL
* @param url URL to transload
* @param string optsize optional resize options, WIDTHxHEIGHT or 'resample'
* @param boolean removeBar optional, if set to true, no information bar is available on generated thumbnail
* @param string tags optional, comma-separated tags
* @param boolean public optional, if specified then image visibility will be set to public or private
*/
function transload($url, $optsize = null, $removeBar = true, $tags = null, $public = null) {
$contentType = $this->detectUrlContentType($url);
if (!$contentType)
return false;
if (strpos($contentType, 'image/') === 0)
$endpoint = self::IMAGESHACK_API_URL;
else
$endpoint = self::IMAGESHACK_VIDEO_API_URL;
$params = array();
$params[] = new stringpart('url', $url);
if ($optsize) {
$params[] = new stringpart('optimage', 1);
$params[] = new stringpart('optsize', $optsize);
}
if ($tags)
$params[] = new stringpart('tags', $tags);
$params[] = new stringpart('rembar', $removeBar ? 'yes' : 'no');
if ($public)
$params[] = new stringpart('public', $public ? 'yes' : 'no');
if ($this->cookie)
$params[] = new stringpart('cookie', $this->cookie);
$params[] = new stringpart('key', $this->developer_key);
return $this->exec($endpoint, $params);
}
private function detectFileContentType($name) {
$ext = strtolower(substr($name, strrpos($name, '.') + 1));
switch ($ext) {
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'png':
case 'bmp':
case 'gif':
return 'image/' . $ext;
case 'tif':
case 'tiff':
return 'image/tiff';
case 'mp4':
return 'video/mp4';
case 'mov':
return 'video/quicktime';
case '3gp':
return 'video/3gpp';
case 'avi':
return 'video/avi';
case 'wmv':
return 'video/x-ms-wmv';
case 'mkv':
return 'video/x-matroska';
}
return 'application/octet-stream';
}
private function detectUrlContentType($url) {
$ch = @curl_init($url);
if ($ch) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'ImageShack Image Fetcher 1.1');
$response = curl_exec($ch);
$is_err = curl_errno($ch);
if ($is_err) {
@curl_close($ch);
return false;
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
@curl_close($ch);
return false;
}
$ret = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
@curl_close($ch);
return $ret;
}
else
return false;
}
private function exec($endpoint, $request) {
$response = multipost($endpoint,
$request,
$errno,
$errormessage,
$this->timeout);
$xml = @simplexml_load_string($response);
if (!$xml) {
error_log('Failed to parse XML: ' . $response);
return false;
}
if (!@$xml->error && !@$xml->files) {
error_log('Unexpected XML');
return false;
}
return $xml;
}
}
?>