백엔드 개발 PHP 튜토리얼 썸네일과 워터마크를 구현하는 PHP 클래스

썸네일과 워터마크를 구현하는 PHP 클래스

Jul 25, 2016 am 08:45 AM

  1. /**
  2. * 이미지 스케일링 워터마크 클래스
  3. *
  4. * @version 1.0;
  5. *
  6. */
  7. class cls_photo
  8. {
  9. protected $waterrate = 0.2; //이미지에 대한 워터마크 아이콘의 비율
  10. protected $width = 300; //썸네일의 기본 너비
  11. protected $height = 200; //썸네일의 기본 높이
  12. protected $padding = 5 //워터마크에서 가장자리까지의 거리
  13. protected $ water_mark = "./water.png";
  14. protected $water_mark_pos = 5;//워터마크 이미지 위치 (1=왼쪽 위, 2=오른쪽 위, 3=왼쪽 아래, 4=오른쪽 아래, 5 center)
  15. protected $watermode = 0; // 워터마크가 없는 썸네일 0개 워터마크가 있는 썸네일 1개
  16. protected $magick_handle; // 그림 작업 핸들
  17. protected $format = array('jpg','gif', 'png','jpeg'); // 사진 파일 형식 제한
  18. protected $smallpic_mode = 2; //기본 모드 0은 썸네일을 생성하지 않고, 1은 자르기 및 크기 조절, 2는 비례 크기 조절, 3은 크기 조절 및 채우기 모드
  19. /**
  20. * 이미지 매개변수 설정
  21. *
  22. * @param $arg 이미지 매개변수는 다음과 같이 배열에 여러 번 입력할 수 있습니다.
  23. * @param $protected 매개변수 값
  24. * array(
  25. * 'waterrate'=>0.2,
  26. * 'water_mark'=>'./water.png',
  27. * 'water_mark_pos'=>4,
  28. * 'smallpic_mode'=>1
  29. * );
  30. * @return ture/false
  31. */
  32. 공개 함수 set_args($arg,$val="")
  33. {
  34. $params = array('waterrate','water_mark ', 'water_mark_pos','smallpic_mode','watermode','width','height');
  35. if(is_array($arg))
  36. {
  37. foreach ($arg as $k => ;$ v)
  38. {
  39. if(in_array($k,$params))
  40. {
  41. $this->$k = $v;
  42. }
  43. }
  44. }
  45. else
  46. {
  47. if(empty($val))
  48. {
  49. return false;
  50. }
  51. else
  52. {
  53. if(in_array( $arg ,$params))
  54. {
  55. $this->$arg = $val;
  56. }
  57. }
  58. }
  59. return true;
  60. }
  61. /**
  62. * 이미지 크기 조정
  63. *
  64. * @param $src_file 소스 파일 경로
  65. * @param $dst_file 대상 파일 경로
  66. * @return 썸네일 이미지 경로/false
  67. */
  68. 공용 함수 scale($src_file,$dst_file="")
  69. {
  70. $dst_width = $this->width;
  71. $dst_height = $this ->height;
  72. $mode = $this->smallpic_mode;
  73. $magic_water_handle = NewMagickWand();
  74. if (!MagickReadImage($magic_water_handle, $src_file))return false;
  75. //유형
  76. $srcext = strtolower(MagickGetImageFormat($magic_water_handle));
  77. if($srcext=='bmp')
  78. {
  79. $srcext = 'jpeg';
  80. }
  81. if(!in_array($srcext,$this->format))return false;
  82. //Size
  83. $src_width = MagickGetImageWidth($magic_water_handle);
  84. $src_height = MagickGetImageHeight ($ Magic_water_handle);
  85. //자르기 크기 조정 모드
  86. if($mode == 1)
  87. {
  88. $pos_x=$pos_y = 0;//임시 위치 자르기
  89. $src_widthc = $src_width;//임시 너비 자르기
  90. $src_heightc = $src_height;//임시 높이 자르기
  91. if($src_width/$src_height>$dst_width/$dst_height)
  92. {
  93. $ src_widthc = $src_height*$dst_width/$dst_height;
  94. $pos_x = ($src_width-$src_widthc)/2;
  95. }
  96. else
  97. {
  98. $src_heightc = $src_width *$dst_height/$dst_width;
  99. $pos_y = ($src_height-$src_heightc)/2;
  100. }
  101. MagickCropImage($magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y); // 자르기
  102. //MagickCropImage 함수 이후 Gif 이미지는 변경되지만 캔버스는 변경되지 않기 때문입니다
  103. $this->magick_handle = NewMagickWand();
  104. MagickNewImage($this->magick_handle,$src_widthc ,$src_heightc,'#ffffff');
  105. MagickSetFormat($this->magick_handle,$srcext);
  106. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0);
  107. //Scale
  108. MagickScaleImage($this->magick_handle, $dst_width, $dst_height);
  109. }
  110. //비례 스케일링 모드
  111. if($mode == 2)
  112. {
  113. if($src_width/$src_height>$dst_width/$dst_height)
  114. {
  115. $dst_height=$dst_width*$src_height/$src_width;
  116. }
  117. else
  118. {
  119. $dst_width=$dst_height*$src_width/$src_height;
  120. }
  121. $this->magick_handle=$magic_water_handle;//替换
  122. MagickScaleImage($this->magick_handle, $dst_width, $dst_height);//缩放
  123. }
  124. //缩放填充模式
  125. if($mode == 3)
  126. {
  127. if($src_width/$src_height>$dst_width/$dst_height)
  128. {
  129. $dst_heightc=$dst_width*$src_height/$src_width ;
  130. $dst_widthc=$dst_width;
  131. }
  132. else
  133. {
  134. $dst_widthc=$dst_height*$src_width/$src_height;
  135. $dst_heightc=$dst_height;
  136. }
  137. MagickScaleImage($magic_water_handle, $dst_widthc, $dst_heightc);//缩放
  138. $this->magick_handle = NewMagickWand();
  139. MagickNewImage($this->magick_handle,$dst_width,$dst_height, $this->smallpic_bgcolor);
  140. MagickSetFormat($this->magick_handle,$srcext);
  141. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,($dst_width-$dst_widthc)/2 ,($dst_height-$dst_heightc)/2);
  142. }
  143. //打水印
  144. if($this->watermode == 1)
  145. {
  146. $this-> set_mark();
  147. }
  148. if(empty($dst_file))
  149. {
  150. //建立临时文件
  151. $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG" );
  152. }
  153. MagickWriteImage($this->magick_handle, $dst_file);
  154. return $dst_file;
  155. }
  156. /**
  157. * 워터마크
  158. *
  159. * @param $src_file 워터마크를 생성할 이미지의 경로
  160. * @param $dst_file 워터마크 생성을 위한 파일 저장 경로가 비어 있는 경우 임의의 임시 파일. 제작됩니다
  161. * @return 워터마크 파일 경로/false
  162. */
  163. 공용 함수 water_mark($src_file,$dst_file="")
  164. {
  165. $this->magick_handle = NewMagickWand();
  166. if (!MagickReadImage($this->magick_handle, $src_file))
  167. false 반환;
  168. $this->set_mark();
  169. if(empty($dst_file))
  170. {
  171. //建立临时文件
  172. $dst_file = tempnam($ _SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
  173. }
  174. MagickWriteImage($this->magick_handle, $dst_file);
  175. return $dst_file;
  176. }
  177. / **
  178. * 내부 인터페이스
  179. * 워터마크 사진
  180. *
  181. */
  182. 보호 함수 set_mark()
  183. {
  184. //尺寸
  185. $dst_width = MagickGetImageWidth($this->magick_handle);
  186. $dst_height = MagickGetImageHeight($this->magick_handle);
  187. //관리수印图
  188. if ($this->water_mark && is_file($this->water_mark))
  189. {
  190. $magic_water_handle = NewMagickWand();
  191. MagickRemoveImage($magic_water_handle);
  192. if (MagickReadImage($magic_water_handle, $this->water_mark))
  193. {
  194. MagickScaleImage($magic_water_handle, $dst_width*$this-> ;waterrate, $dst_width*$this->waterrate*MagickGetImageHeight($magic_water_handle)/MagickGetImageWidth($magic_water_handle));//缩放水印到图 Pictures 1/5
  195. if ($this->water_mark_pos == 1 )
  196. {
  197. $left = $this->padding;
  198. $top = $this->padding;
  199. }
  200. elseif ($this->water_mark_pos == 2)
  201. {
  202. $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
  203. $top = $this->padding;
  204. }
  205. elseif($this ->water_mark_pos == 3)
  206. {
  207. $left = $this->padding;
  208. $top = $dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
  209. }
  210. elseif ($this->water_mark_pos == 4)
  211. {
  212. $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
  213. $top =$dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
  214. }
  215. elseif ($this->water_mark_pos == 5)
  216. {
  217. $left = ($dst_width-MagickGetImageWidth($ Magic_water_handle))/2;
  218. $top =($dst_height -MagickGetImageHeight($magic_water_handle))/2;
  219. }
  220. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$ 상단);
  221. }
  222. }
  223. }
  224. }
