ホームページ  >  記事  >  バックエンド開発  >  PHP 画像サムネイル生成コード (png 透明度をサポート)

PHP 画像サムネイル生成コード (png 透明度をサポート)

WBOY
WBOYオリジナル
2016-07-25 08:52:191342ブラウズ
  1. /*

  2. * desc: 画像のサイズ変更(png、jpg、gif)
  3. */
  4. class ResizeImage {
  5. //画像タイプ
  6. private $type;
  7. //実際の幅
  8. private $width;
  9. //実際の高さ
  10. private $height;
  11. //変更された幅
  12. private $resize_width;
  13. //変更された高さ
  14. private $resize_height;
  15. //画像をトリミングするかどうか
  16. private $cut;
  17. //ソース画像
  18. private $srcimg;
  19. //宛先画像アドレス
  20. private $dstimg;
  21. //一時的に作成された画像
  22. private $im;
  23. function __construct ($imgPath, $width , $height, $isCut, $savePath) {

  24. $this->srcimg = $imgPath;
  25. $this->resize_width = $width;
  26. $this->resize_height = $height; Cut = $isCut;
  27. //画像のタイプ
  28. $this->type = strto lower(substr(strrchr($this->srcimg,"." ),1));< ;/p>

  29. //画像を初期化します

  30. $this->initi_img();
  31. //ターゲット画像アドレス
  32. $this ->dst_img($savePath); >width = imagex($this->im);
  33. $this->height = imagey($this->im);
  34. //画像を生成
  35. $this ->newimg();
  36. ImageDestroy ( $this->im);
  37. }
  38. private function newimg() {

  39. //変更された画像の比率
  40. $resize_ratio = ($this->resize_width)/($ this->resize_height);
  41. //実際の画像の比率
  42. $ratio = ($this->width)/($this->height);
  43. if ($this->cut) {
  44. //画像を切り取る
  45. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  46. if($this->type=="png") {
  47. imagefill($newimg, 0, 0 , imagecolorallocatealpha($newimg, 0, 0, 0, 127));
  48. }
  49. if($ratio>=$resize_ratio) {
  50. //高優先度
  51. imagecopyresampled($newimg, $this->im, 0, 0 , 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height) ;
  52. } else {
  53. //Width first
  54. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width )/$resize_ratio));
  55. }
  56. } else {
  57. //画像をトリミングしません
  58. if($ratio>=$resize_ratio) {
  59. $newimg = imagecreatetruecolor($this-> ;resize_width,($this->) ;resize_width)/$ratio);
  60. if($this->type=="png") {
  61. imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0 , 0, 127));
  62. }
  63. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this ->高さ);
  64. } else {
  65. $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
  66. if($this- >type=="png" ) {
  67. imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
  68. }
  69. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
  70. }
  71. }
  72. if($ this->type== png") {
  73. imagesavealpha($newimg, true);
  74. imagepng ($newimg,$this->dstimg);
  75. } else {
  76. imagejpeg ($newimg,$this->dstimg);
  77. }
  78. }< ;/p>
  79. //画像初期化

  80. プライベート関数 initi_img() {
  81. if($this->type=="jpg") {
  82. $this-> ;im = imagecreatefromjpeg($this-> ;srcimg);
  83. }
  84. if($this->type=="gif") {
  85. $this->im = imagecreatefromgif($this->srcimg);
  86. }
  87. if($this-> type=="png") {
  88. $this->im = imagecreatefrompng($this->srcimg);
  89. }
  90. }
  91. / /画像ターゲットアドレス

  92. private function dst_img( $dstpath) {
  93. $full_length = strlen($this->srcimg);
  94. $type_length = strlen($this->type) ;

  95. $name_length = $full_length-$type_length ;
  96. $name = substr($this->srcimg,0,$name_length-1);
  97. $this->dstimg = $dstpath;
  98. }
  99. }
  100. ?> ;
  101. コードをコピー

それを使用するときは、クラスのコンストラクターを直接呼び出します。 $resizeimage = 新しいサイズ変更画像($imgPath, $width, $height, $isCut, $savePath);

パラメータ $imgPath: 元の画像アドレス $width: サムネイルの幅 $height: サムネイルの高さ $isCut: トリミングするかどうか、ブール値 $savePath: サムネイルのアドレス (元の画像のアドレスと同じでも構いません)

例:

  1. include "ResizeImage.php";

  2. //jpg

  3. $jpgResize = new ResizeImage("img/test_1920_1200.jpg") , 320, 240, false, "img/test_320_240.jpg");
  4. //png

  5. $pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");
  6. ?>
コードをコピー

効果: PHP 画像サムネイル生成コード (png 透明度をサポート)PHP 画像サムネイル生成コード (png 透明度をサポート)



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。