스마트에디터5 이미지 (png 압축 수준)
본문
사이트 특성상 png파일들이 많이 올라올 것 같은데 압축률을 9로 설정해도 파일용량은 크게 줄어들지를 않네요.
png파일의 압축률을 9 이상으로 더 압축하는 방법이 있을까요? 이게 불가능하다면 png파일은 jpg파일로 변경하여 더 적은 용량을 갖도록 하는 방법이 있을까요?
답변 1
/**
*
* @param string $importPath
* @param string $exportPath
* @param int $quality 0 = low / smaller file, 100 = better / bigger file
* @throws Exception
* @return boolean
*/
function png2jpg($importPath, $exportPath = null, $quality = 50) {
$rtn = true;
try {
$arr = [];
$image = imagecreatefrompng($importPath);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
$arr[] = $image;
$arr[] = $bg;
$arr[] = imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
$arr[] = imagealphablending($bg, TRUE);
$arr[] = imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
$arr[] = imagedestroy($image);
$result = $importPath . '.jpg';
if (empty($exportPath) == true) {
$result = $exportPath;
}
$arr[] = imagejpeg($bg, $result, $quality);
$arr[] = imagedestroy($bg);
foreach ($arr as $v) {
if ($v === false) {
$rtn = false;
break;
}
}
} catch (Exception $err) {
$rtn = false;
throw $err;
}
return $rtn;
}
답변을 작성하시기 전에 로그인 해주세요.