复代码

허자, PHP


본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

JWT (JSON Web Tokens) 및 PHP API의 사용 사례를 설명하십시오. JWT (JSON Web Tokens) 및 PHP API의 사용 사례를 설명하십시오. Apr 05, 2025 am 12:04 AM

JWT는 주로 신분증 인증 및 정보 교환을 위해 당사자간에 정보를 안전하게 전송하는 데 사용되는 JSON을 기반으로 한 개방형 표준입니다. 1. JWT는 헤더, 페이로드 및 서명의 세 부분으로 구성됩니다. 2. JWT의 작업 원칙에는 세 가지 단계가 포함됩니다. JWT 생성, JWT 확인 및 Parsing Payload. 3. PHP에서 인증에 JWT를 사용하면 JWT를 생성하고 확인할 수 있으며 사용자 역할 및 권한 정보가 고급 사용에 포함될 수 있습니다. 4. 일반적인 오류에는 서명 검증 실패, 토큰 만료 및 대형 페이로드가 포함됩니다. 디버깅 기술에는 디버깅 도구 및 로깅 사용이 포함됩니다. 5. 성능 최적화 및 모범 사례에는 적절한 시그니처 알고리즘 사용, 타당성 기간 설정 합리적,

PHP에서 늦은 정적 결합의 개념을 설명하십시오. PHP에서 늦은 정적 결합의 개념을 설명하십시오. Mar 21, 2025 pm 01:33 PM

