PHP Image Collection Program--Image Collection Class

Source: Internet
Author: User
Tags readfile
Code from my blog: http://www.taoniwu.com/archives/3234.html all the great god if there is any good advice, thank you for proposing ~~~~~
  1. ? class collection{
  2. protected $url;//Capture Address
  3. protected $prefix;//rename file prefix
  4. protected $style;//image format to be collected, passing in an array
  5. Const PREL = '/(?: http?| HTTPS?): \ /\/(?:[^\.\/\(\)\?] +)\. (?:[^\.\/]+)\. (?: com|cn|net|org) \/(?: [^\.:\] \'\(\)\?] +)\. (jpg|png|gif)/I ';//Collection rules
  6. constructor function
  7. function __construct ($url, $prefix, $style) {
  8. Switch ($this->checkdata ($url, $prefix, $style)) {
  9. Case 1:
  10. Echo ';
  11. Exit
  12. Break
  13. Case 2:
  14. Echo ';
  15. Exit
  16. Break
  17. Case 3:
  18. Echo ';
  19. Exit
  20. Break
  21. Case 4:
  22. Echo ';
  23. Exit
  24. }
  25. $this->url = $url;
  26. $this->prefix = $prefix;
  27. $this->style = $style;
  28. }
  29. Start collecting data
  30. Public Function action () {
  31. $url = $this->checkurl ();
  32. $imgurl = $this->collecturl ($url);
  33. $this->savafile ($imgurl);
  34. }
  35. URL Handling
  36. protected function Checkurl () {
  37. $munprel = '/\ ([0-9]+,[0-9]+\)/I ';
  38. $myurl;
  39. if (Preg_match ($munprel, $this->url, $arr)) {
  40. $temp = substr ($arr [0],1,strlen ($arr [0])-2);
  41. $mymunber = Explode (', ', $temp);
  42. $temparr = explode ($arr [0], $this->url);
  43. for ($i = $mymunber [0]; $i <= $mymunber [1]; $i + +) {
  44. $myurl [] = $temparr [0]. $i. $temparr [1];
  45. }
  46. }else{
  47. $myurl = $this->url;
  48. }
  49. return $myurl;
  50. }
  51. File Save
  52. protected function Savafile ($imgurl) {
  53. if (!empty ($imgurl)) {
  54. foreach ($imgurl [0] as $key = + $value) {
  55. $filename = ";
  56. if (In_array ($imgurl [1][$key], $this->style)) {
  57. $size = @getimagesize ($value);
  58. if ($size = = = False) {
  59. Continue
  60. }
  61. List ($w, $h, $t, $a) = $size;
  62. if ($w <200 | | $h <200) {
  63. Continue
  64. }
  65. Ob_start ();
  66. ReadFile ($value);
  67. $obj = Ob_get_contents ();
  68. Ob_end_clean ();
  69. $dir = ' f:/php/';
  70. if (!is_dir ($dir)) {
  71. mkdir ($dir, 0777);
  72. }
  73. if (!empty ($this->prefix)) {
  74. $filename = $dir. $this->prefix.date (' YMD '). Rand (10000,99999). $imgurl [1][$key];
  75. }else{
  76. $filename = $dir. Date (' YMD '). Rand (10000,99999). $imgurl [1][$key];
  77. }
  78. $FO = @fopen ($filename, ' WB ');
  79. if ($fo = = = False) {
  80. Echo ';
  81. Exit
  82. }
  83. $FW = fwrite ($fo, $obj);
  84. Echo '. $filename. ' Acquisition success ';
  85. }
  86. }
  87. }
  88. }
  89. Address acquisition functions, including image suffix names
  90. protected function Collecturl ($url) {
  91. Set_time_limit (0);
  92. if (Is_array ($url)) {
  93. $arr = Array ();
  94. $imgkey = Array ();
  95. foreach ($url as $value) {
  96. $code = file_get_contents ($value);
  97. Preg_match_all (self::p rel, $code, $arrimg);
  98. $arr = Array_merge ($arr, $arrimg [0]);
  99. $imgkey = Array_merge ($imgkey, $arrimg [1]);
  100. }
  101. Return Array ($arr, $imgkey);
  102. }else{
  103. $code = file_get_contents ($url);
  104. Preg_match_all (self::p rel, $code, $arrimg);
  105. return $arrimg;
  106. }
  107. }
  108. Test data
  109. Private Function Checkdata ($url, $prefix, $style) {
  110. if (empty ($url)) {
  111. return 1;
  112. }elseif (!is_array ($style)) {
  113. return 2;
  114. }elseif (Count ($style) ==0) {
  115. return 3;
  116. }elseif (Stripos ($prefix, '. ')!== false | | Stripos ($prefix, '/')!== false | | Stripos ($prefix, ' | ')!== false) {
  117. return 4;
  118. }
  119. }
  120. }
  121. ?>
