getRemoteImage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: taoqili
  5. * Date: 11-12-28
  6. * Time: 上午9:54
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. header("Content-Type: text/html; charset=utf-8");
  10. error_reporting(E_ERROR|E_WARNING);
  11. //远程抓取图片配置
  12. $config = array(
  13. "savePath" => "upload/". date("Ymd") .'/' , //保存路径
  14. "allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" ) , //文件允许格式
  15. "maxSize" => 3000 //文件大小限制,单位KB
  16. );
  17. $uri = htmlspecialchars( $_POST[ 'upfile' ] );
  18. $uri = str_replace( "&amp;" , "&" , $uri );
  19. getRemoteImage( $uri,$config );
  20. /**
  21. * 远程抓取
  22. * @param $uri
  23. * @param $config
  24. */
  25. function getRemoteImage( $uri,$config)
  26. {
  27. //忽略抓取时间限制
  28. set_time_limit( 0 );
  29. //ue_separate_ue ue用于传递数据分割符号
  30. $imgUrls = explode( "ue_separate_ue" , $uri );
  31. $tmpNames = array();
  32. foreach ( $imgUrls as $imgUrl ) {
  33. //http开头验证
  34. if(strpos($imgUrl,"http")!==0){
  35. array_push( $tmpNames , "error" );
  36. continue;
  37. }
  38. //获取请求头
  39. $heads = get_headers( $imgUrl );
  40. //死链检测
  41. if ( !( stristr( $heads[ 0 ] , "200" ) && stristr( $heads[ 0 ] , "OK" ) ) ) {
  42. array_push( $tmpNames , "error" );
  43. continue;
  44. }
  45. //格式验证(扩展名验证和Content-Type验证)
  46. $fileType = strtolower( strrchr( $imgUrl , '.' ) );
  47. if ( !in_array( $fileType , $config[ 'allowFiles' ] ) || stristr( $heads[ 'Content-Type' ] , "image" ) ) {
  48. array_push( $tmpNames , "error" );
  49. continue;
  50. }
  51. //打开输出缓冲区并获取远程图片
  52. ob_start();
  53. $context = stream_context_create(
  54. array (
  55. 'http' => array (
  56. 'follow_location' => false // don't follow redirects
  57. )
  58. )
  59. );
  60. //请确保php.ini中的fopen wrappers已经激活
  61. readfile( $imgUrl,false,$context);
  62. $img = ob_get_contents();
  63. ob_end_clean();
  64. //大小验证
  65. $uriSize = strlen( $img ); //得到图片大小
  66. $allowSize = 1024 * $config[ 'maxSize' ];
  67. if ( $uriSize > $allowSize ) {
  68. array_push( $tmpNames , "error" );
  69. continue;
  70. }
  71. //创建保存位置
  72. $savePath = $config[ 'savePath' ];
  73. if ( !file_exists( $savePath ) ) {
  74. mkdir( "$savePath" , 0777 );
  75. }
  76. //写入文件
  77. $tmpName = $savePath . time() . rand( 1 , 10000 ) . strrchr( $imgUrl , '.' );
  78. try {
  79. $fp2 = @fopen( $tmpName , "a" );
  80. fwrite( $fp2 , $img );
  81. fclose( $fp2 );
  82. array_push( $tmpNames , $tmpName );
  83. } catch ( Exception $e ) {
  84. array_push( $tmpNames , "error" );
  85. }
  86. }
  87. /**
  88. * 返回数据格式
  89. * {
  90. * 'url' : '新地址一ue_separate_ue新地址二ue_separate_ue新地址三',
  91. * 'srcUrl': '原始地址一ue_separate_ue原始地址二ue_separate_ue原始地址三',
  92. * 'tip' : '状态提示'
  93. * }
  94. */
  95. echo "{'url':'" . implode( "ue_separate_ue" , $tmpNames ) . "','tip':'远程图片抓取成功!','srcUrl':'" . $uri . "'}";
  96. }