国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php使用GD創建保持寬高比縮略圖的方法

2024-05-04 23:34:27
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php使用GD創建保持寬高比縮略圖的方法,涉及php使用GD庫操作圖片的技巧,需要的朋友可以參考下

本文實例講述了php使用GD創建保持寬高比縮略圖的方法。分享給大家供大家參考。具體如下:

 

 
  1. /** 
  2. * Create a thumbnail image from $inputFileName no taller or wider than 
  3. * $maxSize. Returns the new image resource or false on error. 
  4. * Author: mthorn.net 
  5. */ 
  6. function thumbnail($inputFileName$maxSize = 100) 
  7. $info = getimagesize($inputFileName); 
  8. $type = isset($info['type']) ? $info['type'] : $info[2]; 
  9. // Check support of file type 
  10. if ( !(imagetypes() & $type) ) 
  11. // Server does not support file type 
  12. return false; 
  13. $width = isset($info['width']) ? $info['width'] : $info[0]; 
  14. $height = isset($info['height']) ? $info['height'] : $info[1]; 
  15. // Calculate aspect ratio 
  16. $wRatio = $maxSize / $width
  17. $hRatio = $maxSize / $height
  18. // Using imagecreatefromstring will automatically detect the file type 
  19. $sourceImage = imagecreatefromstring(file_get_contents($inputFileName)); 
  20. // Calculate a proportional width and height no larger than the max size. 
  21. if ( ($width <= $maxSize) && ($height <= $maxSize) ) 
  22. // Input is smaller than thumbnail, do nothing 
  23. return $sourceImage
  24. elseif ( ($wRatio * $height) < $maxSize ) 
  25. // Image is horizontal 
  26. $tHeight = ceil($wRatio * $height); 
  27. $tWidth = $maxSize
  28. else 
  29. // Image is vertical 
  30. $tWidth = ceil($hRatio * $width); 
  31. $tHeight = $maxSize
  32. $thumb = imagecreatetruecolor($tWidth$tHeight); 
  33. if ( $sourceImage === false ) 
  34. // Could not load image 
  35. return false; 
  36. // Copy resampled makes a smooth thumbnail 
  37. imagecopyresampled($thumb,$sourceImage,0,0,0,0,$tWidth,$tHeight,$width,$height); 
  38. imagedestroy($sourceImage); 
  39. return $thumb
  40. /** 
  41. * Save the image to a file. Type is determined from the extension. 
  42. * $quality is only used for jpegs. 
  43. * Author: mthorn.net 
  44. */ 
  45. function imageToFile($im$fileName$quality = 80) 
  46. if ( !$im || file_exists($fileName) ) 
  47. return false; 
  48. $ext = strtolower(substr($fileNamestrrpos($fileName'.'))); 
  49. switch ( $ext ) 
  50. case '.gif'
  51. imagegif($im$fileName); 
  52. break
  53. case '.jpg'
  54. case '.jpeg'
  55. imagejpeg($im$fileName$quality); 
  56. break
  57. case '.png'
  58. imagepng($im$fileName); 
  59. break
  60. case '.bmp'
  61. imagewbmp($im$fileName); 
  62. break
  63. default
  64. return false; 
  65. return true; 
  66. $im = thumbnail('temp.jpg', 100); 
  67. imageToFile($im'temp-thumbnail.jpg'); 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苍南县| 乐平市| 梁山县| 青阳县| 固始县| 安远县| 工布江达县| 舟曲县| 高邮市| 隆安县| 成都市| 田东县| 永仁县| 大足县| 信丰县| 墨玉县| 英德市| 建昌县| 鲁甸县| 锦州市| 富源县| 南木林县| 当涂县| 宜州市| 怀安县| 随州市| 安塞县| 都安| 祁连县| 台中县| 贵定县| 仪陇县| 华池县| 延安市| 长丰县| 恭城| 巍山| 柏乡县| 托克逊县| 东宁县| 济阳县|