Copy Code
  1. Class collection{
  2. protected $url;//Capture Address
  3. protected $prefix;//rename file prefix
  4. protected $style;//image format to be collected, passing in an array
  5. Const PREL = '/(?: http?| HTTPS?): \ /\/(?:[^\.\/\(\)\?] +)\. (?:[^\.\/]+)\. (?: com|cn|net|org) \/(?: [^\.:\] \'\(\)\?] +)\. (jpg|png|gif)/I ';//Collection rules
  6. constructor function
  7. function __construct ($url, $prefix, $style) {
  8. Switch ($this->checkdata ($url, $prefix, $style)) {
  9. Case 1:
  10. Echo ';
  11. Exit
  12. Break
  13. Case 2:
  14. Echo ';
  15. Exit
  16. Break
  17. Case 3:
  18. Echo ';
  19. Exit
  20. Break
  21. Case 4:
  22. Echo ';
  23. Exit
  24. }
  25. $this->url = $url;
  26. $this->prefix = $prefix;
  27. $this->style = $style;
  28. }
  29. Start collecting data
  30. Public Function action () {
  31. $url = $this->checkurl ();
  32. $imgurl = $this->collecturl ($url);
  33. $this->savafile ($imgurl);
  34. }
  35. URL Handling
  36. protected function Checkurl () {
  37. $munprel = '/\ ([0-9]+,[0-9]+\)/I ';
  38. $myurl;
  39. if (Preg_match ($munprel, $this->url, $arr)) {
  40. $temp = substr ($arr [0],1,strlen ($arr [0])-2);
  41. $mymunber = Explode (', ', $temp);
  42. $temparr = explode ($arr [0], $this->url);
  43. for ($i = $mymunber [0]; $i <= $mymunber [1]; $i + +) {
  44. $myurl [] = $temparr [0]. $i. $temparr [1];
  45. }
  46. }else{
  47. $myurl = $this->url;
  48. }
  49. return $myurl;
  50. }
  51. File Save
  52. protected function Savafile ($imgurl) {
  53. if (!empty ($imgurl)) {
  54. foreach ($imgurl [0] as $key = + $value) {
  55. $filename = ";
  56. if (In_array ($imgurl [1][$key], $this->style)) {
  57. $size = @getimagesize ($value);
  58. if ($size = = = False) {
  59. Continue
  60. }
  61. List ($w, $h, $t, $a) = $size;
  62. if ($w <200 | | $h <200) {
  63. Continue
  64. }
  65. Ob_start ();
  66. ReadFile ($value);
  67. $obj = Ob_get_contents ();
  68. Ob_end_clean ();
  69. $dir = ' f:/php/';
  70. if (!is_dir ($dir)) {
  71. mkdir ($dir, 0777);
  72. }
  73. if (!empty ($this->prefix)) {
  74. $filename = $dir. $this->prefix.date (' YMD '). Rand (10000,99999). $imgurl [1][$key];
  75. }else{
  76. $filename = $dir. Date (' YMD '). Rand (10000,99999). $imgurl [1][$key];
  77. }
  78. $FO = @fopen ($filename, ' WB ');
  79. if ($fo = = = False) {
  80. Echo ';
  81. Exit
  82. }
  83. $FW = fwrite ($fo, $obj);
  84. Echo '. $filename. ' Acquisition success ';
  85. }
  86. }
  87. }
  88. }
  89. Address acquisition functions, including image suffix names
  90. protected function Collecturl ($url) {
  91. Set_time_limit (0);
  92. if (Is_array ($url)) {
  93. $arr = Array ();
  94. $imgkey = Array ();
  95. foreach ($url as $value) {
  96. $code = file_get_contents ($value);
  97. Preg_match_all (self::p rel, $code, $arrimg);
  98. $arr = Array_merge ($arr, $arrimg [0]);
  99. $imgkey = Array_merge ($imgkey, $arrimg [1]);
  100. }
  101. Return Array ($arr, $imgkey);
  102. }else{
  103. $code = file_get_contents ($url);
  104. Preg_match_all (self::p rel, $code, $arrimg);
  105. return $arrimg;
  106. }
  107. }
  108. Test data
  109. Private Function Checkdata ($url, $prefix, $style) {
  110. if (empty ($url)) {
  111. return 1;
  112. }elseif (!is_array ($style)) {
  113. return 2;
  114. }elseif (Count ($style) ==0) {
  115. return 3;
  116. }elseif (Stripos ($prefix, '. ')!== false | | Stripos ($prefix, '/')!== false | | Stripos ($prefix, ' | ')!== false) {
  117. return 4;
  118. }
  119. }
  120. }
Copy Code
  • 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.