forked from iwangq/FastDFS_Imagick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
163 lines (153 loc) · 3.58 KB
/
upload.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
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
<?PHP
/**
* FastDFS + imagick图片上传服务
* @Author Uxin <iwangq@gmail.com><jorygong@gmail.com>
* @Modify 2013-05-29 21:12:21
*
* 通过POST方式上传图片或图片URL,本程序返回存储后的图片路径和尺寸。
* @param app 应用名,key 应用密钥
* @param method,默认post文件流$_FILES['pic'],form表单
* method=url以POST图片URL地址的方式$_POST['pic']='http://img.....jpg',自动去抓取并存储。
* method=buff以POST图片文件buff方式上传$_POST['pic']=图片文件二进制数据
* @return width图片宽度,height图片高度。
*/
require('config.php');
require('imagick.class.php');
//验证app key
if ( !$_POST['app'] || !$_CONFIG_APP_KEY[$_POST['app']] || ($_CONFIG_APP_KEY[$_POST['app']] != $_POST['key']) )
{
err_ret('app key error.');
}
if ($_POST['method'] == 'url') //URL方式上传
{
// require('curl.class.php');
// $curl = new curl_class();
// $curl->get($_POST['pic']);
// if ($buff = $curl->currentResponse('body'))
if ($buff = file_get_contents($_POST['pic']))
{
$imagick = new Imagick();
if ( $imagick->readImageBlob($buff) )
{
resize_upload($imagick);
}
else
{
err_ret("url's content is not pic.");
}
}
else
{
err_ret('file not exists.');
}
}
elseif ($_POST['method'] == 'buff') //POST buff方式上传
{
if ($buff = $_POST['pic'])
{
$imagick = new Imagick();
if ( $imagick->readImageBlob($buff) )
{
resize_upload($imagick);
}
else
{
err_ret("post buff is not pic.");
}
}
else
{
err_ret('file buff is empty.');
}
}
else
{
if ($_FILES['pic']['size'])
{
$imagick = new Imagick();
if ( $imagick->readImage($_FILES['pic']['tmp_name']) )
{
unlink($_FILES['pic']['tmp_name']);
resize_upload($imagick);
}
else
{
err_ret("post file is not pic.");
}
}
else
{
err_ret('post file is empty.');
}
}
//处理图片
function resize_upload($imagick)
{
global $_CONFIG_MAX_SIZE;
$width = $imagick->getImageWidth();
$height = $imagick->getImageHeight();
if ($width > $height && $width > $_CONFIG_MAX_SIZE)
{
$width = $_CONFIG_MAX_SIZE;
$imagick->resizeImage($_CONFIG_MAX_SIZE, $_CONFIG_MAX_SIZE, Imagick::FILTER_CATROM, 1, true);
$height = $imagick->getImageHeight();
}
elseif ($height > $width && $height > $_CONFIG_MAX_SIZE)
{
$height = $_CONFIG_MAX_SIZE;
$imagick->resizeImage($_CONFIG_MAX_SIZE, $_CONFIG_MAX_SIZE, Imagick::FILTER_CATROM, 1, true);
$width = $imagick->getImageWidth();
}
$file_ext_name = strtolower($imagick->getImageFormat());
if (!$file_ext_name || $file_ext_name == 'jpeg')
{
$imagick->setImageFormat('jpeg');
$file_ext_name = 'jpg';
}
$buff = $imagick->getImageBlob();
if ($ret = fastdfs_storage_upload_by_filebuff1($buff, $file_ext_name))
{
$ret = '/'.$ret;
ok_ret($ret, $width, $height);
exit;
}
else
{
err_ret('upload fail.');
}
}
/**
* 成功时返回
* @param $pic 文件名
* @param $width 宽
* @param $height 高
*/
function ok_ret($pic, $width, $height)
{
if ($_POST['backurl'])
{
$url = $_POST['backurl'].'?code=1&pic='.$pic.'&width='.$width.'&height='.$height;
header('location:'.$url);
}
else
{
$json = array('code'=>1, 'pic'=>$pic, 'width'=>$width, 'height'=>$height);
echo json_encode($json);
}
exit;
}
//失败返回信息
function err_ret( $msg )
{
if ($_POST['backurl'])
{
$url = $_POST['backurl'].'?code=-1&msg='.$msg;
header('location:'.$url);
}
else
{
$json = array('code'=>-1, 'msg'=>$msg);
echo json_encode($json);
}
exit;
}