首页 后端开发 php教程 PHP文件夹遍历,图片等比例压缩

PHP文件夹遍历,图片等比例压缩

Jul 25, 2016 am 08:42 AM

  1. /**
  2. * 来源:互联网
  3. */
  4. class image
  5. {
  6. var $w_pct = 100; //透明度
  7. var $w_quality = 100; //质量
  8. var $w_minwidth = 500; //最小宽
  9. var $w_minheight = 500; //最小高
  10. var $interlace = 0; //图像是否为隔行扫描的
  11. var $fontfile ; //字体文件
  12. var $w_img ; //默认水印图
  13. function __construct()
  14. {
  15. $this->fontfile = $_SERVER['DOCUMENT_ROOT'].'/public/fonts/simhei.ttf';
  16. $this->w_img = '';
  17. }
  18. function image()
  19. {
  20. $this->__construct();
  21. }
  22. function info($img)
  23. {
  24. $imageinfo = getimagesize($img); //返回图像信息数组 0=>宽的像素 1=>高的像素 2=>是图像类型的标记 3 =>是文本字符串,内容为“height="yyy" width="xxx"”,
  25. if($imageinfo === false) return false;
  26. $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //获取图像文件类型 $imageinfo[2]是图像类型的标记
  27. $imagesize = filesize($img); //图像大小
  28. $info = array(
  29. 'width'=>$imageinfo[0],
  30. 'height'=>$imageinfo[1],
  31. 'type'=>$imagetype,
  32. 'size'=>$imagesize,
  33. 'mime'=>$imageinfo['mime']
  34. );
  35. return $info;
  36. }
  37. /**
  38. * 缩略图
  39. *
  40. * @param string $image
  41. * @param string $filename
  42. * @param int $maxwidth
  43. * @param int $maxheight
  44. * @param string $suffix
  45. * @param int $autocut
  46. * @return string
  47. */
  48. function thumb($image, $filename = '',$maxwidth = 50, $maxheight = 50, $suffix='_thumb', $autocut = 0)
  49. {
  50. if( !$this->check($image)) return false;
  51. $info = $this->info($image); //获取图片信息
  52. if($info === false) return false;
  53. $srcwidth = $info['width']; //源图宽
  54. $srcheight = $info['height']; //源图高
  55. $pathinfo = pathinfo($image);
  56. $type = $pathinfo['extension']; //取得扩展名
  57. if(!$type) $type = $info['type']; //如果没有取到,用$info['type']
  58. $type = strtolower($type);
  59. unset($info);
  60. $scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //获取缩略比例
  61. //获取按照源图的比列
  62. $createwidth = $width = (int) ($srcwidth*$scale);//取得缩略宽();
  63. $createheight = $height = (int)($srcheight*$scale);//(); //取得缩略高
  64. $psrc_x = $psrc_y = 0;
  65. if($autocut) //按照缩略图的比例来获取
  66. {
  67. if($maxwidth/$maxheight=$height) //如果缩略图按比列比源图窄的话
  68. {
  69. $width = $maxheight/$height*$width; //宽按照相应比例做处理
  70. $height = $maxheight;// //高不变
  71. }
  72. elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果缩略图按比列比源图宽的话
  73. {
  74. $height = $maxwidth/$width*$height;//
  75. $width = $maxwidth;
  76. }
  77. if($maxwidth == '55'){
  78. $width = '55';
  79. $createwidth = '55';
  80. }
  81. if($maxwidth == '110'){
  82. $width = '110';
  83. $createwidth = '110';
  84. }
  85. }
  86. if($srcwidth $createwidth= $srcwidth;
  87. $createheight = $srcheight;
  88. }
  89. $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的图像处理函数
  90. $srcimg = $createfun($image); //返回图像标识符
  91. if($type != 'gif' && function_exists('imagecreatetruecolor')){
  92. $thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一个真彩色图像
  93. }else{
  94. if($maxwidth == '110' && $srcheight >= $srcwidth){
  95. $height1 = 72;
  96. $thumbimg = imagecreate($width, $height1); //新建一个基于调色板的图像
  97. }elseif ($maxwidth == '110' && $srcheight $width1 = 110;
  98. $thumbimg = imagecreate($width1, $height); //新建一个基于调色板的图像
  99. }else{
  100. $thumbimg = imagecreate($width, $height); //新建一个基于调色板的图像
  101. }
  102. }
  103. if(function_exists('imagecopyresampled')){ //重采样拷贝部分图像并调整大小,真彩
  104. //imagecopyresampled(新图,源图,新图左上角x距离,新图左上角y距离,源图左上角x距离,源图左上角y距离,新图宽,新图高,源图宽,源图高)
  105. if($maxwidth == '110' && $srcheight >= $srcwidth){
  106. $psrc_x = 0;
  107. $psrc_yz = $height/2;
  108. $psrc_y = $psrc_yz;
  109. $width = '110';
  110. }
  111. if($maxwidth == '110' && $srcheight >= $srcwidth && $srcheight>= '72' && $srcwidth $psrc_x = 0;
  112. $psrc_yz = $height/2;
  113. $psrc_y = $psrc_yz;
  114. $width = '110';
  115. }
  116. if($maxheight == '72' && $srcheight = '110'){
  117. $cha = ($maxheight-$srcheight)/2;
  118. $psrc_y = -($cha);
  119. $width = $srcwidth;
  120. $height = '72';
  121. $srcheight = '72';
  122. }
  123. imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
  124. }else{ //拷贝部分图像并调整大小,调色板
  125. imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
  126. }
  127. if($type=='gif' || $type=='png')
  128. {
  129. //imagecolorallocate 为一幅图像分配颜色
  130. $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 给基于调色板的图像填充背景色, 指派一个绿色
  131. // imagecolortransparent 将某个颜色定义为透明色
  132. imagecolortransparent($thumbimg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  133. }
  134. // imageinterlace 激活或禁止隔行扫描
  135. if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
  136. $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
  137. //imagejpeg imagegif imagepng
  138. if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //获取文件名
  139. //aaa.gif aaa_thumb.gif
  140. $imagefun($thumbimg, $filename); //新建图像
  141. imagedestroy($thumbimg); //销毁缩略图
  142. imagedestroy($srcimg); //销毁源图
  143. return $filename;
  144. }
  145. /**
  146. * 限制宽或高
  147. *
  148. * @param string $image
  149. * @param int $maxwidth
  150. * @param int $maxheight
  151. * @param string $suffix
  152. * @return string
  153. */
  154. function thumb3($image,$maxwidth = 0, $maxheight = 0, $suffix='_thumb')
  155. {
  156. if( !$this->check($image)) return false;
  157. $info = $this->info($image); //获取图片信息
  158. if($info === false) return false;
  159. $srcwidth = $info['width']; //源图宽
  160. $srcheight = $info['height']; //源图高
  161. $pathinfo = pathinfo($image);
  162. $type = $pathinfo['extension']; //取得扩展名
  163. if(!$type) $type = $info['type']; //如果没有取到,用$info['type']
  164. $type = strtolower($type);
  165. unset($info);
  166. if($maxwidth==0){
  167. $scale = $maxheight/$srcheight;
  168. if($scale $createwidth = $srcwidth*$scale;
  169. $createheight = $maxheight;
  170. }else{
  171. $createwidth = $srcwidth;
  172. $createheight = $srcheight;
  173. }
  174. }
  175. if($maxheight==0){
  176. $scale = $maxwidth/$srcwidth;
  177. if($scale $createwidth = $maxwidth;
  178. $createheight = $srcheight*$scale;
  179. }else{
  180. $createwidth = $srcwidth;
  181. $createheight = $srcheight;
  182. }
  183. }
  184. $psrc_x = $psrc_y = 0;
  185. $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的图像处理函数
  186. $srcimg = $createfun($image); //返回图像标识符
  187. if($type != 'gif' && function_exists('imagecreatetruecolor')){
  188. $thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一个真彩色图像
  189. }else{
  190. $thumbimg = imagecreate($createwidth, $createheight); //新建一个基于调色板的图像
  191. }
  192. if(function_exists('imagecopyresampled')){ //重采样拷贝部分图像并调整大小,真彩
  193. //imagecopyresampled(新图,源图,新图左上角x距离,新图左上角y距离,源图左上角x距离,源图左上角y距离,新图宽,新图高,源图宽,源图高)
  194. imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight, $srcwidth, $srcheight);
  195. }else{ //拷贝部分图像并调整大小,调色板
  196. imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight, $srcwidth, $srcheight);
  197. }
  198. if($type=='gif' || $type=='png')
  199. {
  200. //imagecolorallocate 为一幅图像分配颜色
  201. $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 给基于调色板的图像填充背景色, 指派一个绿色
  202. // imagecolortransparent 将某个颜色定义为透明色
  203. imagecolortransparent($thumbimg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  204. }
  205. // imageinterlace 激活或禁止隔行扫描
  206. if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
  207. $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
  208. //imagejpeg imagegif imagepng
  209. $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //获取文件名
  210. //aaa.gif aaa_thumb.gif
  211. $imagefun($thumbimg, $filename); //新建图像
  212. imagedestroy($thumbimg); //销毁缩略图
  213. imagedestroy($srcimg); //销毁源图
  214. return $filename;
  215. }
  216. /**
  217. * 生成特定尺寸缩略图 解决原版缩略图不能满足特定尺寸的问题 PS:会裁掉图片不符合缩略图比例的部分
  218. * @static
  219. * @access public
  220. * @param string $image 原图
  221. * @param string $type 图像格式
  222. * @param string $thumbname 缩略图文件名
  223. * @param string $maxWidth 宽度
  224. * @param string $maxHeight 高度
  225. * @param boolean $interlace 启用隔行扫描
  226. * @return void
  227. */
  228. static function thumb2($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
  229. // 获取原图信息
  230. $info = Image::getImageInfo($image);
  231. if ($info !== false) {
  232. $srcWidth = $info['width'];
  233. $srcHeight = $info['height'];
  234. $type = empty($type) ? $info['type'] : $type;
  235. $type = strtolower($type);
  236. $interlace = $interlace ? 1 : 0;
  237. unset($info);
  238. $scale = max($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例
  239. //判断原图和缩略图比例 如原图宽于缩略图则裁掉两边 反之..
  240. if($maxWidth / $srcWidth > $maxHeight / $srcHeight){
  241. //高于
  242. $srcX = 0;
  243. $srcY = ($srcHeight - $maxHeight / $scale) / 2 ;
  244. $cutWidth = $srcWidth;
  245. $cutHeight = $maxHeight / $scale;
  246. }else{
  247. //宽于
  248. $srcX = ($srcWidth - $maxWidth / $scale) / 2;
  249. $srcY = 0;
  250. $cutWidth = $maxWidth / $scale;
  251. $cutHeight = $srcHeight;
  252. }
  253. // 载入原图
  254. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  255. $srcImg = $createFun($image);
  256. //创建缩略图
  257. if ($type != 'gif' && function_exists('imagecreatetruecolor'))
  258. $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);
  259. else
  260. $thumbImg = imagecreate($maxWidth, $maxHeight);
  261. // 复制图片
  262. if (function_exists("ImageCopyResampled"))
  263. imagecopyresampled($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
  264. else
  265. imagecopyresized($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
  266. if ('gif' == $type || 'png' == $type) {
  267. //imagealphablending($thumbImg, false);//取消默认的混色模式
  268. //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
  269. $background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿色
  270. imagecolortransparent($thumbImg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  271. }
  272. // 对jpeg图形设置隔行扫描
  273. if ('jpg' == $type || 'jpeg' == $type)
  274. imageinterlace($thumbImg, $interlace);
  275. // 生成图片
  276. $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
  277. @chmod(dirname($thumbname),0777);
  278. $imageFun($thumbImg, $thumbname);
  279. imagedestroy($thumbImg);
  280. imagedestroy($srcImg);
  281. return $thumbname;
  282. }
  283. return false;
  284. }
  285. /**
  286. * 取得图像信息
  287. * @static
  288. * @access public
  289. * @param string $image 图像文件名
  290. * @return mixed
  291. */
  292. static function getImageInfo($img) {
  293. $imageInfo = getimagesize($img);
  294. if ($imageInfo !== false) {
  295. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
  296. $imageSize = filesize($img);
  297. $info = array(
  298. "width" => $imageInfo[0],
  299. "height" => $imageInfo[1],
  300. "type" => $imageType,
  301. "size" => $imageSize,
  302. "mime" => $imageInfo['mime']
  303. );
  304. return $info;
  305. } else {
  306. return false;
  307. }
  308. }
  309. //watermark(源图,生成文件,生成位置,水印文件,水印文本,背景色)
  310. function watermark($source, $target = '', $w_pos = 9, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc',$x='',$y='')
  311. {
  312. if( !$this->check($source)) return false;
  313. if(!$target) $target = $source;
  314. if ($w_img == '' && $w_text == '')
  315. $w_img = $this->w_img;
  316. $source_info = getimagesize($source);
  317. $source_w = $source_info[0]; //获取宽
  318. $source_h = $source_info[1]; //获取高
  319. if($source_w w_minwidth || $source_h w_minheight) return false; //宽和高达不到要求直接返回
  320. switch($source_info[2]) //新建图片
  321. {
  322. case 1 :
  323. $source_img = imagecreatefromgif($source);
  324. break;
  325. case 2 :
  326. $source_img = imagecreatefromjpeg($source);
  327. break;
  328. case 3 :
  329. $source_img = imagecreatefrompng($source);
  330. break;
  331. default :
  332. return false;
  333. }
  334. if(!empty($w_img) && file_exists($w_img)) //水印文件
  335. {
  336. $ifwaterimage = 1; //是否水印图
  337. $water_info = getimagesize($w_img); //水印信息
  338. $width = $water_info[0];
  339. $height = $water_info[1];
  340. switch($water_info[2])
  341. {
  342. case 1 :
  343. $water_img = imagecreatefromgif($w_img);
  344. break;
  345. case 2 :
  346. $water_img = imagecreatefromjpeg($w_img);
  347. break;
  348. case 3 :
  349. $water_img = imagecreatefrompng($w_img);
  350. break;
  351. default :
  352. return;
  353. }
  354. }
  355. else
  356. {
  357. $ifwaterimage = 0;
  358. //imagettfbbox 本函数计算并返回一个包围着 TrueType 文本范围的虚拟方框的像素大小。
  359. //imagettfbbox ( 字体大小, 字体角度, 字体文件,文件 )
  360. // echo $this->fontfile;
  361. $temp = imagettfbbox(ceil($w_font*1.6), 0, $this->fontfile, $w_text);//取得使用 truetype 字体的文本的范围
  362. $width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置
  363. $height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置
  364. unset($temp);
  365. }
  366. switch($w_pos)
  367. {
  368. case 0: //随机位置
  369. $wx = rand(0,($source_w - $width));
  370. $wy = rand(0,($source_h - $height));
  371. break;
  372. case 1: //左上角
  373. $wx = 25;
  374. $wy = 25;
  375. break;
  376. case 2: //上面中间位置
  377. $wx = ($source_w - $width) / 2;
  378. $wy = 0;
  379. break;
  380. case 3: //右上角
  381. $wx = $source_w - $width;
  382. $wy = 0;
  383. break;
  384. case 4: //左面中间位置
  385. $wx = 0;
  386. $wy = ($source_h - $height) / 2;
  387. break;
  388. case 5: //中间位置
  389. $wx = ($source_w - $width) / 2;
  390. $wy = ($source_h - $height) / 2;
  391. break;
  392. case 6: //底部中间位置
  393. $wx = ($source_w - $width) / 2;
  394. $wy = $source_h - $height;
  395. break;
  396. case 7: //左下角
  397. $wx = 0;
  398. $wy = $source_h - $height;
  399. break;
  400. case 8: //右面中间位置
  401. $wx = $source_w - $width;
  402. $wy = ($source_h - $height) /2;
  403. break;
  404. case 9: //右下角
  405. $wx = $source_w - $width;
  406. $wy = $source_h - $height ;
  407. break;
  408. default: //随机
  409. $wx = rand(0,($source_w - $width));
  410. $wy = rand(0,($source_h - $height));
  411. break;
  412. case 10://自定义位置
  413. $wx = $x;
  414. $wy = $y;
  415. case 11: //底部中间位置--手工客专用
  416. $wx = ($source_w - $width) / 2;
  417. $wy = $source_h - $height-50;
  418. break;
  419. break;
  420. }
  421. if($ifwaterimage) //如果有水印图
  422. {
  423. //imagecopymerge 拷贝并合并图像的一部分
  424. //参数(源图,水印图,拷贝到源图x位置,拷贝到源图y位置,从水印图x位置,从水印图y位置,高,宽,透明度)
  425. //imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
  426. imagecopy($source_img,$water_img,$wx,$wy,0,0,$width,$height);
  427. }
  428. else
  429. {
  430. if(!empty($w_color) && (strlen($w_color)==7))
  431. {
  432. $r = hexdec(substr($w_color,1,2)); //获取红色
  433. $g = hexdec(substr($w_color,3,2)); //获取绿色
  434. $b = hexdec(substr($w_color,5)); //获取蓝色
  435. }
  436. else
  437. {
  438. return;
  439. }
  440. //imagecolorallocate 基于调色板的图像填充背景色
  441. //imagestring 水平地画一行字符串
  442. //imagestring(源图,字体大小,位置X,位置Y,文字,颜色)
  443. //参数($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
  444. imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
  445. //imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
  446. }
  447. //输出到文件或者浏览器
  448. switch($source_info[2])
  449. {
  450. case 1 :
  451. imagegif($source_img, $target); //以 GIF 格式将图像输出到浏览器或文件
  452. break;
  453. case 2 :
  454. imagejpeg($source_img, $target, $this->w_quality); //以 JPEG 格式将图像输出到浏览器或文件
  455. break;
  456. case 3 :
  457. imagepng($source_img, $target); //以 PNG 格式将图像输出到浏览器或文件
  458. break;
  459. default :
  460. return;
  461. }
  462. if(isset($water_info))
  463. {
  464. unset($water_info); //销毁
  465. }
  466. if(isset($water_img))
  467. {
  468. imagedestroy($water_img); //销毁
  469. }
  470. unset($source_info);
  471. imagedestroy($source_img);
  472. return true;
  473. }
  474. //gd库必须存在,后缀为jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
  475. function check($image)
  476. {
  477. return extension_loaded('gd') &&
  478. preg_match("/\.(JPG|JPEG|PNG|GIF|jpg|jpeg|gif|png)/i", $image, $m) &&
  479. file_exists($image) &&
  480. function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
  481. //imagecreatefromjpeg
  482. //imagecreatefromgif
  483. //imagecreatefrompng
  484. }
  485. }
  486. /**
  487. 缩略图
  488. 1.新建一个图像资源 通过 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
  489. 2.imagecopyresampled 拷贝图像,并调整大小
  490. 水印:图片水印,文字水印
  491. 1. 创建图像
  492. 2.加水印
  493. 图片水印:imagecopymerge 把2张图合并在一起
  494. 文字水印:imagettftext 向图像写入文字
  495. */
  496. error_reporting(0);
  497. $hostdir = dirname(__FILE__);
  498. $filesnames = scandir($hostdir.'/desktop');
  499. $img = new image();
  500. foreach($filesnames as $name){
  501. if($name != '.' && $name != '..'){
  502. $imgPath = $hostdir.'/desktop/';
  503. $imgsize = getimagesize($imgPath.$name);
  504. $imgInfo = explode('.',$name);
  505. $imginfo2 = explode('_',$imgInfo[0]);
  506. if($imginfo2[count($imginfo2)-1] != 'small'){
  507. $img_small = $imgPath.$imgInfo[0].'_small.'.$imgInfo[1];
  508. $img->thumb2($imgPath.$name,$img_small,'',$imgsize[0]/4,$imgsize[1]/4);
  509. }
  510. }
  511. }
复制代码

等比例, PHP


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

描述扎实的原则及其如何应用于PHP的开发。 描述扎实的原则及其如何应用于PHP的开发。 Apr 03, 2025 am 12:04 AM

SOLID原则在PHP开发中的应用包括:1.单一职责原则(SRP):每个类只负责一个功能。2.开闭原则(OCP):通过扩展而非修改实现变化。3.里氏替换原则(LSP):子类可替换基类而不影响程序正确性。4.接口隔离原则(ISP):使用细粒度接口避免依赖不使用的方法。5.依赖倒置原则(DIP):高低层次模块都依赖于抽象,通过依赖注入实现。

如何在系统重启后自动设置unixsocket的权限? 如何在系统重启后自动设置unixsocket的权限? Mar 31, 2025 pm 11:54 PM

如何在系统重启后自动设置unixsocket的权限每次系统重启后,我们都需要执行以下命令来修改unixsocket的权限:sudo...

解释PHP中晚期静态结合的概念。 解释PHP中晚期静态结合的概念。 Mar 21, 2025 pm 01:33 PM

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

如何用PHP的cURL库发送包含JSON数据的POST请求? 如何用PHP的cURL库发送包含JSON数据的POST请求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章讨论了框架中的基本安全功能,以防止漏洞,包括输入验证,身份验证和常规更新。

在PHPStorm中如何进行CLI模式的调试? 在PHPStorm中如何进行CLI模式的调试? Apr 01, 2025 pm 02:57 PM

在PHPStorm中如何进行CLI模式的调试?在使用PHPStorm进行开发时,有时我们需要在命令行界面(CLI)模式下调试PHP�...

See all articles