개발/php
[php] php image resize example / php 이미지 리사이즈 예제 함수 [링크]
효벨
2023. 7. 25. 11:46
728x90
반응형
[php] php image resize example / php 이미지 리사이즈 예제 함수 [링크]
php 에서
이미지 업로드를 다루다 보면
썸네일 용으로 축소해서 업로드 해야하는 경우가 있습니다!
그럴때 유용하게 사용할 수 있는 함수를 찾아 공유해 보려고 합니다!
아래 출처에서 퍼와서 비율을 입력받아 축소하도록 약간 변경한 내용입니다!
function img_resize($file, $newfile, $rate) {
list($width, $height) = getimagesize($file);
$w = $width*($rate/100);
$h = $height*($rate/100);
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);
return "OK";
}
위 내용 보시면 아시겠지만,
업로드된 파일 경로, 새로 저장할 파일 경로, 비율
이 3가지를 전달하면 해당 경로에 비율에 맞체 축소되어 저장되도록 하는 내용입니다!
예제는 아래와 같습니다!
img_resize("data/image001.jpg", "data/thumb_image001.jpg", 20);
위 내용은 파일을 20프로로 축소한 썸네일로 만드는 내용입니다!
참고들 하세요!
출처 - php 이미지 리사이즈 : https://itadventure.tistory.com/124
php - 이미지 리사이즈(크기변경)
php 이미지 리사이즈 php에서 이미지 크기를 변경하는 건 빈번하게 사용되는 기술입니다. 보통 썸네일 이미지를 만들 때 많이 사용되는데요. 관련 소스들이 어중간한게 많아 새로 만들었습니다 :)
itadventure.tistory.com
반응형