Recently our project needs a picture server to store the user avatar, we use ZIMG to process and store the picture, the following brief introduction how to use PHP upload picture to zimg, and obtain the corresponding picture return information
Using the Curl Library to implement uploads
According to ZIMG use the document, we want to let zimg return JSON information, we must raw_post upload the picture, the following is the demo code
$upload _url = ' http://192.168.0.99:5000/upload ';
$image _file = './test.jpg ';
Gets the picture suffix $value = explode (".", $image _file);
$extension = Strtolower (Array_pop ($value));
Generate a local temporary storage path and generate the appropriate folder $dir = ' Aurthur '; $save _path = ' uploads/'. $dir '/'. Date (' Y '). ' /'. Date (' MD ').
/';
$save _rule = MD5 (Uniqid (Mt_rand (), true);
if (!is_dir ($save _path)) {if (false = = mkdir ($save _path, 0700, True)) {exit (' Create folder failed '); }} $save _image_file = $save _path. $save _rule. ".
$extension ";
Store the picture to a temporary path file_put_contents ($save _image_file, file_get_contents ($image _file));
Gets the real address (absolute path) of the temporary saved picture $realpath = Realpath ($save _image_file);
Upload pictures to zimg image storage Service $ch = Curl_init ();
Read the contents of the picture to the variable $post _data;
$post _data = file_get_contents ($realpath);
$headers = Array ();
Be sure to add this header $headers [] = ' content-type: '. $extension;
curl_setopt ($ch, Curlopt_url, $upload _url);
curl_setopt ($ch, Curlopt_header, false);
curl_setopt ($ch, Curlopt_httpheader, $headers);
curl_setopt ($ch, Curlopt_returntransfer, 1); Curl_seTopt ($ch, Curlopt_post, true);
curl_setopt ($ch, Curlopt_binarytransfer, true);
curl_setopt ($ch, Curlopt_postfields, $post _data);//Raw_post Way $info = curl_exec ($ch);
Curl_close ($ch);
$json = Json_decode ($info, true);
$signature = $json [' info '] [' MD5 '];
Echo $signature;
If you want to test the code, change the above uploadurl to your own ZIMG server address and change the image_file to the path of the picture you want to upload