-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest7.php
131 lines (119 loc) · 3.17 KB
/
rest7.php
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
<?php
/**
* @author rest7.com
* @license MIT
* @version 0.1
* @link http://github.com/rest7/api Documentation
*/
function _uploadFile7($url, &$fileBody, $fileName)
{
$boundary = '--------------------------' . microtime(1);
$head = "Content-Type: multipart/form-data; boundary=$boundary";
$content = "--$boundary\r\n".
'Content-Disposition: form-data; name="file"; filename="' . $fileName . '"' . "\r\n" .
"Content-Type: application/zip\r\n\r\n".
"$fileBody\r\n";
$content .= "--$boundary--\r\n";
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $head,
'content' => $content,
)
));
$res = @file_get_contents($url, false, $context);
return $res;
}
function _loadImage7($inputImage, &$imageBody, &$imageName, &$imageUrl)
{
$res = get_resource_type($inputImage);
if ($res == 'gd')
{
ob_start();
imagepng($inputImage, NULL, 1, PNG_NO_FILTER);
$imageBody = ob_get_contents();
$imageName = 'image.png';
$imageUrl = false;
ob_end_clean();
return true;
}
if (is_file($inputImage))
{
$imageBody = file_get_contents($inputImage);
$imageName = basename($image);
$imageUrl = false;
return true;
}
if (substr($inputImage, 0, 4) == 'http')
{
$imageName = $imageBody = false;
$imageUrl = $inputImage;
return true;
}
return false;
}
/**
* Checks if an image was upscaled
*
* @param mixed $image Image resource, filename or URL
*
* @return int 1, if image was upscaled, 0 if not upscaled, false when error occured
*
*/
function imageUpscaled7($image)
{
if (!_loadImage7($image, $imageBody, $imageName, $imageUrl)) return false;
if ($imageUrl)
{
$res = @file_get_contents('http://api.rest7.com/v1/image_upscaled.php?url=' . $imageUrl);
}
else
{
$res = _uploadFile7('http://api.rest7.com/v1/image_upscaled.php', $imageBody, $imageName);
unset($imageBody);
}
$data = json_decode($res);
if (@$data->success !== 1)
{
return false;
}
return ($data->is_upscaled) ? 1 : 0;
}
/**
* Converts an image to a different format
*
* @param mixed $image Image resource, filename or URL
* @param string $outputFormat Output format extension, eg. png, jpg, gif
* @param bool $returnURL True returns an URL to the image, False returns the image as string
*
* @return bool true, if image was converted, false when error occured
*
*/
function convertImage7($image, $outputFormat = 'png', $returnURL = false)
{
if (!_loadImage7($image, $imageBody, $imageName, $imageUrl)) return false;
if ($imageUrl)
{
$res = @file_get_contents('http://api.rest7.com/v1/image_convert.php?format=' . $outputFormat . '&url=' . $imageUrl);
}
else
{
$res = _uploadFile7('http://api.rest7.com/v1/image_convert.php?format=' . $outputFormat, $imageBody, $imageName);
unset($imageBody);
}
$data = json_decode($res);
if (@$data->success !== 1)
{
return false;
}
if ($returnURL)
{
return $data->file;
}
$res = @file_get_contents($data->file);
if (strlen($res)<10)
{
return false;
}
return $res;
}