getRemoteImage.php 3.4 KB

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