기사는 PHP 5.3에 도입 된 PHP의 LSB (Late STATIC BING)에 대해 논의하여 정적 방법의 런타임 해상도가보다 유연한 상속을 요구할 수있게한다. LSB의 실제 응용 프로그램 및 잠재적 성능

프레임 워크 보안 기능 : 취약점 보호. 프레임 워크 보안 기능 : 취약점 보호. Mar 28, 2025 pm 05:11 PM

기사는 입력 유효성 검사, 인증 및 정기 업데이트를 포함한 취약점을 방지하기 위해 프레임 워크의 필수 보안 기능을 논의합니다.

PHP의 CURL 라이브러리를 사용하여 JSON 데이터가 포함 된 게시물 요청을 보내는 방법은 무엇입니까? PHP의 CURL 라이브러리를 사용하여 JSON 데이터가 포함 된 게시물 요청을 보내는 방법은 무엇입니까? Apr 01, 2025 pm 03:12 PM

PHP 개발에서 PHP의 CURL 라이브러리를 사용하여 JSON 데이터를 보내면 종종 외부 API와 상호 작용해야합니다. 일반적인 방법 중 하나는 컬 라이브러리를 사용하여 게시물을 보내는 것입니다 ...

확실한 원칙과 PHP 개발에 적용되는 방법을 설명하십시오. 확실한 원칙과 PHP 개발에 적용되는 방법을 설명하십시오. Apr 03, 2025 am 12:04 AM

PHP 개발에서 견고한 원칙의 적용에는 다음이 포함됩니다. 1. 단일 책임 원칙 (SRP) : 각 클래스는 하나의 기능 만 담당합니다. 2. Open and Close Principle (OCP) : 변경은 수정보다는 확장을 통해 달성됩니다. 3. Lisch의 대체 원칙 (LSP) : 서브 클래스는 프로그램 정확도에 영향을 미치지 않고 기본 클래스를 대체 할 수 있습니다. 4. 인터페이스 격리 원리 (ISP) : 의존성 및 사용되지 않은 방법을 피하기 위해 세밀한 인터페이스를 사용하십시오. 5. 의존성 반전 원리 (DIP) : 높고 낮은 수준의 모듈은 추상화에 의존하며 종속성 주입을 통해 구현됩니다.

프레임 워크 사용자 정의/확장 : 사용자 정의 기능을 추가하는 방법. 프레임 워크 사용자 정의/확장 : 사용자 정의 기능을 추가하는 방법. Mar 28, 2025 pm 05:12 PM

이 기사에서는 프레임 워크에 사용자 정의 기능 추가, 아키텍처 이해, 확장 지점 식별 및 통합 및 디버깅을위한 모범 사례에 중점을 둡니다.

세션 납치는 어떻게 작동하며 PHP에서 어떻게 완화 할 수 있습니까? 세션 납치는 어떻게 작동하며 PHP에서 어떻게 완화 할 수 있습니까? Apr 06, 2025 am 12:02 AM

세션 납치는 다음 단계를 통해 달성 할 수 있습니다. 1. 세션 ID를 얻으십시오. 2. 세션 ID 사용, 3. 세션을 활성 상태로 유지하십시오. PHP에서 세션 납치를 방지하는 방법에는 다음이 포함됩니다. 1. 세션 _regenerate_id () 함수를 사용하여 세션 ID를 재생산합니다. 2. 데이터베이스를 통해 세션 데이터를 저장하십시오.

See all articles