채택완료

스마트에디터5 이미지 (png 압축 수준)

2023-06-17 (토) 21:00:55 2,164

사이트 특성상 png파일들이 많이 올라올 것 같은데 압축률을 9로 설정해도 파일용량은 크게 줄어들지를 않네요.

png파일의 압축률을 9 이상으로 더 압축하는 방법이 있을까요? 이게 불가능하다면 png파일은 jpg파일로 변경하여 더 적은 용량을 갖도록 하는 방법이 있을까요?

|

답변 1개 / 댓글 1개

채택된 답변
+20 포인트
Copy
/**
 * 
 * @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;
}

답변에 대한 댓글 1개

베르만님 감사합니다. 혹시 해당 소스를 어떤 파일에 집어넣어야 할까요?

답변을 작성하려면 로그인이 필요합니다.