سؤال

كيف يمكن حفظ صورتي ، التي تم إنشاؤها باستخدام GD ، كـ PNG-8؟

إنه يحفظ كصورة GIF مع قناة شفافة جيدًا - لكنني أريد استخدام PNG -8.

مع أطيب التحيات ، beerweasle

هل كانت مفيدة؟

المحلول

باستخدام PicturesaVealpha () ولون BG شفاف يجب أن يقوم بالخدعة ...

بناءً على رمز Dfilkovi:

<?php
// Create a new true color image
$im = new imagecreatetruecolor(100, 100);

// Fill with alpha background
$alphabg = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $alphabg);

// Convert to palette-based with no dithering and 255 colors with alpha
imagetruecolortopalette($im, false, 255);
imagesavealpha($im, true);

// Save the image
imagepng($im, './paletteimage.png');
imagedestroy($im);
?>

نصائح أخرى

سون

الافتراض الخاطئ: يمكن أن يكون لدى PNG من أي عمق بت. يتم تسجيله في قطعة TRNS لصورة PNG (باستثناء تلك الموجودة

cf www.libpng.org/pub/png/spec/1.2/png-chunks.html#c.trns

idem www.w3.org/tr/png-chunks.html#c.trns

الفرق هو كيف يكون المسجل: RGBA لديه سجل فريد لكل بكسل ، مع 4 قيم (3 ألوان وقناة ألفا 1) ، حيث "PLETTED" PNG Records Alpha Channel في الجزء الخاص بها.

الألعاب النارية جيدة جدا في ذلك.

أمثلة:

http://www.libpng.org/pub/png/pngs-img.html

اضطررت إلى إضافة خط ImageColorTransparent ($ im ، $ alphabg) ؛ إلى الرمز التالي (مأخوذ من الإجابة السابقة) حتى يعمل هذا:

// Fill with alpha background
$alphabg = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagecolortransparent($im, $alphabg);
imagefill($im, 0, 0, $alphabg);

// Convert to palette-based with no dithering and 255 colors with alpha
imagetruecolortopalette($im, false, 255);
imagesavealpha($im, true);

// Save the image
imagepng($im, './paletteimage.png');
imagedestroy($im);
?>

أعتقد أن هذا يمكن أن يساعدك.

http://roseindia.net/tutorial/php/phpgd/about-transparent.html

<?php
// Create a new true color image
$im = new imagecreatetruecolor(100, 100);

// Convert to palette-based with no dithering and 255 colors
imagetruecolortopalette($im, false, 255);

// Save the image
imagepng($im, './paletteimage.png');
imagedestroy($im);
?>

هذا يجب أن يجعل 8bit png

بناء على حل Dfilkovi ، هل حاولت استخدام Picturesavealpha () لحفظ معلومات قناة ألفا الكاملة؟

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top