PHP image collection program-image collection

Source: Internet
Author: User
PHP image collection program-image collection
The code comes from my blog: Ghost ~~~~~

  1. ? Class Collection {
  2. Protected $ url; // Collection Address
  3. Protected $ prefix; // rename the file prefix
  4. Protected $ style; // The format of the image to be collected. input an array.
  5. Const prel = '/(? : Http? | Https ?) :\/\/(? : [^ \. \/\ (\) \?] + )\.(? : [^ \. \/] + )\.(? : Com | cn | net | org )\/(? : [^ \.: \ "\ '\ (\) \?] +) \. (Jpg | png | gif)/I '; // collection rules
  6. // Constructor
  7. Function _ construct ($ url, $ prefix, $ style ){
  8. Switch ($ this-> checkdata ($ url, $ prefix, $ style )){
  9. Case 1:
  10. Echo 'script alert ("collection address cannot be blank! ") Script ';
  11. Exit;
  12. Break;
  13. Case 2:
  14. Echo 'script alert ("The format of the image to be collected, which should be an array! ") Script ';
  15. Exit;
  16. Break;
  17. Case 3:
  18. Echo 'script alert ("The format of the image to be collected. it cannot be blank! ") Script ';
  19. Exit;
  20. Break;
  21. Case 4:
  22. Echo 'script alert ("the file name cannot contain./| or start with a space! ") Script ';
  23. Exit;
  24. }
  25. $ This-> url = $ url;
  26. $ This-> prefix = $ prefix;
  27. $ This-> style = $ style;
  28. }
  29. // Start data collection
  30. Public function action (){
  31. $ Url = $ this-> checkurl ();
  32. $ Imgurl = $ this-> collecturl ($ url );
  33. $ This-> savafile ($ imgurl );
  34. }
  35. // Url processing
  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. // Save the file
  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 ('ymmd'). rand (Random, 99999). '.'. $ imgurl [1] [$ key];
  75. } Else {
  76. $ Filename = $ dir. date ('ymd'). rand (Random, 99999). '.'. $ imgurl [1] [$ key];
  77. }
  78. $ Fo = @ fopen ($ filename, 'WB ');
  79. If ($ fo === false ){
  80. Echo 'script alert ("file creation failed, file directory cannot be written! ") Script ';
  81. Exit;
  82. }
  83. $ Fw = fwrite ($ fo, $ obj );
  84. Echo'

    '. $ Filename.' collection successful

    ';
  85. }
  86. }
  87. }
  88. }
  89. // Address Collection function, including image suffix
  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: prel, $ 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: prel, $ code, $ arrimg );
  105. Return $ arrimg;
  106. }
  107. }
  108. // Check 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. ?>

  1. Class Collection {
  2. Protected $ url; // Collection Address
  3. Protected $ prefix; // rename the file prefix
  4. Protected $ style; // The format of the image to be collected. input an array.
  5. Const prel = '/(? : Http? | Https ?) :\/\/(? : [^ \. \/\ (\) \?] + )\.(? : [^ \. \/] + )\.(? : Com | cn | net | org )\/(? : [^ \.: \ "\ '\ (\) \?] +) \. (Jpg | png | gif)/I '; // collection rules
  6. // Constructor
  7. Function _ construct ($ url, $ prefix, $ style ){
  8. Switch ($ this-> checkdata ($ url, $ prefix, $ style )){
  9. Case 1:
  10. Echo 'script alert ("collection address cannot be blank! ") Script ';
  11. Exit;
  12. Break;
  13. Case 2:
  14. Echo 'script alert ("The format of the image to be collected, which should be an array! ") Script ';
  15. Exit;
  16. Break;
  17. Case 3:
  18. Echo 'script alert ("The format of the image to be collected. it cannot be blank! ") Script ';
  19. Exit;
  20. Break;
  21. Case 4:
  22. Echo 'script alert ("the file name cannot contain./| or start with a space! ") Script ';
  23. Exit;
  24. }
  25. $ This-> url = $ url;
  26. $ This-> prefix = $ prefix;
  27. $ This-> style = $ style;
  28. }
  29. // Start data collection
  30. Public function action (){
  31. $ Url = $ this-> checkurl ();
  32. $ Imgurl = $ this-> collecturl ($ url );
  33. $ This-> savafile ($ imgurl );
  34. }
  35. // Url processing
  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. // Save the file
  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 ('ymmd'). rand (Random, 99999). '.'. $ imgurl [1] [$ key];
  75. } Else {
  76. $ Filename = $ dir. date ('ymd'). rand (Random, 99999). '.'. $ imgurl [1] [$ key];
  77. }
  78. $ Fo = @ fopen ($ filename, 'WB ');
  79. If ($ fo === false ){
  80. Echo 'script alert ("file creation failed, file directory cannot be written! ") Script ';
  81. Exit;
  82. }
  83. $ Fw = fwrite ($ fo, $ obj );
  84. Echo'

    '. $ Filename.' collection successful

    ';
  85. }
  86. }
  87. }
  88. }
  89. // Address Collection function, including image suffix
  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: prel, $ 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: prel, $ code, $ arrimg );
  105. Return $ arrimg;
  106. }
  107. }
  108. // Check 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. }

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.