Recently our project needs a picture server to store the user picture, we use zimg processing and storage of images, below a brief introduction of how to use PHP to upload images to zimg, and get the corresponding image return information
Using the Curl Library for uploading
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 ';//get picture suffix $value = explode (".", $image _file) ; $extension = Strtolower (Array_pop ($value));//generate local temporary storage path and generate corresponding 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)) {Exi T (' Create folder failed '); }} $save _image_file = $save _path. $save _rule. " $extension ";//Save the picture to the temporary path file_put_contents ($save _image_file, file_get_contents ($image _file));//Get the real address of the temporary saved picture ( Absolute path) $realpath = Realpath ($save _image_file);//upload image to zimg image storage Service $ch = Curl_init ();//Read picture contents to variable $post _data; $post _data = f Ile_get_contents ($realpath); $headers = Array ();//Make 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 Mode $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, please change the above uploadurl to your own ZIMG server address and change the image_file to the path of the image you need to upload.