搜尋
首頁後端開發php教程php取得遠端圖片url產生縮圖的方法

  1. /**

  2. *
  3. *函數:調整圖片尺寸或產生縮圖
  4. *返回:True/False
  5. *參數:
  6. * $Image 需要調整的圖片(含路徑)
  7. * $Dw=450 調整時最大寬度;縮圖時的絕對寬度
  8. * $Dh=450 調整時最大高度;縮圖時的絕對高度
  9. * $Type=1 1,調整尺寸; 2,生成縮圖
  10. */ bbs.it-home.org
  11. $phtypes=array( 'img/gif', 'img/jpg', 'img/jpeg', 'img/bmp', 'img/pjpeg', 'img/x-png');
  12. function compressImg($Image,$Dw,$Dh,$Type){

  13. IF(!file_exists($Image)){
  14. return false;
  15. }
  16. // 如果需要產生縮圖,則將原圖拷貝一下重新給$Image賦值(生成縮圖操作)
  17. // 當Type==1的時候,將不拷貝原始圖像文件,而是在原來的圖像文件上重新生成縮小後的圖像(調整尺寸操作)
  18. IF($Type!=1){
  19. copy($Image,str_replace(".","_x.",$Image));
  20. $Image=str_replace(" .","_x.",$Image);
  21. }
  22. // 取得檔案的類型,依照不同的型別建立不同的物件
  23. $ImgInfo=getimagesize($Image);
  24. Switch ($ImgInfo[2]){
  25. case 1:
  26. $Img =@imagecreatefromgif($Image);
  27. break;
  28. case 2:
  29. $Img =@imagecreatefromjpeg($Image) ;
  30. Break;
  31. case 3:
  32. $Img =@imagecreatefrompng($Image);
  33. break;
  34. }
  35. // 如果物件沒有建立成功,則說明非圖片檔案
  36. IF(Empty($Img)){
  37. // 如果是產生縮圖的時候出錯,則需要刪除已經複製的檔案
  38. IF($Type!=1){
  39. unlink ($Image);
  40. }
  41. return false;
  42. }
  43. // 如果是執行調整尺寸操作則
  44. IF($Type==1){
  45. $w=ImagesX ($Img);
  46. $h=ImagesY($Img);
  47. $width = $w;
  48. $height = $h;
  49. IF($width>$Dw){
  50. $Par=$Dw/$width;
  51. $width=$Dw;
  52. $height=$height*$Par;
  53. IF($height>$Dh){
  54. $Par=$Dh /$height;
  55. $height=$Dh;
  56. $width=$width*$Par;
  57. }
  58. } ElseIF($height>$Dh) {
  59. $Par=$Dh /$height;
  60. $height=$Dh;
  61. $width=$width*$Par;
  62. IF($width>$Dw){
  63. $Par=$Dw/$width;
  64. $width=$Dw;
  65. $height=$height*$Par;
  66. }
  67. } Else {
  68. $width=$width;
  69. $height=$height;
  70. }
  71. $nImg =ImageCreateTrueColor($width,$height);// 新建一個真彩色畫布
  72. ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$ w,$h);// 重採樣拷貝部分影像並調整大小
  73. ImageJpeg($nImg,$Image);// 以JPEG格式將影像輸出到瀏覽器或檔案
  74. return true;
  75. } Else {// 如果是執行生成縮圖操作則
  76. $w=ImagesX($Img);
  77. $h=ImagesY($Img);
  78. $width = $w;
  79. $ height = $h;
  80. $nImg =ImageCreateTrueColor($Dw,$Dh);
  81. IF($h/$w>$Dh/$Dw){// 高比較大
  82. $width=$ Dw;
  83. $height=$h*$Dw/$w;
  84. $IntNH=$height-$Dh;
  85. ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
  86. } Else {// 寬比較大
  87. $height=$Dh;
  88. $width=$w*$Dh/$h;
  89. $IntNW=$width-$Dw;
  90. ImageCopyReSampled($nImg, $Img,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
  91. }
  92. ImageJpeg($nImg,$Image);
  93. return true;
  94. }
  95. };
  96. /**
  97. *根據url取得伺服器上的圖片
  98. *$url伺服器上圖片路徑 $filename檔名
  99. */
  100. function GrabImage($ url,$filename="") {
  101. if($url=="") return false;
  102. if($filename=="") {
  103. $ext=strrchr($url,". ");
  104. if($ext!=".gif" && $ext!=".jpg" && $ext!=".png")
  105. return false;
  106. $filename=date(" YmdHis").$ext;
  107. }
  108. ob_start();
  109. readfile($url);
  110. $img = ob_get_contents();
  111. ob_end_clean();
  112. $size = strlen($img);
  113. $fp2=@fopen($filename, "a");
  114. fwrite($fp2,$img);
  115. fclose($fp2);
  116. return $filename;
  117. }
  118. ?>
複製程式碼

呼叫方法:

  1. //網路圖片路徑
  2. $imgPath = 'http://news.xxxx.cn/images/13820884444437. jpg';//遠端URL 位址
  3. $tempPath = 'aa/bbs.jpg';//儲存圖片路徑
  4. if(is_file($tempPath)){
  5. unlink($tempPath) ;
  6. }
  7. $bigImg=GrabImage($imgPath, $tempPath);
  8. compressImg($bigImg,70,70,1);
  9. ?>
複製程式碼


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
高流量網站的PHP性能調整高流量網站的PHP性能調整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依賴注入:初學者的代碼示例PHP中的依賴注入:初學者的代碼示例May 14, 2025 am 12:08 AM

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

PHP性能:是否可以優化應用程序?PHP性能:是否可以優化應用程序?May 14, 2025 am 12:04 AM

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

PHP性能優化:最終指南PHP性能優化:最終指南May 14, 2025 am 12:02 AM

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

熱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

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

熱門文章

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具