ホームページ  >  記事  >  バックエンド開発  >  PHP画像のアップロードとサムネイルクラスの生成

PHP画像のアップロードとサムネイルクラスの生成

WBOY
WBOYオリジナル
2016-07-25 08:45:21850ブラウズ
    /**
  1. * 写真をアップロードします
  2. */
  3. class imgUpload{
  4. static protected $a;
  5. protected $formName; //フォーム名
  6. protected $directory //ディレクトリにアップロードされたファイル
  7. protected $ maxSize; //最大ファイルアップロードサイズ
  8. protected $canUpload; //アップロード可能かどうか
  9. protected $doUpFile; //アップロードファイル名
  10. private function __construct($_formName='file; ', $_directory='./images/uploads/', $_maxSize=1048576){ //1024*1024=1M
  11. //初期化パラメータ
  12. $this->formName = $_formName;
  13. $this->ディレクトリ= $_directory;
  14. $this->maxSize = $_maxSize;
  15. $this->canUpload = true;
  16. $this->doUpFile = '';
  17. $this->sm_File = '';
  18. }
  19. //画像が許可された形式内であるかどうかを判断します
  20. static public function Type($_formName='file'){
  21. $_type = $_FILES[$_formName]['type'];
  22. switch ($_type){
  23. case 'image/gif':
  24. if (self::$a==NULL)
  25. self::$a = new imgUpload($_formName);
  26. Break;
  27. case 'image/pjpeg':
  28. if (self:: $a ==NULL)
  29. self::$a = new imgUpload($_formName);
  30. Break;
  31. case 'image/x-png':
  32. if (self::$a==NULL)
  33. self::$ a = new imgUpload($_formName);
  34. break;
  35. default:
  36. self::$a = false;
  37. }
  38. return self::$a;
  39. }
  40. //ファイルサイズを取得
  41. public function getSize($ _format=' K'){
  42. if ($this->canUpload) {
  43. if (0 == $_FILES[$this->formName]['size']) {
  44. $this->canUpload = false ;
  45. return $this->canUpload;
  46. Break;
  47. }
  48. switch ($_format){
  49. case 'B':
  50. return $_FILES[$this->formName]['size'];
  51. Break;
  52. case 'K ':
  53. returnround($_FILES[$this->formName]['size'] / 1024);
  54. Break;
  55. case 'M':
  56. returnround($_FILES[$this->formName] ][' size'] / (1024*1024),2);
  57. Break;
  58. }
  59. }
  60. }
  61. //ファイルタイプを取得
  62. public function getExt(){
  63. if ($this->canUpload) {
  64. $_name = $_FILES[$this->formName]['name'];
  65. $_nameArr =explode('.',$_name);
  66. $_count = count($_nameArr)-1;
  67. }
  68. return $_nameArr[ $_count];
  69. }
  70. //ファイル名を取得
  71. public function getName(){
  72. if ($this->canUpload) {
  73. return $_FILES[$this->formName][' name'];
  74. }
  75. }
  76. //新しいファイル名を作成します
  77. public function newName(){
  78. return date('YmdHis').rand(0,9);
  79. }
  80. //ファイルをアップロードします
  81. public function Upload(){
  82. if ($this->canUpload)
  83. {
  84. $_getSize = $this->getSize('B');
  85. if (!$_getSize)
  86. {
  87. return $_getSize;
  88. Break ;
  89. }
  90. else
  91. {
  92. $_newName = $this->newName();
  93. $_ext = $this->getExt();
  94. $_doUpload = move_uploaded_file($_FILES[$this->formName]['tmp_name' ], $this->directory.$_newName.".".$_ext);
  95. if ($_doUpload)
  96. {
  97. $this->doUpFile = $_newName;
  98. }
  99. return $_doUpload;
  100. }
  101. }
  102. }
  103. //创建缩略图
  104. public functionThumb($_dstChar='_m', $_max_len=320){ //$_dstChar:_m , _s
  105. if ($this->canUpload && $this-> doUpFile != "") {
  106. $_ext = $this->getExt();
  107. $_srcImage = $this->gt;directory.$this->doUpFile.".".$_ext;
  108. //得られる画像情報数グループ
  109. $_date = getimagesize($_srcImage, &$info);
  110. $src_w = $_date[0]; //ソース图片宽
  111. $src_h = $_date[1]; //ソース图片高
  112. $src_max_len = max($src_w, $src_h); //求得长边
  113. $src_min_len = min($src_w, $src_h); //求得短边
  114. $dst_w = ''; //目标图片宽
  115. $dst_h = ''; //目标图片高
  116. //宽高比放,最长边は$_max_lenを超えない
  117. if ($src_max_len>$_max_len)
  118. {
  119. $percent = $src_min_len / $src_max_len;
  120. if ($src_w == $src_max_len)
  121. {
  122. $dst_w = $_max_len;
  123. $dst_h = $percent * $dst_w;
  124. }
  125. else
  126. {
  127. $dst_h = $_max_len;
  128. $dst_w = $percent * $dst_h;
  129. }
  130. }
  131. else
  132. {
  133. $dst_w = $src_w;
  134. $dst_h = $src_h;
  135. }
  136. //建立缩略图時、ソース图片の位置
  137. $src_x = '';
  138. $src_y = '';
  139. //ロゴに略図が使用されていると判断した場合は、裁定
  140. if ('s_' == $_dstChar) {
  141. $src_x = $src_w * 0.10;
  142. $src_y = $src_h * 0.10;
  143. $src_w *= 0.8;
  144. $src_h *= 0.8;
  145. }
  146. //判断图片类型并创建对应新图片
  147. switch ($_date[2]){
  148. case 1:
  149. $src_im = imagecreatefromgif($_srcImage);
  150. Break;
  151. case 2:
  152. $src_im = imagecreatefromjpeg($_srcImage);
  153. ブレーク;
  154. ケース 3:
  155. $src_im = imagecreatefrompng($_srcImage);
  156. ブレーク;
  157. ケース 8:
  158. $src_im = imagecreatefromwbmp($_srcImage);
  159. ブレーク;
  160. }
  161. //创建一幅新画像
  162. if ($_date[2]==1) { //gif無法应用imagecreatetruecolor
  163. $dst_im = imagecreate($dst_w, $dst_h);
  164. }else {
  165. $dst_im = imagecreatetruecolor($dst_w, $dst_h);
  166. }
  167. //この画像のサムネイルのコピーを作成します
  168. // $bg = imagecolorallocate($dst_im,255,255,0);
  169. imagecopyresize($dst_im,$src_im, 0, 0, $src_x, $src_y, $dst_w, $ dst_h, $src_w, $src_h );
  170. //画像に対してアンチエイリアス処理を実行します
  171. imageantialias($dst_im, true);
  172. switch ($_date[2]) {
  173. case 1:
  174. $cr = imagegif( $ dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
  175. Break;
  176. case 2:
  177. $cr = imagejpeg($dst_im, $this-> ; directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
  178. Break;
  179. case 3://imagepng には問題があるため、ここでは代わりに imagejpg を使用してください
  180. $cr = imagejpeg( $dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
  181. Break;
  182. }
  183. // $cr = imagejpeg($dst_im, $this-> ;ディレクトリ。$_dstChar.$this->doUpFile, 90);
  184. if ($cr) {
  185. $this->sm_File = $this->doUpFile.$_dstChar."。 ".$ _ext;
  186. return $this->sm_File;
  187. }else {
  188. return false;
  189. }
  190. }
  191. imagedestroy($dst_im);
  192. imagedestroy($cr);
  193. }
  194. //アップロードされたファイルを取得ファイル名
  195. public function getUpFile(){
  196. if ($this->doUpFile!='') {
  197. $_ext = $this->getExt();
  198. return $this->doUpFile."."。 $_ext ;
  199. }else {
  200. return false;
  201. }
  202. }
  203. //アップロードされたファイルのフルパスを取得します
  204. public function getFilePatch(){
  205. if ($this->doUpFile!='') {
  206. $_ext = $this->getExt();
  207. return $this->directory.$this->doUpFile.".".$_ext;
  208. }else {
  209. return false;
  210. }
  211. }
  212. / /省略形を取得 サムネイル ファイルのフル パス
  213. public function getThumb(){
  214. if ($this->sm_File!='') {
  215. return $this->sm_File;
  216. }else {
  217. return false;
  218. }
  219. }
  220. // アップロードされたファイルのパスを取得します
  221. public function getDirectory(){
  222. if ($this->directory!='') {
  223. return $this->directory;
  224. }else {
  225. return false;
  226. }
  227. }
  228. }
  229. ?>
コードをコピー

画像アップロード、PHP


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。