首頁 後端開發 php教程 將圖片儲存為不同規格的圖片的php程式碼

將圖片儲存為不同規格的圖片的php程式碼

Jul 25, 2016 am 09:03 AM

  1. /**

  2. 圖片處理類別
  3. */
  4. class imagecls
  5. {
  6. / **
  7. * 文件資訊
  8. */
  9. var $file = array();
  10. /**
  11. * 保存目錄
  12. */
  13. var $dir = '';
  14. /**
  15. * 錯誤代碼
  16. */
  17. var $error_code = 0;
  18. /**
  19. * 檔案上傳最大KB
  20. */
  21. var $max_size = -1;
  22. function es_imagecls()

  23. {
  24. }

  25. 蕨函數checkSize($size)
  26. {
  27. return !($size > $this->max_size) || (-1 == $this->max_size);
  28. }
  29. /**
  30. * 處理上傳檔案
  31. * @param array $file 上傳的檔案
  32. * @param string $dir 儲存的目錄
  33. * @return bool
  34. */
  35. function init($file, $dir = 'temp')
  36. {
  37. if(!is_array($file) || 空($file) || !$this->isUploadFile($file['tmp_name']) || trim($file['name']) == ' ' || $file['size'] == 0)
  38. {
  39. $this->file = array();
  40. $this->error_code = -1;
  41. 回傳false;
  42. }
  43. else
  44. {
  45. $file['size'] = intval($file['size']);
  46. $file['name'] = trim( $file[' name']);
  47. $file['thumb'] = '';
  48. $file['ext'] = $this->fileExt($file['name']);
  49. $file['name'] = htmlspecialchars($file['name'], ENT_QUOTES);
  50. $file['is_image'] = $this->isImageExt($file['ext']) ;
  51. $file['file_dir'] = $this->getTargetDir($dir);
  52. $file['prefix'] = md5(microtime(true)).rand(10,99);
  53. $file['target'] = "./public/".$file['file_dir'].'/'.$file['prefix'].'.jpg'; //相對
  54. $file['local_target'] = APP_ROOT_PATH."public/".$file['file_dir'].'/'.$file['prefix'].'.jpg';//物理
  55. $this->file = &$file;
  56. $this->error_code = 0;
  57. return true;
  58. }
  59. }
  60. ;/* *

  61. * 儲存檔案
  62. * @return bool
  63. */
  64. 函數save()
  65. {
  66. if(empty($this->file) ||empty($this->file['tmp_name']) )
  67. $ this ->error_code = -101;
  68. elseif(!$this->checkSize($this->file['size']))
  69. $this->error_code = -105;
  70. elseif(! $ this->file['is_image'])
  71. $this->error_code = -102;
  72. elseif(!$this->saveFile($this->file['tmp_name'], $this-> file ['local_target']))
  73. $this->error_code = -103;
  74. elseif($this->file['is_image' ] &&
  75. (!$this->file['image_info' ] = $this->getImageInfo($this->file['local_target'], true)))
  76. {
  77. $ this->error_code = -104;
  78. @unlink($this->file [ 'local_target']);
  79. }
  80. else
  81. {
  82. $this->error_code = 0;
  83. 回傳true;
  84. }
  85. 回傳false;
  86. }
  87. }
  88. 回傳false
  89. /**

  90. * 取得錯誤代碼
  91. * @return number
  92. */
  93. 函數error()
  94. {
  95. return $this->error_code;
  96. }
  97. /**
  98. * 取得檔案副檔名
  99. * @return string
  100. */
  101. function fileExt($file_name)
  102. {
  103. return addslashes(strtolower(substr(strrchr($file_name, '.'), 1, 10))) ;
  104. }
  105. /**

  106. * 根據副檔名判斷檔案是否為圖片
  107. * @param string $ext 副檔名
  108. * @return bool
  109. */
  110. function isImageExt($ext)
  111. {
  112. static $img_ext = array('jpg', ' jpeg ', 'png', 'bmp','gif', 'giff');
  113. 回傳 in_array($ext, $img_ext) ? 1 : 0;
  114. }
  115. /**

  116. * 取得映像資訊
  117. * @param string $target 檔案路徑
  118. * @return mixed
  119. */
  120. function getImageInfo($target)
  121. {
  122. $ext = es_imagecls::fileExt(> {
  123. $ext = es_imagecls::fileExt(> {
  124. $ext = es_imagecls::fileExt( $target);
  125. $is_image = es_imagecls::isImageExt($ext) ;
  126. if(!$is_image)

  127. return false;
  128. elseif(!is_read($is_read ) target))
  129. 回傳false;
  130. elseif($image_info = @getimagesize($target))
  131. {
  132. 列表($width, $height, $type) = !empty($image_info) ? $image_info :
  133. array('', '', '');
  134. $size = $width * $height;
  135. if($is_image && !in_array($type, array(1,2, 3,6,13)))
  136. 回傳false;
  137. $image_info['type'] =

  138. strtolower (substr(image_type_to_extension($image_info[2]),1) );
  139. return $image_info;
  140. }
  141. else
  142. return false;
  143. }
  144. /**

  145. * 取得是否充值上傳檔案
  146. * @param string $source 檔案路徑
  147. * @return bool
  148. */
  149. functionisloadFile ($source) { return $source && ($source != 'none') && (is_uploaded_file($source) || is_uploaded_file (str_replace('\', '', $source) )); }
  150. /**

  151. * 取得已儲存的路徑
  152. * @param string $dir 指定的保存目錄
  153. * @return string
  154. */
  155. function getTargetDir($dir)
  156. {
  157. if (!is_dir(APP_ROOT_PATH."public/".$dir)) {
  158. @mkdir(APP_ROOT_PATH." public/".$dir);
  159. @chmod(APP_ROOT_PATH."public/".$dir, 0777);
  160. }
  161. return $dir;
  162. }
  163. /**

  164. * 儲存檔案
  165. * @param string $source 來源檔案路徑
  166. * @param string $target 目錄檔案路徑
  167. * @return bool
  168. */
  169. 原生函式saveFile($source, $target)
  170. {
  171. if(!es_imagecls::isUploadFile($source ) ))
  172. $succeed = false;
  173. elseif(@copy($source, $target))
  174. $succeed = true;
  175. elseif(function_exists('move_uploaded_file') & $成功= true;
  176. elseif (@is_read($source) &&
  177. (@$fp_s = fopen($source, 'rb')) && (@ $ fp_t = fopen($target, 'wb')) )
  178. {
  179. while (!feof($fp_s))
  180. {
  181. $s = @fread($fp_s, 1024 * 512) ;
  182. @fwrite($fp_t, $s) ;
  183. }
  184. fclose($fp_s);
  185. fclose($fp_t);
  186. $succeed = true;
  187. } p >
  188. if($succeed)

  189. {
  190. $this->error_code = 0;
  191. @chmod($target, 0644);
  192. @unlink($source);
  193. }
  194. else
  195. {
  196. $this->error_code = 0;
  197. }
  198. 回傳$succeed;

  199. }
  200. 回傳$succeed;

  201. }
  202. 公用函數拇指($image,$maxWidth=200,$maxHeight=50,$gen = 0,

  203. $interlace=true,$filepath = '',$is_preview = true)
  204. {
  205. $info = es_imagecls::getImageInfo($image);
  206. if($info !== false)

  207. {
  208. $srcWidth = $info[0];
  209. $srcHeight = $info[1];
  210. $type = $info['type'] ;
  211. $交錯= $交錯?設定($info);

  212. if($maxWidth > 0 && $maxHeight > 0)

  213. $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight);
  214. // 計算縮放比例
  215. elseif($maxWidth == 0)
  216. $scale = $maxHeight/$srcHeight;
  217. elseif($maxHeight == 0)
  218. $scale = $maxWidth /$srcWidth;
  219. $paths = pathinfo($image);
  220. $paths['filename'] = trim(strtolower($paths['basename']),
  221. ".".strtolower($paths['extension) ']));
  222. $basefilename =explode("_",$paths['filename']);
  223. $basefilename = $basefilename[ 0];
  224. if(empty($filepath))
  225. {
  226. if($is_preview)
  227. $thumbname = $paths['dirname'].'/'.$basefilename.
  228. '_'.$maxWidth.'x'.$maxHeight.' .jpg';
  229. else
  230. $thumbname = $paths['dirname'].'/'.$basefilename.
  231. 'o_'.$maxWidth.'x'.$maxHeight.'.jpg' ;
  232. }
  233. else
  234. $thumbname = $filepath;
  235. $thumburl = str_replace(APP_ROOT_PATH,'./',$thumbname);

  236. if($scale >= 1)
  237. {
  238. // 超過原圖大小不再縮略
  239. $width = $srcWidth;
  240. $height = $srcHeight;
  241. if(!$is_preview)
  242. {
  243. //非預覽模式寫入原圖
  244. file_put_contents($thumbname,file_get_contents($image)); //用原圖寫入
  245. return array('url'=>$thumburl,'path'=>$thumbname);
  246. }
  247. }
  248. else
  249. {
  250. / / 最大尺寸
  251. $width = (int)($srcWidth*$scale);
  252. $height = (int)($srcHeight*$scale);
  253. }
  254. if($ gen == 1)
  255. {
  256. $width = $maxWidth;
  257. $height = $maxHeight; }
  258. // 載入原圖

  259. $createFun = 'imagecreatefrom'.($type=='jpg'?'jpeg':$type);
  260. if(!function_exists( $createFun))
  261. $createFun = 'imagecreatefromjpeg';
  262. $srcImg = $createFun($image);

  263. // 建立每個

  264. if($type!='gif' && function_exists('imagecreatetruecolor'))
  265. $thumbImg = imagecreatetruecolor($width, $height);
  266. else
  267. $thumbImg = idage,$3, $.高度);
  268. $x = 0;

  269. $y = 0;
  270. if($gen == 1 && $maxWidth > 0 && $ maxHeight > 0)

  271. {
  272. $resize_ratio = $maxWidth/$maxHeight;
  273. $src_ratio = $srcWidth/$srcHeight;
  274. if($src_ratio >= $resize_ratio >= $hresize > $x = ($srcWidth - ($resize_ratio * $srcHeight)) / 2;
  275. $width = ($height * $srcWidth) / $srcHeight;
  276. }
  277. else
  278. {
  279. $y = ($srcHeight - ( (1 / $resize_ratio) * $srcWidth)) / 2;
  280. $height = ($width * $srcHeight) / $srcWidth;
  281. }
  282. } p>
  283. // 複製圖片

  284. if(function_exists("imagecopyresampled"))
  285. imagecopyresampled($thumbImg, $srcImg, 0, 0, $x, $y, $height, $height, $height ,
  286. $srcWidth,$srcHeight);
  287. else
  288. imagecopyresized($thumbImg, $srcImg, 0, 0, $x, $y, $width, $height,
  289. $srcWidight );
  290. if('gif'==$type || 'png'==$type) {
  291. $background_color = imagecolorallocate($thumbImg, 0,255,0); // 指派一個綠色
  292. imagecolortransparent($thumbImg,$background_color);
  293. // 設定為透明顏色,註解若掉該行則輸出綠色的圖
  294. }
  295. // 對jpeg圖形設定隔行掃描

  296. if('jpg' ==$type || 'jpeg'==$type)
  297. imageinterlace($thumbImg,$interlace);
  298. // 產生圖片

  299. imagejpeg($thumbImg,$thumbname ,100);
  300. imagedestroy($thumbImg);
  301. imagedestroy($srcImg);
  302. return array('url'=>$thumburl,'path'=>$thumbname=>$thumbname=>$thumbname=>$thumbname=$ );

  303. }
  304. return false;
  305. }
  306. 公用函數make_thumb($srcImg,$srcWidth,$srcHeight,$type,$maxWidth=200,$srcWidth,$srcHeight,$type,$maxWidth=200,

  307. $maxHeight=50,$gen = 0)
  308. {
  309. $交錯= $交錯? && $maxHeight > 0)

  310. $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight);
  311. // 計算縮放比例
  312. elseif($maxWidth == 0)
  313. $
  314. elseif($maxWidth == 0)
  315. $
  316. $縮放= $maxHeight/$srcHeight;
  317. elseif($maxHeight == 0)
  318. $scale = $maxWidth/$srcWidth;
  319. ; if($scale >= 1)

  320. {
  321. // 超過原圖大小不再縮略
  322. $width = $srcWidth;
  323. $height = $srcHeight;
  324. }
  325. else
  326. {
  327. // 最重要的尺寸
  328. $width = (int)($srcWidth*$scale);
  329. $height = (int)($srcHeight*$scale);
  330. }
  331. if($gen == 1)

  332. {
  333. $width = $maxWidth;
  334. $height = $maxHeight;
  335. }
  336. / /創建大概
  337. if($type!='gif' && function_exists('imagecreatetruecolor'))
  338. $thumbImg = imagecreatetruecolor($width, $height); else
  339. $thumbImg = imagecreatetruecolor ($寬度, $高度);
  340. $x = 0;

  341. $y = 0;
  342. if ($gen == 1 && $maxWidth > 0 && $maxHeight > 0)

  343. {
  344. $resize_ratio = $maxWidth/$maxHeight;
  345. $src_ratio = $srcWidth/$srcHeight;
  346. if ( $src_ratio >= $resize_ratio)
  347. {
  348. $x = ($srcWidth - ($resize_ratio * $srcHeight)) / 2;
  349. $width = ($height * $srcWidth) / $srcHeightth);
  350. }
  351. else
  352. {
  353. $y = ($srcHeight - ( (1 / $resize_ratio) * $srcWidth)) / 2;
  354. $height = ($width * $srcHeight) * $srcHeight) / $srcWidth;
  355. }
  356. }
  357. // 複製圖片

  358. if(function_exists("imagecopyresampled"))
  359. imagecopyresampled($thumbImg, $srcImg, 0, 0, $x, $ $height,
  360. $srcWidth,$srcHeight);
  361. else
  362. imagecopyresized($thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, if('gif'==$type || 'png'==$type) {
  363. $background_color = imagecolorallocate($thumbImg, 255,255,255);
  364. // 指派一個綠色
  365. imagecolortransparent($thumbImg,$background_color);
  366. // 設定為透明色,若註解掉該行則輸出綠色的圖
  367. }
  368. // 對jpeg圖形設定隔行掃描

  369. if('jpg'==$type || 'jpeg'==$type)
  370. imageinterlace($thumbImg,$interlace);
  371. return $thumbImg ;

  372. }

  373. public function water($source,$water,$alpha=80,$position="0")
  374. {
  375. //檢查檔案是否存在
  376. if(!file_exists($source)||!file_exists($water))
  377. return false;
  378. //圖片資訊

  379. $ sInfo = es_imagecls::getImageInfo($source);
  380. $wInfo = es_imagecls::getImageInfo($water);
  381. //如果圖片小於水印圖片,則不產生圖片

  382. if($sInfo["0"] return false;
  383. if(is_animated_gif($source))
  384. {
  385. require_once APP_ROOT_PATH."system/utils/gif_encoder.php";
  386. require_onceAPPAPP APP_/ ;
  387. $gif = new GIFReader();

  388. $gif->load($source);
  389. foreach($gif->IMGS['frames'] as $k =>$img)
  390. {
  391. $im = imagecreatefromstring($gif->getgif($k));
  392. //為im加水印
  393. $sImage=$im;
  394. $ wCreateFun="imagecreatefrom".$wInfo['type'];
  395. if(!function_exists($wCreateFun))
  396. $wCreateFun = 'imagecreatefromjpeg'; $wImage 🎜>
  397. //設定影像的混色模式
  398. imagealphablending($wImage, true);
  399. switch (intval($position))
  400. {
  401. case 0: break;
  402. //左上
  403. case 1:
  404. $posY=0;
  405. $posX=0;
  406. //產生混合影像
  407. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  408. break;
  409. //右上
  410. case 2:
  411. $posY=0;
  412. $posX =$sInfo[0]-$wInfo[0];
  413. //產生混合映像
  414. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo [1],$alpha);
  415. break;
  416. //左下
  417. case 3:
  418. $posY=$sInfo[1]-$wInfo[1];
  419. $posX=0 ;
  420. //產生混合映像
  421. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  422. break ;
  423. //右下
  424. case 4:
  425. $posY=$sInfo[1]-$wInfo[1];
  426. $posX=$sInfo[0]-$wInfo[0];
  427. //產生混合映像
  428. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  429. break;
  430. //居中
  431. case 5:
  432. $posY=$sInfo[1]/2-$wInfo[1]/2;
  433. $posX=$sInfo[0]/2-$wInfo [0]/2;
  434. //產生混合映像
  435. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha) ;
  436. break;
  437. }
  438. //end im加浮水印
  439. ob_start();
  440. imagegif($sImage);
  441. $content = ob_get_contents() ;
  442. ob_end_clean();
  443. $frames [ ] = $content;
  444. $framed [ ] = $img['frameDelay'];
  445. }
  446. $gif_? = new GIFEncoder (
  447. $frames,
  448. $framed,
  449. 0,
  450. 2,
  451. 0, 0, 0,
  452. "bin" //bin為二進位url為位址
  453. );
  454. $image_rs = $gif_maker->GetAnimation ( );
  455. //如果沒有給出保存檔名,預設為原始圖像名稱
  456. @unlink($source);
  457. //儲存圖片
  458. file_put_contents($source,$image_rs);
  459. return true;
  460. }
  461. //建立映像
  462. $sCreateFun="imagecreatefrom".$sInfo['type'];
  463. if(!function_exists($sCreateFun))
  464. $sCreateFunfimage;
  465. $sImage=$sCreateFun($source);
  466. $wCreateFun="imagecreatefrom".$wInfo['type'];

  467. if(!function_exists($wCreateFun))
  468. $wCreateFun = 'imagecreatefromjpeg';
  469. $wImage=$wCreateFun($water);
  470. //設定影像的混色模式

  471. imagealphablending(truewImage, true );
  472. switch (intval($position))

  473. {
  474. case 0: break;
  475. //左上
  476. case 1:
  477. $posY= 0;
  478. $posX=0;
  479. //產生混合圖片
  480. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1] ,$alpha);
  481. break;
  482. //右邊
  483. case 2:
  484. $posY=0;
  485. $posX=$sInfo[0]-$wInfo[0];
  486. //產生混合圖
  487. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  488. break;
  489. //左下
  490. case 3:
  491. $posY=$sInfo[1]-$wInfo[1];
  492. $posX=0;
  493. //產生混合圖
  494. imagecopymerge($ sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  495. break;
  496. //右邊
  497. case 4:
  498. $posY=$sInfo[1]-$wInfo[1];
  499. $posX=$sInfo[0]-$wInfo[0];
  500. //產生混合圖片
  501. imagecopymerge($sImage , $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  502. break;
  503. //居中
  504. case 5:
  505. $posY=$sInfo[1]/2-$wInfo[1]/2;
  506. $posX=$sInfo[0]/2-$wInfo[0]/2;
  507. //產生混合映像
  508. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  509. break;
  510. }
  511. //若沒有給予儲存檔名,預設為原始圖片名稱

  512. @unlink($source);
  513. //儲存圖片
  514. imagejpeg($sImage,$source,100) ;
  515. imagedestroy($sImage);
  516. }
  517. }
  518. if(!function_exists('image_type_to_extension'))

  519. {
  520. f )
  521. {
  522. if(empty($imagetype))
  523. return false;
  524. switch($imagetype)

  525. {
  526. case IMAGETYPE_G IF: return '. gif';
  527. case IMAGETYPE_JPEG : return '.jpeg';
  528. case IMAGETYPE_PNG : return '.png';
  529. case IMAGETYPE_SWF : return '.png';
  530. case IMAGETYPE_SWF : return '.swf';
  531. case IMAGETYPE_SWF : return '。 ;
  532. case IMAGETYPE_BMP : return '.bmp';
  533. case IMAGETYPE_TIFF_II : return '.tiff';
  534. case IMAGETYPE_TIFF_MM : return '.tiff';
  535. case IMAGETYPE_TIFF_MM : return '.tiff';
  536. case IMAGETYPE_TIFF_MM : return '.tiff';
  537. case IMAGETYPE_JP2 : return '.jp2';
  538. case IMAGETYPE_JPX : return '.jpf';
  539. case IMAGETYPE_JB2 : return '.jb2';
  540. case IMAGETYPE_JB2 : return '.jb2';
  541. > case IMAGETYPE_IFF : return '.aiff';
  542. case IMAGETYPE_WBMP : return '.wbmp';
  543. case IMAGETYPE_XBM : return '.xbm';
  544. default : return default; 🎜>}
  545. ?>
複製程式碼

2.get_spec_img()呼叫圖片類別,然後再用下面的方法保存不同規格的圖片並返回圖片連接

  1. //取得對應規格的圖片地址
  2. //gen=0:保持比例縮放,不剪裁,如高為0,則保證寬度按比例縮放gen=1:保證長寬,剪裁
  3. function get_spec_image($img_path,$width=0,$height=0,$gen=0,$is_preview=true)
  4. {
  5. if($width==0)
  6. $new_path = $img_path;
  7. else
  8. {
  9. $img_name = substr($img_path,0,-4);
  10. $ img_ext = substr($img_path,-3);
  11. if($is_preview)
  12. $new_path = $img_name."_".$width."x".$height.".jpg";
  13. else
  14. $new_path = $img_name."o_".$width."x".$height.".jpg";
  15. if(!file_exists($new_path))
  16. {
  17. require_once " imagecls.php";
  18. $imagec = new imagecls();
  19. $thumb = $imagec->thumb($img_path,$width,$height,$gen,true,"
  20. ",$is_preview );
  21. if(app_conf("PUBLIC_DOMAIN_ROOT")!='')
  22. {
  23. $paths = pathinfo($new_path);
  24. $path = str_replace("./", "",$paths['dirname']);
  25. $filename = $paths['basename'];
  26. $pathwithoupublic = str_replace("public/","",$path);
  27. $file_data = @file_get_contents($path.$file);
  28.     $img = @imagecreatefromstring($file_data);
  29.     $img = @imagecreatefromstring($file_data);
  30.     if($ "public/".$path;
  31.       if(!is_dir($save_path))
  32.       {
  33.           
  34.       @file_put_contents($save_path.$name, $file_data);
  35.     }
  36. }
  37. }
  38. }
  39. return $new_path;
  40. }
複製程式碼🎜>}
>
3.使用方法:

  1. //im:將店鋪圖片儲存為3種規格:小圖:48x48,中圖120x120,大圖200x200
  2. $small_url ($data['image'],48,48,0);
  3. $
  4. middle_url=get_spec_image($data['image'],120,120,0);
  5. $big_url=get_spec_image($data['image'],200,200,0);
複製代碼

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡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脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++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 06, 2025 am 12:02 AM

會話劫持可以通過以下步驟實現:1.獲取會話ID,2.使用會話ID,3.保持會話活躍。在PHP中防範會話劫持的方法包括:1.使用session_regenerate_id()函數重新生成會話ID,2.通過數據庫存儲會話數據,3.確保所有會話數據通過HTTPS傳輸。

描述紮實的原則及其如何應用於PHP的開發。 描述紮實的原則及其如何應用於PHP的開發。 Apr 03, 2025 am 12:04 AM

SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。

在PHPStorm中如何進行CLI模式的調試? 在PHPStorm中如何進行CLI模式的調試? Apr 01, 2025 pm 02:57 PM

在PHPStorm中如何進行CLI模式的調試?在使用PHPStorm進行開發時,有時我們需要在命令行界面(CLI)模式下調試PHP�...

PHP 8.1中的枚舉(枚舉)是什麼? PHP 8.1中的枚舉(枚舉)是什麼? Apr 03, 2025 am 12:05 AM

PHP8.1中的枚舉功能通過定義命名常量增強了代碼的清晰度和類型安全性。 1)枚舉可以是整數、字符串或對象,提高了代碼可讀性和類型安全性。 2)枚舉基於類,支持面向對象特性,如遍歷和反射。 3)枚舉可用於比較和賦值,確保類型安全。 4)枚舉支持添加方法,實現複雜邏輯。 5)嚴格類型檢查和錯誤處理可避免常見錯誤。 6)枚舉減少魔法值,提升可維護性,但需注意性能優化。

如何在系統重啟後自動設置unixsocket的權限? 如何在系統重啟後自動設置unixsocket的權限? Mar 31, 2025 pm 11:54 PM

如何在系統重啟後自動設置unixsocket的權限每次系統重啟後,我們都需要執行以下命令來修改unixsocket的權限:sudo...

解釋PHP中的晚期靜態綁定(靜態::)。 解釋PHP中的晚期靜態綁定(靜態::)。 Apr 03, 2025 am 12:04 AM

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

See all articles