我有一個網站,從數據庫數據生成BMP圖像文件(圖形)。我想讓用戶能夠以JPG和PDF文件形式下載圖像(圖形)。每個鏈接一個。這個網站是用PHP完成的。圖書館或想法從網站導出圖像爲JPG和/或PDF
我試過使用FPDF,但是,它不喜歡BMP文件。
我的圖片網址看起來像這樣:chart.php行動= IMG & T =使用& FY = 2010 & RT =丙烷&月=月沒有.BMP,但是,當你右鍵點擊另存爲。 ..這是你得到的唯一選擇。
我有一個網站,從數據庫數據生成BMP圖像文件(圖形)。我想讓用戶能夠以JPG和PDF文件形式下載圖像(圖形)。每個鏈接一個。這個網站是用PHP完成的。圖書館或想法從網站導出圖像爲JPG和/或PDF
我試過使用FPDF,但是,它不喜歡BMP文件。
我的圖片網址看起來像這樣:chart.php行動= IMG & T =使用& FY = 2010 & RT =丙烷&月=月沒有.BMP,但是,當你右鍵點擊另存爲。 ..這是你得到的唯一選擇。
FPDF是一個很好的生成PDF的途徑,它可以處理添加到文檔中的JPG圖像。這意味着剩下的唯一任務就是將這些BMP轉換爲JPG(或其他)。
我在這個問題上的第一種方法是質疑底層格式:爲什麼這些圖形需要以BMP開頭?這種格式不是非常友好的,所以要在BMP中生成圖形會產生一個問題,您將不得不一次又一次地處理。爲什麼要擺在首位?找到一種方法來生成圖表,爲您提供一些有用的工具。
假設您別無選擇,只能在BMP中生成圖形,下一條路徑將會考慮將這些BMP轉換爲一些有用的格式。在imagecreate
手冊頁上,評論者提供了一個稱爲ImageCreateFromBMP
的示例函數,我已經測試並使用了自己(包含在本答案的末尾)。這將使您的圖形變成實際有用的文件格式,然後您可以將文件寫入磁盤,將其添加到PDF或提供下載。
文檔和相關讀物
imagecreate
(在這個答案的代碼的源) - http://php.net/manual/en/function.imagecreate.phpFPDF::Image
- http://www.fpdf.org/en/doc/image.htm碼信貸:DHKold從中檢索:http://php.net/manual/en/function.imagecreate.php上:2013年2月13日
<?php
/*********************************************/
/* Fonction: ImageCreateFromBMP */
/* Author: DHKold */
/* Contact: [email protected] */
/* Date: The 15th of June 2005 */
/* Version: 2.0B */
/*********************************************/
function ImageCreateFromBMP($filename)
{
//Ouverture du fichier en mode binaire
if (! $f1 = fopen($filename,"rb")) return FALSE;
//1 : Chargement des ent�tes FICHIER
$FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
if ($FILE['file_type'] != 19778) return FALSE;
//2 : Chargement des ent�tes BMP
$BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
'/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
'/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
$BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
$BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
$BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
$BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal'] = 4-(4*$BMP['decal']);
if ($BMP['decal'] == 4) $BMP['decal'] = 0;
//3 : Chargement des couleurs de la palette
$PALETTE = array();
if ($BMP['colors'] < 16777216)
{
$PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
}
//4 : Cr�ation de l'image
$IMG = fread($f1,$BMP['size_bitmap']);
$VIDE = chr(0);
$res = imagecreatetruecolor($BMP['width'],$BMP['height']);
$P = 0;
$Y = $BMP['height']-1;
while ($Y >= 0)
{
$X=0;
while ($X < $BMP['width'])
{
if ($BMP['bits_per_pixel'] == 24)
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
elseif ($BMP['bits_per_pixel'] == 16)
{
$COLOR = unpack("n",substr($IMG,$P,2));
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 8)
{
$COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 4)
{
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 1)
{
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
else
return FALSE;
imagesetpixel($res,$X,$Y,$COLOR[1]);
$X++;
$P += $BMP['bytes_per_pixel'];
}
$Y--;
$P+=$BMP['decal'];
}
//Fermeture du fichier
fclose($f1);
return $res;
}
?>
感謝評論。我會把這個實施起來!我一直在玩弄imagecreate功能...還沒有得到結果,但我肯定會。 – jasonflaherty 2013-02-14 00:13:52
是的,'imagecreate'本身並不適用於BMP;就像您在示例代碼中看到的,他必須手動解壓縮上齶,然後一次手動創建一個像素的圖像。如果有任何可能的方法可以避免處理BMP ...... – 2013-02-14 01:20:02