PHP圖片擷取程式--圖片擷取類

WBOY
發布: 2016-07-25 09:02:08
原創
1065 人瀏覽過
程式碼來自本人部落格: 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("檔案名稱不能含有. / |或用空格開頭! 🎜> $this->style = $style;<li> }<li> //開始收集資料<li> public function action(){<li> $url = $this->checkurl();<li> $ imgurl = $this->collecturl($url);<li> $this->savafile($imgurl);<li> }<li> //url處理<li> protected function checkurl(){<li> $munprel = '/([0-9]+,[0-9]+)/i';<li> $myurl;<li> if(preg_match($munprel,$this->url,$arr)){<li> $temp = substr($arr[0],1,strlen($arr[0])-2);<li> $mymunber = explode(',',$temp);<li> $temparr = explode ($arr[0],$this->url);<li> for($i=$mymunber[0];$i<=$mymunber[1];$i++){<li> $myurl[] = $temparr[0].$i.$temparr[1];<li> }<li> }else{<li> $myurl = $this->url;<li> }<li> return $myurl;<li> }<li> //檔案儲存<li> protected function savafile($imgurl){<li> if(!empty($imgurl)){<li> foreach($imgurl[0] as $key=>$value ){<li> $filename = '';<li> if(in_array($imgurl[1][$key],$this->style)){<li> $size = @getimagesize($value);<li> if($size === false){<li> continue;<li> }<li> list($w,$h,$t,$a) = $size;<li> if($w< 200 || $h<200){<li> continue;<li> } <li> ob_start();<li> readfile($value);<li> $obj = ob_get_contents();<li> ); ;<li> $dir = 'F:/php/';<li> if(!is_dir($dir)){<li> mkdir($dir,0777);<li> }<li> if(!empty ($this->prefix)){<li> $filename = $dir.$this->prefix.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key ];<li> }else{<li> $filename = $dir.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];<li> }<li> $fo = @fopen($filename,'wb');<li> if($fo === false){<li> echo '<script>alert("建立檔案失敗,檔案目錄不可寫! ")</script>';
  23. exit;
  24. }
  25. $fw = fwrite($fo,$obj);
  26. echo '
    '.$filename.'擷取成功
    ';
  27. }
  28. }
  29. }
  30. }
  31. //位址擷取函數,包含圖片後綴名
  32. protected function collecturl($url){
  33. set_time_limit(0);
  34. if(is_array($url)){
  35. $arr = array();
  36. $imgkey = array();
  37. foreach($url as $value){
  38. $code = file_get_contents($value);
  39. preg_match_all(self::prel,$code,$arrimg);
  40. $arr = array_merge($array_merge($array_merge($array_merge($array_merge($array_merge))); arr,$arrimg[0]);
  41. $imgkey = array_merge($imgkey,$arrimg[1]);
  42. }
  43. return array($arr,$imgkey);
  44. }else{
  45. $code = file_get_contents($url);
  46. preg_match_all(self::prel,$code,$arrimg);
  47. return $arrimg;
  48. }
  49. }
  50. return $arrimg;
  51. }
  52. }
  53. // //檢定資料
  54. private function checkdata($url,$prefix,$style){
  55. if(empty($url)){
  56. return 1;
  57. }elseif(!is_array($style)){
  58. return 2;
  59. }elseif(count($style)==0){
  60. return 3;
  61. }elseif(stripos($prefix,'.') !== false || stripos ($prefix,'/') !== false || stripos($prefix,'|') !== false){
  62. return 4;
  63. }
  64. }
  65. }
  66. }
  67. ?>
複製程式碼
  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. }
  17. $this->url = $url;
  18. $this->prefix = $prefix;
  19. $this->style = $style;
  20. }
  21. //開始收集資料
  22. public function action(){
  23. $url = $this->checkurl();
  24. $imgurl = $this->collecturl ($url);
  25. $this->savafile($imgurl);
  26. }
  27. //url處理
  28. protected function checkurl(){
  29. $munprel = '/([0- 9]+,[0-9]+)/i';
  30. $myurl;
  31. if(preg_match($munprel,$this->url,$arr)){
  32. $temp = substr( $arr[0],1,strlen($arr[0])-2);
  33. $mymunber = explode(',',$temp);
  34. $temparr = explode($arr[0], $this->url);
  35. for($i=$mymunber[0];$i $myurl[] = $temparr[0].$ i.$temparr[1];
  36. }
  37. }else{
  38. $myurl = $this->url;
  39. }
  40. return $myurl;
  41. }
  42. //檔案儲存
  43. protected function savafile($imgurl){
  44. if(!empty($imgurl)){
  45. foreach($imgurl[0]as $key=>$value){
  46. $filename = '';
  47. if(in_array($imgurl[1][$key],$this->style)){
  48. $size = @getimagesize($value);
  49. if($size = == false){
  50. continue;
  51. }
  52. list($w,$h,$t,$a) = $size;
  53. if($w continue;
  54. }
  55. ob_start();
  56. readfile($value);
  57. $obj = ob_get_contents();
  58. ob_end_clean(); if(!is_dir($dir)){
  59. mkdir($dir,0777);
  60. }
  61. if(!empty($this->prefix) ){
  62. $filename = $dir.$this->prefix.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];
  63. }else {
  64. $filename = $dir.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];
  65. }
  66. $fo = @fopen ($filename,'wb');
  67. if($fo === false){
  68. echo '<script>alert("建立檔案失敗,檔案目錄不可寫! ")</script>';
  69. exit;
  70. }
  71. $fw = fwrite($fo,$obj);
  72. echo '
    '.$filename.'擷取成功
    ';
  73. }
  74. }
  75. }
  76. }
  77. //位址擷取函數,包含圖片後綴名
  78. protected function collecturl($url){
  79. set_time_limit(0);
  80. if(is_array($url)){
  81. $arr = array();
  82. $imgkey = array();
  83. foreach($url as $value){
  84. $code = file_get_contents($value);
  85. preg_match_all(self::prel,$code,$arrimg);
  86. $arr = array_merge($array_merge($array_merge($array_merge($array_merge($array_merge))); arr,$arrimg[0]);
  87. $imgkey = array_merge($imgkey,$arrimg[1]);
  88. }
  89. return array($arr,$imgkey);
  90. }else{
  91. $code = file_get_contents($url);
  92. preg_match_all(self::prel,$code,$arrimg);
  93. return $arrimg;
  94. }
  95. }
  96. return $arrimg;
  97. }
  98. }
  99. // //檢定資料
  100. private function checkdata($url,$prefix,$style){
  101. if(empty($url)){
  102. return 1;
  103. }elseif(!is_array($style)){
  104. return 2;
  105. }elseif(count($style)==0){
  106. return 3;
  107. }elseif(stripos($prefix,'.') !== false || stripos ($prefix,'/') !== false || stripos($prefix,'|') !== false){
  108. return 4;
  109. }
  110. }
  111. }
  112. }
  113. 複製程式碼
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!