UploadFile.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/2/27/027
  6. * Time: 15:31
  7. */
  8. namespace backend\server;
  9. use yii\web\UploadedFile;
  10. use Yii;
  11. //use yii\base\Model;
  12. use yii\base\DynamicModel;
  13. class UploadFile
  14. {
  15. // public static function Radio($path,$model)
  16. // {
  17. // if(empty($path) || !is_object($model)) return false;
  18. // if(!is_dir($path)){
  19. // mkdir($path,0777,true);
  20. // chmod($path,0777);
  21. // }
  22. // $imgName = md5(uniqid()).'.'.$model->extension;
  23. // if($model->saveAs($path.$imgName) === true)
  24. // {
  25. // return $imgName;
  26. // }
  27. // return false;
  28. // }
  29. /*
  30. * 单张
  31. * */
  32. public static function InstanceImgName($name,$path)
  33. {
  34. if(empty($path)) return false;
  35. if(!is_dir($path)){
  36. mkdir($path,0777,true);
  37. chmod($path,0777);
  38. }
  39. $row = UploadedFile::getInstanceByName($name);
  40. if(is_object($row))
  41. {
  42. $validator = new \yii\validators\ImageValidator();
  43. $validator->extensions = ['png','jpg','jpeg'];
  44. $validator->maxSize = 1024*1024*2; //2M
  45. if(!$validator->validate($row,$error))return false;
  46. $name = md5(uniqid(microtime())).'.'.$row->extension;
  47. if($row->saveAs($path.$name) == true) return $name;
  48. }
  49. return false;
  50. }
  51. /*
  52. * 重写
  53. * */
  54. public static function InstanceImgName_v1($name,$path)
  55. {
  56. if(empty($path)) throw new \Exception('请添加图片路径');
  57. if(!is_dir($path)){
  58. mkdir($path,0777,true);
  59. chmod($path,0777);
  60. }
  61. $row = UploadedFile::getInstanceByName($name);
  62. if(is_object($row))
  63. {
  64. $validator = new \yii\validators\ImageValidator();
  65. switch ($path)
  66. {
  67. case Yii::$app->params['img_url']['push_lb'] :
  68. $validator->extensions = ['png','jpg','jpeg'];
  69. $validator->maxSize = 1024*1024*0.3; //2KB
  70. break;
  71. case Yii::$app->params['img_url']['push_lb']:
  72. $validator->extensions = ['png','jpg','jpeg'];
  73. $validator->maxSize = 1024*1024*0.3; //3KB
  74. break;
  75. default:
  76. $validator->extensions = ['png','jpg','jpeg'];
  77. $validator->maxSize = 1024*1024*2; //2M
  78. break;
  79. }
  80. if(!$validator->validate($row,$error))
  81. {
  82. throw new \Exception($error);
  83. }
  84. $name = md5(uniqid(microtime())).'.'.$row->extension;
  85. if($row->saveAs($path.$name) == true) return $name;
  86. }
  87. throw new \Exception('图片添加失败');
  88. }
  89. /*
  90. * 重写多张
  91. * */
  92. public static function InstancesImgName_v1($name,$path)
  93. {
  94. if(empty($path)) throw new \Exception('请添加图片路径');
  95. if(!is_dir($path)){
  96. mkdir($path,0777,true);
  97. chmod($path,0777);
  98. }
  99. $arr = [];
  100. $row = UploadedFile::getInstancesByName($name);
  101. switch ($path)
  102. {
  103. case Yii::$app->params['img_url']['news']: //资讯
  104. $validator = new \yii\validators\ImageValidator();
  105. $validator->extensions = ['png','jpg','jpeg'];
  106. $validator->maxSize = 1024*1024*0.3;
  107. $validator->maxFiles = 3;
  108. if(empty($row)) throw new \Exception('请上传图片');
  109. // $validator->notImage = '请上传图片';
  110. break;
  111. default:
  112. $validator = new \yii\validators\ImageValidator();
  113. $validator->extensions = ['png','jpg','jpeg'];
  114. $validator->maxSize = 1024*1024*0.5;
  115. break;
  116. }
  117. foreach ($row as $k=>$v)
  118. {
  119. if(!$validator->validate($row[$k],$error))
  120. {
  121. throw new \Exception($error);
  122. }
  123. $name = md5(uniqid(microtime())).'.'.$row[$k]->extension;
  124. if($row[$k]->saveAs($path.$name) == true)
  125. {
  126. $arr[$k] = $name;
  127. }
  128. }
  129. if(!empty($arr)) return $arr;
  130. throw new \Exception('图片添加失败');
  131. }
  132. public static function InstancesImgName($name,$path)
  133. {
  134. $arr = [];
  135. $row = UploadedFile::getInstancesByName($name);
  136. if(empty($row[0])){
  137. return '请上传图片';
  138. }
  139. $validator = new \yii\validators\ImageValidator();
  140. $validator->extensions = ['png','jpg','jpeg'];
  141. $validator->maxSize = 1024*1024*2; //2M
  142. foreach ($row as $k=>$v)
  143. {
  144. if(!is_object($row[$k])) return false;
  145. if(!$validator->validate($row[$k],$error))
  146. {
  147. return $error;
  148. }
  149. $name = md5(uniqid(microtime())).'.'.$row[$k]->extension;
  150. if($row[$k]->saveAs($path.$name) == true)
  151. {
  152. $arr[$k] = $name;
  153. }
  154. }
  155. if(!empty($arr)) return $arr;
  156. }
  157. /*
  158. * 删除临时目录里面的图片
  159. * */
  160. public static function delImg($path,$name)
  161. {
  162. @unlink($path.$name);
  163. }
  164. }