IT/PHP

[PHP] 이미지 사이즈 변경 및 용량 줄이기

월공 2021. 5. 10. 08:52
728x90
300x250
//파라미터(원본이미지, 바뀔이미지, 화질 (0 ~100) , 사이즈 width, height)

//파일 업로드 완료됐다라는 가정하에 진행 (move_uploaded_file)

$source_img		= $file_path."/".$file_nm1; //업로드 된 이미지
$destination_img = $file_path."/"."64_".$file_nm1; //앞에 64_ 를 붙히고 저장
            
compress($source_img, $destination_img , 90, 64,64);
// 파일 사이즈 및 용량 변경
function compress($file, $destination, $quality, $w, $h) {

    $info = getimagesize($file);

    $width = $info[0];
    $height = $info[1];

    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($file);

    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($file);

    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($file);

    $dst = imagecreatetruecolor($w, $h);
    imagecopyresampled($dst, $image, 0, 0, 0, 0, $w, $h, $width, $height);
    $result = imagejpeg($dst, $destination, $quality);

	return $result;
}

 

imagejpeg 이지만 , png, gif 모두 다 잘되는것 확인했는데 딱 하나 문제가 있다.

png 의 경우 바탕이 투명색인걸 올려서 돌리면 아예 검은 이미지가 나온다 -_- ;

728x90
300x250