Home > Backend Development > PHP Tutorial > PHP image collection program--image collection class

PHP image collection program--image collection class

WBOY
Release: 2016-07-25 09:02:08
Original
1134 people have browsed it
The code comes from my blog: http://www.taoniwu.com/archives/3234.html If you have any good suggestions, thank you~~~~~
  1. ?class Collection{
  2. protected $url; //Collection address
  3. protected $prefix; //Rename file prefix
  4. protected $style; //Pass in an array for the image format to be collected
  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 '<script>alert("The collection address cannot be empty!")< /script>';</li> <li> exit;</li> <li> break;</li> <li> case 2:</li> <li> echo '<script>alert("The image format to be collected should be an array!")</script>';
  11. exit;
  12. break;
  13. case 3:
  14. echo '<script>alert("The image format that needs to be collected cannot be empty!")</script>';
  15. exit;
  16. break;
  17. case 4:
  18. echo '<script> alert("The file name cannot contain . / | or start with a space!")</script>';
  19. exit;
  20. }
  21. $this->url = $url;
  22. $this->prefix = $prefix ;
  23. $this->style = $style;
  24. }
  25. //Start collecting data
  26. public function action(){
  27. $url = $this->checkurl();
  28. $imgurl = $this->collecturl ($url);
  29. $this->savafile($imgurl);
  30. }
  31. //url processing
  32. protected function checkurl(){
  33. $munprel = '/([0-9]+,[0-9] +)/i';
  34. $myurl;
  35. if(preg_match($munprel,$this->url,$arr)){
  36. $temp = substr($arr[0],1,strlen($arr[0 ])-2);
  37. $mymunber = explode(',',$temp);
  38. $temparr = explode($arr[0],$this->url);
  39. for($i=$mymunber[0 ];$i<=$mymunber[1];$i++){
  40. $myurl[] = $temparr[0].$i.$temparr[1];
  41. }
  42. }else{
  43. $myurl = $this- >url;
  44. }
  45. return $myurl;
  46. }
  47. //File saving
  48. protected function savafile($imgurl){
  49. if(!empty($imgurl)){
  50. foreach($imgurl[0] as $key= >$value){
  51. $filename = '';
  52. if(in_array($imgurl[1][$key],$this->style)){
  53. $size = @getimagesize($value);
  54. if ($size === false){
  55. continue;
  56. }
  57. list($w,$h,$t,$a) = $size;
  58. if($w<200 || $h<200){
  59. continue ;
  60. }
  61. ob_start();
  62. readfile($value);
  63. $obj = ob_get_contents();
  64. ob_end_clean();
  65. $dir = 'F:/php/';
  66. if(!is_dir($dir)) {
  67. mkdir($dir,0777);
  68. }
  69. if(!empty($this->prefix)){
  70. $filename = $dir.$this->prefix.date('Ymd').rand( 10000,99999).'.'.$imgurl[1][$key];
  71. }else{
  72. $filename = $dir.date('Ymd').rand(10000,99999).'.'.$imgurl [1][$key];
  73. }
  74. $fo = @fopen($filename,'wb');
  75. if($fo === false){
  76. echo '<script>alert("Failed to create file, The file directory cannot be written! ")</script>';
  77. exit;
  78. }
  79. $fw = fwrite($fo,$obj);
  80. echo '
    '. $filename.'Collection successful
    ';
  81. }
  82. }
  83. }
  84. }
  85. //Address collection function, including image suffix name
  86. protected function collecturl($url){
  87. set_time_limit(0);
  88. if (is_array($url)){
  89. $arr = array();
  90. $imgkey = array();
  91. foreach($url as $value){
  92. $code = file_get_contents($value);
  93. preg_match_all(self:: prel,$code,$arrimg);
  94. $arr = array_merge($arr,$arrimg[0]);
  95. $imgkey = array_merge($imgkey,$arrimg[1]);
  96. }
  97. return array($arr, $imgkey);
  98. }else{
  99. $code = file_get_contents($url);
  100. preg_match_all(self::prel,$code,$arrimg);
  101. return $arrimg;
  102. }
  103. }
  104. //Check data
  105. private function checkdata($url,$prefix,$style){
  106. if(empty($url)){
  107. return 1;
  108. }elseif(!is_array($style)){
  109. return 2;
  110. }elseif(count($style )==0){
  111. return 3;
  112. }elseif(stripos($prefix,'.') !== false || stripos($prefix,'/') !== false || stripos($prefix,' |') !== false){
  113. return 4;
  114. }
  115. }
  116. }
  117. ?>
Copy code
  1. class Collection{
  2. protected $url; //Collection address
  3. protected $prefix; //Rename file prefix
  4. protected $style; //The image format to be collected, pass 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 '<script>alert("The collection address cannot be empty!")</script>';
  11. exit;
  12. break;
  13. case 2:
  14. echo '<script>alert("The image format to be collected should be an array!")</script>';
  15. exit;
  16. break;
  17. case 3:
  18. echo '<script>alert("The image format that needs to be collected cannot be empty!")</script>';
  19. exit;
  20. break;
  21. case 4:
  22. echo '<script>alert("The file name cannot be Contains . / | or starts with a space! ")</script>';
  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 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. //File saving
  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 '<script>alert("Failed to create the file, the file directory is not writable! ")</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 name
  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. }
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template