当前位置:Gxlcms > PHP教程 > PHP图片采集程序--图片采集类

PHP图片采集程序--图片采集类

时间:2021-07-01 10:21:17 帮助过:27人阅读

代码来自本人博客: http://www.taoniwu.com/archives/3234.html各位大神如果有什么好的建议,感谢提出~~~~~
  1. ?class Collection{
  2. protected $url; //采集地址
  3. protected $prefix; //重命名文件前缀
  4. protected $style; //需要采集的图片格式,传入一个数组
  5. const prel = '/(?:http?|https?):\/\/(?:[^\.\/\(\)\?]+)\.(?:[^\.\/]+)\.(?:com|cn|net|org)\/(?:[^\.:\"\'\(\)\?]+)\.(jpg|png|gif)/i'; //采集规则
  6. //构造函数
  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. //开始采集数据
  30. public function action(){
  31. $url = $this->checkurl();
  32. $imgurl = $this->collecturl($url);
  33. $this->savafile($imgurl);
  34. }
  35. //url处理
  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. //文件保存
  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.'采集成功';
  85. }
  86. }
  87. }
  88. }
  89. //地址采集函数,包括图片后缀名
  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. //检验数据
  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; //采集地址
  3. protected $prefix; //重命名文件前缀
  4. protected $style; //需要采集的图片格式,传入一个数组
  5. const prel = '/(?:http?|https?):\/\/(?:[^\.\/\(\)\?]+)\.(?:[^\.\/]+)\.(?:com|cn|net|org)\/(?:[^\.:\"\'\(\)\?]+)\.(jpg|png|gif)/i'; //采集规则
  6. //构造函数
  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. //开始采集数据
  30. public function action(){
  31. $url = $this->checkurl();
  32. $imgurl = $this->collecturl($url);
  33. $this->savafile($imgurl);
  34. }
  35. //url处理
  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. //文件保存
  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.'采集成功';
  85. }
  86. }
  87. }
  88. }
  89. //地址采集函数,包括图片后缀名
  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. //检验数据
  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. }

人气教程排行