从网站导出JPG和/或PDF图像的图书馆或想法


Library or Ideas on Exporting Images from Website as JPG and/or PDF

我有一个网站,可以从数据库数据中生成BMP图像文件(图形)。我想让用户只下载JPG和PDF文件的图像(图形)。每个链接一个。这个网站是用PHP完成的。

我试过使用FPDF,但是,它不喜欢BMP文件。

我的图片URL看起来是这样的:chart.php?act=img&t=使用&fy=2010&rt=丙烷&mo=October不带.bmp,但是,当右键单击"另存为…"时,这是唯一的选项。

FPDF是生成PDF的好方法,它当然可以处理添加到文档中的JPG图像。这意味着剩下的唯一任务就是将这些BMP转换为JPG(或其他什么)。

关于这个问题,我的第一个方法是质疑基本格式:为什么图形一开始就需要BMP?这种格式对网络不是很友好,所以一开始就用BMP生成图形会产生一个问题,你必须一次又一次地处理这个问题。为什么一开始就这么做?找到一种生成图形的方法,为您提供有用的工作。

假设您别无选择,只能用BMP生成图形,那么下一步将着眼于将这些BMP转换为一些有用的格式。在imagecreate手册页面上,一位评论者提供了一个名为ImageCreateFromBMP的示例函数,我已经测试并使用了它(包含在本答案的末尾)。这将使您的图形成为一种真正有用的文件格式,然后您可以将文件写入磁盘,将其添加到PDF中,或提供下载。

文档和相关阅读

  • PHP imagecreate(此答案中的代码源)-http://php.net/manual/en/function.imagecreate.php
  • FPDF::Image-http://www.fpdf.org/en/doc/image.htm
  • pChart,一个PHP图表库,它不会创建无用的BMP图像-http://pchart.sourceforge.net/
  • JpGraph,另一个无用的PHP图表库-http://jpgraph.net/
  • Highcharts,交互式javascript图表,将PHP渲染的图表吹出水面-http://www.highcharts.com/

代码来源:DHKold检索自:http://php.net/manual/en/function.imagecreate.php日期:2013年2月13日

<?php
/*********************************************/
/* Fonction: ImageCreateFromBMP              */
/* Author:   DHKold                          */
/* Contact:  admin@dhkold.com                */
/* 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;
}
?>