Heim > Backend-Entwicklung > PHP-Tutorial > PHP图片采集程序--图片采集类

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

WBOY
Freigeben: 2016-07-25 09:02:08
Original
1155 Leute haben es durchsucht
代码来自本人博客: 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 '<script>alert("采集地址不能为空!")</script>';
  11. exit;
  12. break;
  13. case 2:
  14. echo '<script>alert("需要采集的图片格式,应该为数组!")</script>';
  15. exit;
  16. break;
  17. case 3:
  18. echo '<script>alert("需要采集的图片格式,不能为空!")</script>';
  19. exit;
  20. break;
  21. case 4:
  22. echo '<script>alert("文件名不能含有. / |或用空格开头!")</script>';
  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 $myurl[] = $temparr[0].$i.$temparr[1];
  44. }
  45. }else{
  46. $myurl = $this->url;
  47. }
  48. return $myurl;
  49. }
  50. //文件保存
  51. protected function savafile($imgurl){
  52. if(!empty($imgurl)){
  53. foreach($imgurl[0] as $key=>$value){
  54. $filename = '';
  55. if(in_array($imgurl[1][$key],$this->style)){
  56. $size = @getimagesize($value);
  57. if($size === false){
  58. continue;
  59. }
  60. list($w,$h,$t,$a) = $size;
  61. if($w continue;
  62. }
  63. ob_start();
  64. readfile($value);
  65. $obj = ob_get_contents();
  66. ob_end_clean();
  67. $dir = 'F:/php/';
  68. if(!is_dir($dir)){
  69. mkdir($dir,0777);
  70. }
  71. if(!empty($this->prefix)){
  72. $filename = $dir.$this->prefix.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];
  73. }else{
  74. $filename = $dir.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];
  75. }
  76. $fo = @fopen($filename,'wb');
  77. if($fo === false){
  78. echo '<script>alert("创建文件失败,文件目录不可写!")</script>';
  79. exit;
  80. }
  81. $fw = fwrite($fo,$obj);
  82. echo '
    '.$filename.'采集成功
    ';
  83. }
  84. }
  85. }
  86. }
  87. //地址采集函数,包括图片后缀名
  88. protected function collecturl($url){
  89. set_time_limit(0);
  90. if(is_array($url)){
  91. $arr = array();
  92. $imgkey = array();
  93. foreach($url as $value){
  94. $code = file_get_contents($value);
  95. preg_match_all(self::prel,$code,$arrimg);
  96. $arr = array_merge($arr,$arrimg[0]);
  97. $imgkey = array_merge($imgkey,$arrimg[1]);
  98. }
  99. return array($arr,$imgkey);
  100. }else{
  101. $code = file_get_contents($url);
  102. preg_match_all(self::prel,$code,$arrimg);
  103. return $arrimg;
  104. }
  105. }
  106. //检验数据
  107. private function checkdata($url,$prefix,$style){
  108. if(empty($url)){
  109. return 1;
  110. }elseif(!is_array($style)){
  111. return 2;
  112. }elseif(count($style)==0){
  113. return 3;
  114. }elseif(stripos($prefix,'.') !== false || stripos($prefix,'/') !== false || stripos($prefix,'|') !== false){
  115. return 4;
  116. }
  117. }
  118. }
  119. ?>
复制代码
  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 '<script>alert("采集地址不能为空!")</script>';
  11. exit;
  12. break;
  13. case 2:
  14. echo '<script>alert("需要采集的图片格式,应该为数组!")</script>';
  15. exit;
  16. break;
  17. case 3:
  18. echo '<script>alert("需要采集的图片格式,不能为空!")</script>';
  19. exit;
  20. break;
  21. case 4:
  22. echo '<script>alert("文件名不能含有. / |或用空格开头!")</script>';
  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 $myurl[] = $temparr[0].$i.$temparr[1];
  44. }
  45. }else{
  46. $myurl = $this->url;
  47. }
  48. return $myurl;
  49. }
  50. //文件保存
  51. protected function savafile($imgurl){
  52. if(!empty($imgurl)){
  53. foreach($imgurl[0] as $key=>$value){
  54. $filename = '';
  55. if(in_array($imgurl[1][$key],$this->style)){
  56. $size = @getimagesize($value);
  57. if($size === false){
  58. continue;
  59. }
  60. list($w,$h,$t,$a) = $size;
  61. if($w continue;
  62. }
  63. ob_start();
  64. readfile($value);
  65. $obj = ob_get_contents();
  66. ob_end_clean();
  67. $dir = 'F:/php/';
  68. if(!is_dir($dir)){
  69. mkdir($dir,0777);
  70. }
  71. if(!empty($this->prefix)){
  72. $filename = $dir.$this->prefix.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];
  73. }else{
  74. $filename = $dir.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];
  75. }
  76. $fo = @fopen($filename,'wb');
  77. if($fo === false){
  78. echo '<script>alert("创建文件失败,文件目录不可写!")</script>';
  79. exit;
  80. }
  81. $fw = fwrite($fo,$obj);
  82. echo '
    '.$filename.'采集成功
    ';
  83. }
  84. }
  85. }
  86. }
  87. //地址采集函数,包括图片后缀名
  88. protected function collecturl($url){
  89. set_time_limit(0);
  90. if(is_array($url)){
  91. $arr = array();
  92. $imgkey = array();
  93. foreach($url as $value){
  94. $code = file_get_contents($value);
  95. preg_match_all(self::prel,$code,$arrimg);
  96. $arr = array_merge($arr,$arrimg[0]);
  97. $imgkey = array_merge($imgkey,$arrimg[1]);
  98. }
  99. return array($arr,$imgkey);
  100. }else{
  101. $code = file_get_contents($url);
  102. preg_match_all(self::prel,$code,$arrimg);
  103. return $arrimg;
  104. }
  105. }
  106. //检验数据
  107. private function checkdata($url,$prefix,$style){
  108. if(empty($url)){
  109. return 1;
  110. }elseif(!is_array($style)){
  111. return 2;
  112. }elseif(count($style)==0){
  113. return 3;
  114. }elseif(stripos($prefix,'.') !== false || stripos($prefix,'/') !== false || stripos($prefix,'|') !== false){
  115. return 4;
  116. }
  117. }
  118. }
复制代码


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage