PHP 이미지 썸네일 생성 코드(PNG 투명도 지원)

WBOY
풀어 주다: 2016-07-25 08:52:19
원래의
1362명이 탐색했습니다.
  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. / /소스 image
  18. private $srcimg;
  19. //대상 이미지 주소
  20. private $dstimg;
  21. //임시 생성 이미지
  22. private $im;
  23. 함수 __construct($imgPath, $width, $height, $isCut, $savePath) {

  24. $this->srcimg = $imgPath;
  25. $this->resize_width = $width ;
  26. $this ->resize_height = $height;
  27. $this->cut = $isCut;
  28. //사진 유형
  29. $this- >type = strtolower(substr (strrchr($this->srcimg,"."),1));

  30. //이미지 초기화

  31. $this-> ;initi_img();
  32. //대상 이미지 주소
  33. $this -> dst_img($savePath);
  34. //--
  35. $this->width = Imagesx($this- >im);
  36. $ this->height = imagey($this->im);
  37. //이미지 생성
  38. $this->newimg();
  39. ImageDestroy( $this->im);
  40. }
  41. 비공개 함수 newimg() {

  42. //이미지 비율 변경
  43. $resize_ratio = ($ this->resize_width)/($this->resize_height );
  44. //실제 이미지의 비율
  45. $ratio = ($this->width)/($this->height)
  46. if($this->cut);
  47. //이미지 자르기
  48. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  49. if($this ->type=="png") {
  50. imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
  51. }
  52. if($ratio> =$resize_ratio) {
  53. //높은 우선순위
  54. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, ( ($this->height)*$resize_ratio), $this->height);
  55. } else {
  56. //너비 우선
  57. imagecopyresampled($newimg, $this->im, 0 , 0, 0, 0, $this->resize_width , $this->resize_height, $this->width, (($this->width)/$resize_ratio));
  58. }
  59. } else {
  60. //이미지를 자르지 마세요.
  61. if($ratio>=$resize_ratio) {
  62. $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio );
  63. if($this-> ;type=="png") {
  64. imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
  65. }
  66. imagecopyresampled($newimg, $this ->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
  67. } else {
  68. $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
  69. if($this- >type=="png") {
  70. imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
  71. }
  72. imagecopyresampled($newimg, $ this->im, 0, 0, 0 , 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
  73. }
  74. }
  75. if( $this->type=="png") {
  76. imagesavealpha($newimg, true);
  77. imagepng($newimg,$this->dstimg) ;
  78. } else {
  79. imagejpeg ($newimg,$this->dstimg);
  80. }
  81. }
  82. //이미지 초기화

  83. 비공개 함수 initi_img() {
  84. if( $this->type=="jpg") {
  85. $this->im = imagecreatefromjpeg($this->srcimg);
  86. }
  87. if($this->type== "gif") {
  88. $this->im = imagecreatefromgif($this->srcimg);
  89. }
  90. if($this-> ;type=="png") {
  91. $this->im = imagecreatefrompng($this->srcimg);
  92. }
  93. }
  94. / /이미지 대상 주소

  95. 비공개 함수 dst_img($ dstpath) {
  96. $full_length = strlen($this->srcimg);
  97. $type_length = strlen($ this->type);

  98. $name_length = $full_length-$type_length;
  99. $name = substr($this->srcimg,0,$name_length -1);
  100. $this->dstimg = $dstpath;
  101. }
  102. }
  103. ?>
코드 복사

사용할 때 클래스 생성자를 직접 호출하면 됩니다. $resizeimage = 새로운 크기 조정 이미지($imgPath, $width, $height, $isCut, $savePath);

매개변수 $imgPath: 원본 이미지 주소 $width: 썸네일 너비 $height: 썸네일 높이 $isCut: 자르기 여부, bool 값 $savePath: 썸네일 주소(원본 이미지 주소와 동일할 수 있음)

예:

  1. include "ResizeImage.php";

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

  3. // png

  4. $pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");
  5. ?>
코드 복사

효과: PHP 이미지 썸네일 생성 코드(PNG 투명도 지원) PHP 이미지 썸네일 생성 코드(PNG 투명도 지원)



원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!