png 파일을 webp 파일로 변환하는 방법
이미지(png)를 (webp) 파일로 변환해야 합니다.
png 파일을 업로드한 후 webp 이미지가 생성되었지만 webp 파일이 png 파일의 투명성을 복사하지 않고 검은 배경을 만듭니다.
이것은 나의 php 코드입니다.
$type = wp_check_filetype($file, null);
$ext = $type['ext'];
if ($ext === 'png') {
$im = imagecreatefrompng($file);
imagepalettetotruecolor($im);
$webp = imagewebp($im, str_replace('png', 'webp', $file));
}
imagedestroy($im);
PHP 버전은 5.6입니다.
7.3.0에서 테스트 완료 -- 동작.
면책사항: 이후 버전 또는 일부 PHP 버전에서만 사용할 수 있습니다.
5.6.15(동작하지 않는 검은 배경) 및 7.3.0(작동한 투명 배경)에서만 테스트 완료.
코드는 다음과 같습니다.
// get png in question
$pngimg = imagecreatefrompng($file);
// get dimens of image
$w = imagesx($pngimg);
$h = imagesy($pngimg);;
// create a canvas
$im = imagecreatetruecolor ($w, $h);
imageAlphaBlending($im, false);
imageSaveAlpha($im, true);
// By default, the canvas is black, so make it transparent
$trans = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefilledrectangle($im, 0, 0, $w - 1, $h - 1, $trans);
// copy png to canvas
imagecopy($im, $pngimg, 0, 0, 0, 0, $w, $h);
// lastly, save canvas as a webp
imagewebp($im, str_replace('png', 'webp', $file));
// done
imagedestroy($im);
편집 1. ** 프루프
PHP GD 라이브러리는 libgd 라이브러리에 의존합니다.
링크:
https://github.com/libgd/libgd
저장 시 관련 코드(파일: gd_webp.c), 알파 채널에 대한 존중을 나타내는 발췌:
c = im->tpixels[y][x];
a = gdTrueColorGetAlpha(c);
if (a == 127) {
a = 0;
} else {
a = 255 - ((a << 1) + (a >> 6));
}
*(p++) = gdTrueColorGetRed(c);
*(p++) = gdTrueColorGetGreen(c);
*(p++) = gdTrueColorGetBlue(c);
*(p++) = a;
에 대해서static int _gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality)
제가 제시한 PHP 코드는 GD 라이브러리에서 알파가 실제로 존중되고 있다는 사실에 의존하며, 당신이 사용하고 있는 버전보다 새로운 PHP 버전에서 테스트된 경우, 특히 7.3.0에서 테스트되었지만, 당신의 버전 이후 초기 릴리스에서 작동할 수 있습니다.
알파 채널을 활성화하고 저장해야 할 수 있습니다.한번 시도해 보세요.
$ext = $type['ext'];
if ($ext === 'jpg' || $ext === 'jpeg') {
$im = imagecreatefromjpeg($file);
$webp = imagewebp($im, str_replace($ext, 'webp', $file), 70);
} elseif ($ext === 'png') {
$im = imagecreatefrompng($file);
imagepalettetotruecolor($im);
imageAlphaBlending($im, true); // alpha channel
imageSaveAlpha($im, true); // save alpha setting
$webp = imagewebp($file, str_replace('png', 'webp', $file));
}
imagedestroy($im);
PHP 버전은 5.6입니다.
출력 형식이 전체 알파 투명도를 지원하는 경우 원본 이미지를 복사할 필요가 없습니다.대신 저장 시 GD에 알파 채널을 유지하도록 지시하면 됩니다.
$im = imagecreatefrompng($infilename);
imagesavealpha($im, true);
imagewebp($im, $outfilename);
추가만 하면 됩니다.
imagealphablending($im, true);
imagesavealpha($im, true);
$im = imagecreatefrompng 파일)과 imagewebp(..) 사이.
$type = wp_check_filetype($file, null);
$ext = $type['ext'];
if ($ext === 'png') {
$im = imagecreatefrompng($file);
imagealphablending($im, true);
imagesavealpha($im, true);
$webp = imagewebp($im, str_replace('png', 'webp', $file));
}
imagedestroy($im);
이미지를 다음과 같은 장소에 배치합니다.
<html>
<body style="background-color:red;">
<img src="url_of_your_img.webp">
</body>
</html>
그리고 당신은 당신의 웹p를 올바른 방법으로 볼 수 있어야 합니다.
언급URL : https://stackoverflow.com/questions/57757439/how-to-convert-png-file-to-webp-file
'source' 카테고리의 다른 글
| 여기서 'it()' 함수는 무엇을 하고 있습니까? (0) | 2023.02.15 |
|---|---|
| Postgres에서의 JSON과 JSONB의 차이점 (0) | 2023.02.15 |
| Angular UI-Bootstrap에서 모달의 "해체"와 "닫기"의 차이점은 무엇입니까? (0) | 2023.02.15 |
| 웹 팩을 사용하여 인라인 svg를 설정하는 방법 (0) | 2023.02.15 |
| jq를 사용하여 JSONL을 출력하는 방법(1줄에 1개의 독립된 JSON 객체) (0) | 2023.02.15 |