PHP implementation upload image to zimg server _php instance

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.