之前這篇..
http://phorum.study-area.org/index.php/topic,55349.0.html
最後我是決定使用 pngfix.js 來處理
其做法是使用瀏覽器端的 windows 本身提供的 AlphaImageLoader 來處理圖片
但是用 AlphaImageLoader 處理過後的圖片會有一個問題
圖片上右鍵的功能會失效
因為原來的圖片已經被 AlphaImageLoader 轉換成非圖片格式
某些目錄裡的圖片我還需要右鍵的功能
這些目錄裡的圖片就不能用 pngfix.js 來修正透明圖
稍微玩了一下用 PHP::imagick png 轉 gif 保留透明的方式
.htaccess
程式碼:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/4(.*)MSIE\s6\.0
RewriteRule (.*\.png)$ convertPng2Gif.php?i=$1
convertPng2Gif.php
程式碼:
/*
Yamaka 2009/09/05
convertPng2Gif.php
*/
$imgPath = trim($_GET['i']);
if (empty($imgPath)) exit;
if (substr($imgPath, -4, 4) != '.png') exit;
if (file_exists($imgPath)) {
$im = new Imagick($imgPath);
$im->thresholdImage(0.8, Imagick::CHANNEL_ALPHA);
$im->setImageFormat("gif");
header('Content-type: image/gif');
echo $im;
}
?>
將這兩個檔案放在要將 png 轉成 gif 的目錄裡就可以了
做了一些修改
如果 png 圖檔有同檔名 gif 圖檔
直接送出 gif 圖檔..
convertPng2Gif.php
程式碼:
/*
Yamaka 2009/09/05
convertPng2Gif.php
*/
$imgPath = trim($_GET['i']);
if (empty($imgPath)) exit;
if (substr($imgPath, -4, 4) != '.png') exit;
$imgPath2 = str_replace('.png', '.gif', $imgPath);
if (file_exists($imgPath2)) {
header('Content-type: image/gif');
readfile($imgPath2);
exit;
}
if (file_exists($imgPath)) {
$im = new Imagick($imgPath);
$im->thresholdImage(0.8, Imagick::CHANNEL_ALPHA);
$im->setImageFormat("gif");
header('Content-type: image/gif');
echo $im;
}
?>