PHP

[PHP] 이미지 리사이즈 (크기변경, 썸네일생성)

chsr 2021. 3. 30. 15:10
728x90
반응형

PHP 이미지 리사이즈 (크기변경, 썸네일생성) / 이미지 크기변경 / 이미지 썸네일 생성


 함수 정의

function resize_image($file, $newfile, $w, $h) {
  list($width, $height) = getimagesize($file);
  if(strpos(strtolower($file), ".jpg"))
  	$src = imagecreatefromjpeg($file);
  else if(strpos(strtolower($file), ".png"))
  	$src = imagecreatefrompng($file);
  else if(strpos(strtolower($file), ".gif"))
 	$src = imagecreatefromgif($file);
  $dst = imagecreatetruecolor($w, $h);
  imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
  if(strpos(strtolower($newfile), ".jpg"))
  	imagejpeg($dst, $newfile);
  else if(strpos(strtolower($newfile), ".png"))
  	imagepng($dst, $newfile);
  else if(strpos(strtolower($newfile), ".gif"))
  	imagegif($dst, $newfile);
}

 

■ 사용법

resize_image("원본파일명", "대상파일명", 변경될가로크기, 변경될세로크기);

* 원본/대상파일명 (string), 변경될 가로/세로크기 (int, 픽셀단위)  * 폴더 권한 체크

resize_image("org/tmpImg.jpg", "new/tmpImg.jpg", 200, 200);

 

출처

 

 

php - 이미지 리사이즈(크기변경)

php 이미지 리사이즈 php에서 이미지 크기를 변경하는 건 빈번하게 사용되는 기술입니다. 보통 썸네일 이미지를 만들 때 많이 사용되는데요. 관련 소스들이 어중간한게 많아 새로 만들었습니다 :)

itadventure.tistory.com

728x90
반응형