PHP implementation upload picture to ZIMG server _php instance

Source: Internet
Author: User
Tags md5

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

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.