国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蚌埠市| 长丰县| 连江县| 政和县| 如皋市| 兰州市| 壤塘县| 临夏县| 谢通门县| 长乐市| 保康县| 德阳市| 陕西省| 灵璧县| 寿阳县| 景宁| 建德市| 八宿县| 西华县| 阿克苏市| 白水县| 扶余县| 汶川县| 会泽县| 定兴县| 长葛市| 攀枝花市| 合肥市| 平顺县| 马鞍山市| 舞钢市| 铜梁县| 海南省| 微博| 丹阳市| 大同市| 株洲市| 太保市| 浦城县| 含山县| 徐州市|