본문 바로가기
PHP

[PHP] png 파일 압축해서 업로드하기

by teamnova 2022. 12. 6.

안녕하세요. 오늘은 png 파일을 업로드할 때 용량을 압축하는 방법을 알아보겠습니다.

 

<?php

$file = $_FILES['file'];
$filename = $file['name'];
$extension = end(explode(".", $filename)); // . 구분자로 파일명 분리 <- 확장자 추출을 위해서
$tmpName = $file['tmp_name'];

$image = imagecreatefrompng($tmpName);

$upload_dir = "이미지 저장할 경로" . $filename . '.' . $extension;

imagepng($image, $upload_dir, 7);

?>

imagepng(이미지, 업로드할 경로, 압축 퀄리티);

퀄리티는 0부터 9까지 지정할 수 있으며, 9가 압축률이 가장 높습니다.

 

 

더 자세한 내용은 여기서 확인하실 수 있습니다.

https://www.php.net/manual/en/function.imagepng.php

 

PHP: imagepng - Manual

Trying to resize a png 256 colors image and save it in 256 colors with a correct color palette ? (if you'll save a 256 color image in truecolor palette the result image will have a big size).I spent some hours trying various function to get a good quality

www.php.net