最新消息:XAMPP默认安装之后是很不安全的,我们只需要点击左方菜单的 "安全"选项,按照向导操作即可完成安全设置。

PHP 代码积累(一)

XAMPP下载 admin 642浏览 0评论

 

  1. <?php
  2. /**一、使用代码块
  3. * code block name.
  4. * some description
  5. * @version 1.0.2
  6. * @tutorial 用于显示相关渠道的二维码,并供用户下载
  7. * @author leebaotong
  8. *
  9. */
  10. /**block name
  11. * some description
  12. * @tutorial ps:如果页面太乱,可用于规范代码层次;
  13. * 这种用法我本见于 javascript,后来灵机一动用在了 PHP 里,竟然也可使用,可能因为同是脚本的原因吧.
  14. * 但是在 python 里尝试时失败了,可能因为 python 的缩进要求很严格的原因吧.需要注意的是,这里有作用域的问题,切,切切!!!
  15. */
  16. {
  17. if (Request::get ( 1 )) {
  18. } else {
  19. die ( “缺少信息” );
  20. }
  21. }
  22. /**二、用于生成图片下载页:
  23. * ps:如果图片较大,最好不要用直接下载的方式,太耗资源,可显示到页面供用户自行保存
  24. */
  25. {
  26. function createPicToDown($picLocalPath) {
  27. header ( “Content-Type:text/html;charset=utf-8” );
  28. header ( “Content-type: image/jpeg” );
  29. $image = imagecreatefromjpeg ( $picLocalPath );
  30. header ( “Content-Disposition: attachment;filename=qr.jpg ” );
  31. imagejpeg ( $image );
  32. imagedestroy ( $image );
  33. }
  34. }
  35. /**三、对于常用到的变量,或字符串常量,可用定义;这样修改时只修改一即可.
  36. */
  37. {
  38. define ( ‘DS’, DIRECTORY_SEPARATOR );
  39. }
  40. /**四、建立文件夹
  41. */
  42. {
  43. /**
  44. * 生成路径.
  45. *
  46. * @param string $dir
  47. * @return boolean
  48. */
  49. function directory($dir) {
  50. return is_dir ( $dir ) or (directory ( dirname ( $dir ) ) and mkdir ( $dir, 0777 ));
  51. }
  52. }
  53. /**五、处理代分支有不确定因素时,要增加 default 做为默认处理方法
  54. */
  55. {
  56. $filetype = $_FILES [‘img’] [‘type’];
  57. switch ($filetype) {
  58. case ‘image/pjpeg’ :
  59. case ‘image/jpg’ :
  60. case ‘image/jpeg’ :
  61. $fileExtAsString = ‘.jpg’;
  62. break;
  63. case ‘image/gif’ :
  64. $fileExtAsString = ‘.gif’;
  65. break;
  66. default :
  67. $fileExtAsString = ‘.gif’;
  68. break;
  69. }
  70. }
  71. /**六、上传文件处理
  72. */
  73. {
  74. /**
  75. * 前台表单:
  76. *
  77. * @tutorial :直接写在视图页面
  78. */
  79. {
  80. ‘<form action=”” method=”post” enctype=”multipart/form-data”>’;
  81. ‘<input type=”file” name=”img” >’;
  82. ‘</form>’;
  83. }
  84. /**
  85. * 后台:
  86. * @tutorial :过程比较简单,根据项目需要可增删.
  87. */
  88. {
  89. if (! empty ( $_FILES [“img”] [“name”] )) {
  90. /**
  91. * 检查上传文件是否在允许上传的类型
  92. */
  93. {
  94. // 允许上传的文件格式
  95. $tp = array (
  96. “image/gif” => ‘.gif’,
  97. “image/pjpeg” => ‘.jpeg’,
  98. “image/jpg” => ‘.jpg’,
  99. “image/jpeg” => ‘.jpeg’
  100. );
  101. // 检查上传文件是否在允许上传的类型
  102. if (! array_key_exists ( $_FILES [“img”] [“type”], $tp )) {
  103. tusi ( ‘格式不对’ );
  104. Redirect::delay_to ( ‘addchannel.html’, 1 );
  105. }
  106. $filetype = $_FILES [‘img’] [‘type’];
  107. $fileExtAsString = in_array ( $filetype, $tp ) ? $tp [$filetype] : ‘.jpg’;
  108. }
  109. /**
  110. * 验证目录
  111. */
  112. {
  113. $picSubPath = date ( ‘Y’ ) . ‘/’ . date ( ‘m’ ) . ‘/’ . date ( ‘d’ );
  114. $picName = date ( ‘Ymdhis’, time () );
  115. if (! is_dir ( $picSubPath )) {
  116. directory ( $picSubPath );
  117. }
  118. $tempFileFullPathAsString = $picSubPath . ‘/’ . $picName . $fileExtAsString; // 图片的完整路径
  119. }
  120. $flag = 1;
  121. }
  122. // 上传文件检测完成,移至存储路径
  123. if ($flag) {
  124. $result = move_uploaded_file ( $_FILES [“img”] [“tmp_name”], $tempFileFullPathAsString );
  125. } else {
  126. // ….错误处理
  127. }
  128. }
  129. }
  130. /**七、合并两张图片
  131. *
  132. */
  133. {
  134. /**设置路径相关,预备图片资源及临时文件存储位置
  135. */
  136. {
  137. $logoUrl = ‘http://img.hb.aicdn.com/317b4fa2476e23235f73a01a07d2727b2a4b3abae2f5-b3Ove2_fw658’;
  138. $logoTemp = ‘./tempLogo.jpg’;
  139. $bgUrl = ‘http://h.hiphotos.baidu.com/image/pic/item/c83d70cf3bc79f3d3f442939b8a1cd11728b2916.jpg’;
  140. $bgTemp = ‘./tempbg.jpg’;
  141. $newPic = ‘./new.jpeg’;
  142. }
  143. /**获取图片资源;如是本地文件,可省略此处.
  144. */
  145. {
  146. $bgTempContent = file_get_contents ($bgUrl );
  147. file_put_contents ( $bgTemp, $bgTempContent );
  148. $logoTempContent = file_get_contents ( $logoUrl );
  149. file_put_contents ( $logoTemp, $logoTempContent );
  150. }
  151. /**合成图片
  152. */
  153. {
  154. if (file_exists ( $bgTemp )) {
  155. $QR = imagecreatefromstring ( file_get_contents ( $bgTemp ) );
  156. if ($logoTemp !== FALSE) {
  157. $logo = imagecreatefromstring ( file_get_contents ( $logoTemp ) );
  158. $QR_width = imagesx ( $QR );
  159. $QR_height = imagesy ( $QR );
  160. $logo_width = imagesx ( $logo );
  161. $logo_height = imagesy ( $logo );
  162. $logoW = $QR_width / 5;
  163. $scale = $logo_width / $logo_qr_width;
  164. $logoH = $logo_height / $scale;
  165. $fw = ($QR_width – $logo_qr_width) / 2;
  166. imagecopyresampled ( $QR, $logo, $from_width, $fw, 0, 0, $logoW, $logoH, $logo_width, $logo_height );
  167. }
  168. imagepng ( $QR, $newPic ); // 最终将图片输出到文件
  169. imagedestroy ( $QR );
  170. }
  171. }

转载请注明:XAMPP中文组官网 » PHP 代码积累(一)

您必须 登录 才能发表评论!