Share a remote picture of PHP capture

Source: Internet
Author: User
Tags count fread functions preg return string trim

  1. <?php
  2. /* Use PHP to achieve remote image capture function. Basic process:
  3. 1, get the target website picture address.
  4. 2, read the picture content.
  5. 3, create the path to save the picture and name the picture.
  6. 4, write the picture content.
  7. 5, complete.
  8. We do this by writing a few functions. */
  9. function Make_dir () to create a directory. Determine if the picture file directory you want to save exists, create a directory if it does not exist, and set the directory to write permissions.
  10. function Make_dir ($path) {
  11. if (!file_exists ($path)) {//No presence is established
  12. $MK = @mkdir ($path, 0777); Permissions
  13. @chmod ($path, 0777);
  14. }
  15. return true;
  16. }
  17. function Read_filetext () Gets the picture content. Use fopen to open the picture file, and then fread read the picture file contents.
  18. function Read_filetext ($filepath) {
  19. $filepath =trim ($filepath);
  20. $HTMLFP = @fopen ($filepath, "R");
  21. Remote
  22. if (Strstr ($filepath, "://")) {
  23. while ($data = @fread ($HTMLFP, 500000)) {
  24. $string. = $data;
  25. }
  26. }
  27. Local
  28. else{
  29. $string = @fread ($HTMLFP, @filesize ($filepath));
  30. }
  31. @fclose ($HTMLFP);
  32. return $string;
  33. }
  34. The function Write_filetext () writes the file, writes the picture content fputs to the file, namely saves the picture file.
  35. function Write_filetext ($filepath, $string) {
  36. $string =stripslashes ($string);
  37. $fp = @fopen ($filepath, "w");
  38. @fputs ($fp, $string);
  39. @fclose ($FP);
  40. }
  41. function Get_filename () Gets the name of the picture, or you can customize the name of the file you want to save.
  42. function Get_filename ($filepath) {
  43. $FR =explode ("/", $filepath);
  44. $count =count ($FR)-1;
  45. return $FR [$count];
  46. }
  47. Several functions are then combined, called in the function Save_pic (), and finally the saved picture path is returned.
  48. function Save_pic ($url, $savepath = ' ") {
  49. Processing addresses
  50. $url =trim ($url);
  51. $url =str_replace ("", "%20", $url);
  52. Read files
  53. $string =read_filetext ($url);
  54. if (Emptyempty ($string)) {
  55. Echo ' can't read file '; exit;
  56. }
  57. Filename
  58. $filename = Get_filename ($url);
  59. Storage Directory
  60. Make_dir ($savepath); Create a storage directory
  61. File address
  62. $filepath = $savepath. $filename;
  63. Write a file
  64. Write_filetext ($filepath, $string);
  65. return $filepath;
  66. }
  67. The final step is to call the Save_pic () function to save the picture, and we'll use the following code to do the test.
  68. Destination picture Address
  69. $pic = "/program/uploadpic/2013-5/2013510152511166.jpg";
  70. For ($i =100 $i <=204; $i + +)
  71. // {
  72. $pic = ' http://cache.soso.com/img/img/e '. $i '. gif ';
  73. Save Directory
  74. $savepath = "images/";
  75. Echo save_pic ($pic, $savepath). " <br/> ";
  76. // }
  77. /*
  78. In practice, we may collect the content of a certain site, such as product information, including the collection of photos to save the chain to the Web server. At this time we can use the regular matching page content, the pages of matching pictures are found, and then downloaded to the Web server, complete the collection of pictures. The following code is for testing only:
  79. */
  80. function Get_pic ($cont, $path) {
  81. $pattern _src = '/<[imgimg].*?src=&/.jpg]) "[\]].*? [\/]?>/';
  82. $num = Preg_match_all ($pattern _src, $cont, $match _src);
  83. $pic _arr = $match _src[1]; Get an array of pictures
  84. foreach ($pic _arr as $pic _item) {//loop out the address of each picture
  85. Save_pic ($pic _item, $path); Download and save pictures
  86. echo "[ok]..! <br/> ";
  87. }
  88. }
  89. Then we analyze the content of the page, find out the main content, call Get_pic () to achieve the preservation of the picture.
  90. We collected Pacific Internet a picture of the content page of the mobile phone report php100.com
  91. $url = "http://gz.pconline.com.cn/321/3215791.html";
  92. $content = file_get_contents ($url);//Get Web page content
  93. $preg = ' #<div class= "Art_con" > (. *) <div class= "ivy620 ivy620ex" ></div> #iUs ";
  94. Preg_match_all ($preg, $content, $arr);
  95. $cont = $arr [1][0];
  96. Get_pic ($cont, ' img/');
  97. /*
  98. The above code to test, you can collect pictures, but still some scenes did not take into account, such as the target site did more than 302 jumps, the target site did a variety of anti-collection, left to like tossing students to try it.
  99. */
  100. ?>


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.