Php remote image retrieval instance

Source: Internet
Author: User
Php remote image retrieval instance

Example: a php remote image retrieval class. Code:

  1. If (! Defined ('basepath') exit ('no direct script access allowed ');
  2. /*
  3. * Remote image retrieval
  4. *
  5. * Required to enable curl extension
  6. * Simulate the php Upload principle, create a cache directory, and store the remotely obtained files to the cache directory.
  7. */
  8. Class url_pic {
  9. Protected $ cache; // cache path
  10. Public function _ construct ($ cache = '')
  11. {
  12. If (! Emptyempty ($ cache ))
  13. {
  14. $ This-> cache = $ cache;
  15. }
  16. Else
  17. {
  18. $ This-> cache = 'uploads/cache /';
  19. }
  20. }
  21. // Set the cache Directory
  22. Public function set_cache ($ cache = '')
  23. {
  24. If (! Emptyempty ($ cache ))
  25. {
  26. $ This-> cache = $ cache;
  27. }
  28. }
  29. /*
  30. * Get a remote image and save the file to the cache folder.
  31. *
  32. * $ Url: obtain the Remote File link
  33. * $ Error
  34. * @ Return 777: a folder cannot be created.
  35. * @ Return refers to the name of the file stored in the cache.
  36. */
  37. Public function get_file ($ url, $ error = 777)
  38. {
  39. $ Path = $ this-> build_folder ($ this-> cache );
  40. If ($ path = false) return $ error;
  41. $ Curl = curl_init ();
  42. // Set the url you want to capture
  43. Curl_setopt ($ curl, curlopt_url, $ url );
  44. // Set the header
  45. Curl_setopt ($ curl, curlopt_header, 0 );
  46. // Set the curl parameter to save the result to the string or output to the screen.
  47. Curl_setopt ($ curl, curlopt_returntransfer, 1 );
  48. // Run curl to request the webpage
  49. $ File = curl_exec ($ curl );
  50. // Close the url request
  51. Curl_close ($ curl );
  52. // Write the file into the obtained data
  53. $ Filename = $ this-> cache. date ("ymdhis ");
  54. If (self: build_file ($ file, $ filename) = false)
  55. {
  56. Return false;
  57. }
  58. Return $ filename;
  59. }
  60. // Create a folder
  61. Public function build_folder ($ dir)
  62. {
  63. If (! Is_dir ($ dir ))
  64. {
  65. If (! Mkdir ($ dir, 0777, true) |! Chmod ($ dir, 0777 ))
  66. {
  67. Return false;
  68. }
  69. }
  70. Return true;
  71. }
  72. /*
  73. * Simulate the move_uploaded_file method of php for mobile files
  74. *
  75. * $ Cache file path
  76. * $ Absolute path of the file name to be generated by filename
  77. *
  78. * @ Return $ filename
  79. */
  80. Public function move_file ($ cache, $ filename)
  81. {
  82. $ File = @ file_get_contents ($ cache );
  83. If (self: build_file ($ file, $ filename) = false)
  84. {
  85. Return false;
  86. }
  87. Unlink ($ cache); // clear the cached image
  88. Return $ filename;
  89. }
  90. /*
  91. * Generate a file
  92. * $ File: the file or binary stream to be written.
  93. * $ Absolute path of the file name to be generated for newname
  94. */
  95. Protected static function build_file ($ file, $ filename)
  96. {
  97. $ Write = @ fopen ($ filename, "w ");
  98. If ($ write = false)
  99. {
  100. Return false;
  101. }
  102. If (fwrite ($ write, $ file) = false)
  103. {
  104. Return false;
  105. }
  106. If (fclose ($ write) = false)
  107. {
  108. Return false;
  109. }
  110. Return true;
  111. }
  112. }

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.