insertion d'une image dynamique dans un pdf

insertion d'une image dynamique dans un pdf - PHP - Programmation

Marsh Posté le 06-03-2008 à 19:09:53    

bonjour a tous c'est la pemiere fois que je participe dans cet interessant forum
bon bref  mon brobleme est le suivant:
j'ai creer une image dynamique en php et je veux l'inserer dans un pdf  
j'ai vu la solution de mr omega por jenny  
et j'ai fait des recherche dans le site www.fpdf.org
voila mon code (j'ai suivi tous les etapes que mr omega à proposer)

Code :
  1. Warning: Cannot modify header information - headers already sent by (output started at c:\documents and settings\mm\bureau\easyphp1-8\home\phptopdf\amouna\mem_image.php:220) in c:\documents and settings\mm\bureau\easyphp1-8\home\phptopdf\amouna\fpdf.php on line 1022
  2. FPDF error: Some data has already been output to browser, can't send PDF file


 
mon code est le suivant :
 

Code :
  1. <?php
  2. require('mem_image.php');
  3. $pdf=new MEM_IMAGE();
  4. $pdf->AddPage();
  5. // on définit les caractères utilisés pour le code généré
  6. $liste = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  7. // génére le code en piochant dans les caractères de la liste
  8. $code = '';
  9. while(strlen($code) != 6) {
  10.    $code .= $liste[rand(0,36)];
  11. }
  12. // on créé une image de 70 x 20 pixels (larg x hauteur)  
  13. $img = @imagecreatetruecolor (100, 30) or die ("Impossible de crée un flux d'image GD" );
  14. // Choix de la couleur de fond, ici ça donne du Gris ( RVB)
  15. $background_color = imagecolorallocate ($img, 238, 238, 238);
  16. // Choix de la couleur de la police, ici du noir
  17. $ecriture_color = imagecolorallocate ($img, 233, 14, 91);
  18. // le code la police utilisée
  19. $code_police=30;
  20. // on introduit le code dans l'image
  21. imageString($img, $code_police,(100-imagefontwidth($code_police) * strlen("".$code."" ))/2,0, $code,$ecriture_color);
  22. //Affichage
  23. $pdf->GDImage($img, 50, 25, 40);
  24. imagedestroy($img);
  25. $pdf->Output();
  26. ?>


s'il vous plait j'ai besoin d'une reponse ça fait 2 jours que je travaille pour regler ce probleme et je n'arrive pas à le corriger  

Reply

Marsh Posté le 06-03-2008 à 19:09:53   

Reply

Marsh Posté le 06-03-2008 à 19:48:24    

la seule méthode simple que j'ai trouvé pour faire ça c'est d'écrire les images temporaires dans /tmp le temps de générer le pdf.


---------------
Can't buy what I want because it's free -
Reply

Marsh Posté le 06-03-2008 à 20:13:04    

Est-ce que c'est votre premier essai avec fpdf, ou bien avez-vous déjà pu sortir des fichiers pdf avant ?
 
Sauf erreur, il manque du code, car la ligne

$pdf=new MEM_IMAGE();

indique qu'il existerait une classe nommée MEM_IMAGE qui étenderait la classe FPDF, par exemple :

class MEM_IMAGE extends FPDF
{
// En-tete
function Header()
{
blabla
}
 
// Pied de page
function Footer()
{
blabla
}
}

Edit : C'est peut-être inclus dans mem_image.php


Message édité par olivthill le 06-03-2008 à 20:14:50
Reply

Marsh Posté le 07-03-2008 à 15:34:25    

j'ai pu sortir des pdf avec d'autre code
 
j'ai déja deffinie la fonction mem_image()  
le code est le suivant  
c le mm code de la fonction "impot d'image en memoire" dans le script des fpdf'

Code :
  1. <?php
  2. require('fpdf.php');
  3. class VariableStream
  4. {
  5.     // Stream handler to read from global variables
  6.     var $varname;
  7.     var $position;
  8.     function stream_open($path, $mode, $options, &$opened_path)
  9.     {
  10.         $url = parse_url($path);
  11.         $this->varname = $url['host'];
  12.         if(!isset($GLOBALS[$this->varname]))
  13.         {
  14.             trigger_error('Global variable '.$this->varname.' does not exist', E_USER_WARNING);
  15.             return false;
  16.         }
  17.         $this->position = 0;
  18.         return true;
  19.     }
  20.     function stream_read($count)
  21.     {
  22.         $ret = substr($GLOBALS[$this->varname], $this->position, $count);
  23.         $this->position += strlen($ret);
  24.         return $ret;
  25.     }
  26.     function stream_eof()
  27.     {
  28.         return $this->position >= strlen($GLOBALS[$this->varname]);
  29.     }
  30.     function stream_tell()
  31.     {
  32.         return $this->position;
  33.     }
  34.     function stream_seek($offset, $whence)
  35.     {
  36.         if($whence==SEEK_SET)
  37.         {
  38.             $this->position = $offset;
  39.             return true;
  40.         }
  41.         return false;
  42.     }
  43. }
  44. class MEM_IMAGE extends FPDF
  45. {
  46.     // (c) Xavier Nicolay
  47.     // V1.01 : 2006-11-19
  48.     //
  49.     // CONSTRUCTOR
  50.     //
  51.     function MEM_IMAGE($orientation='P',$unit='mm',$format='A4')
  52.     {
  53.         $this->FPDF($orientation, $unit, $format);
  54.         //Register var stream protocol (requires PHP>=4.3.2)
  55.         if(function_exists('stream_wrapper_register'))
  56.             stream_wrapper_register('var','VariableStream');
  57.     }
  58.     //
  59.     // PRIVATE FUNCTIONS
  60.     //
  61.     function _readstr($var, &$pos, $n)
  62.     {
  63.         //Read some bytes from string
  64.         $string = substr($var, $pos, $n);
  65.         $pos += $n;
  66.         return $string;
  67.     }
  68.    
  69.     function _readstr_int($var, &$pos)
  70.     {
  71.         //Read a 4-byte integer from string
  72.         $i =ord($this->_readstr($var, $pos, 1))<<24;
  73.         $i+=ord($this->_readstr($var, $pos, 1))<<16;
  74.         $i+=ord($this->_readstr($var, $pos, 1))<<8;
  75.         $i+=ord($this->_readstr($var, $pos, 1));
  76.         return $i;
  77.     }
  78.     function _parsemempng($var)
  79.     {
  80.         $pos=0;
  81.         //Check signature
  82.         $sig = $this->_readstr($var,$pos, 8);
  83.         if($sig != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
  84.             $this->Error('Not a PNG image');
  85.         //Read header chunk
  86.         $this->_readstr($var,$pos,4);
  87.         $ihdr = $this->_readstr($var,$pos,4);
  88.         if( $ihdr != 'IHDR')
  89.             $this->Error('Incorrect PNG Image');
  90.         $w=$this->_readstr_int($var,$pos);
  91.         $h=$this->_readstr_int($var,$pos);
  92.         $bpc=ord($this->_readstr($var,$pos,1));
  93.         if($bpc>8)
  94.             $this->Error('16-bit depth not supported: '.$file);
  95.         $ct=ord($this->_readstr($var,$pos,1));
  96.         if($ct==0)
  97.             $colspace='DeviceGray';
  98.         elseif($ct==2)
  99.             $colspace='DeviceRGB';
  100.         elseif($ct==3)
  101.             $colspace='Indexed';
  102.         else
  103.             $this->Error('Alpha channel not supported: '.$file);
  104.         if(ord($this->_readstr($var,$pos,1))!=0)
  105.             $this->Error('Unknown compression method: '.$file);
  106.         if(ord($this->_readstr($var,$pos,1))!=0)
  107.             $this->Error('Unknown filter method: '.$file);
  108.         if(ord($this->_readstr($var,$pos,1))!=0)
  109.             $this->Error('Interlacing not supported: '.$file);
  110.         $this->_readstr($var,$pos,4);
  111.         $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  112.         //Scan chunks looking for palette, transparency and image data
  113.         $pal='';
  114.         $trns='';
  115.         $data='';
  116.         do
  117.         {
  118.             $n=$this->_readstr_int($var,$pos);
  119.             $type=$this->_readstr($var,$pos,4);
  120.             if($type=='PLTE')
  121.             {
  122.                 //Read palette
  123.                 $pal=$this->_readstr($var,$pos,$n);
  124.                 $this->_readstr($var,$pos,4);
  125.             }
  126.             elseif($type=='tRNS')
  127.             {
  128.                 //Read transparency info
  129.                 $t=$this->_readstr($var,$pos,$n);
  130.                 if($ct==0)
  131.                     $trns=array(ord(substr($t,1,1)));
  132.                 elseif($ct==2)
  133.                     $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
  134.                 else
  135.                 {
  136.                     $p=strpos($t,chr(0));
  137.                     if(is_int($p))
  138.                         $trns=array($p);
  139.                 }
  140.                 $this->_readstr($var,$pos,4);
  141.             }
  142.             elseif($type=='IDAT')
  143.             {
  144.                 //Read image data block
  145.                 $data.=$this->_readstr($var,$pos,$n);
  146.                 $this->_readstr($var,$pos,4);
  147.             }
  148.             elseif($type=='IEND')
  149.                 break;
  150.             else
  151.                 $this->_readstr($var,$pos,$n+4);
  152.         }
  153.         while($n);
  154.         if($colspace=='Indexed' and empty($pal))
  155.             $this->Error('Missing palette in '.$file);
  156.         return array('w'=>$w,
  157.                      'h'=>$h,
  158.                      'cs'=>$colspace,
  159.                      'bpc'=>$bpc,
  160.                      'f'=>'FlateDecode',
  161.                      'parms'=>$parms,
  162.                      'pal'=>$pal,
  163.                      'trns'=>$trns,
  164.                      'data'=>$data);
  165.     }
  166.  
  167.     /********************/
  168.     /* PUBLIC FUNCTIONS */
  169.     /********************/
  170.     function MemImage($data, $x, $y, $w=0, $h=0, $link='')
  171.     {
  172.         //Put the PNG image stored in $data
  173.         $id = md5($data);
  174.         if(!isset($this->images[$id]))
  175.         {
  176.             $info = $this->_parsemempng( $data );
  177.             $info['i'] = count($this->images)+1;
  178.             $this->images[$id]=$info;
  179.         }
  180.         else
  181.             $info=$this->images[$id];
  182.    
  183.         //Automatic width and height calculation if needed
  184.         if($w==0 and $h==0)
  185.         {
  186.             //Put image at 72 dpi
  187.             $w=$info['w']/$this->k;
  188.             $h=$info['h']/$this->k;
  189.         }
  190.         if($w==0)
  191.             $w=$h*$info['w']/$info['h'];
  192.         if($h==0)
  193.             $h=$w*$info['h']/$info['w'];
  194.         $this->_out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i']));
  195.         if($link)
  196.             $this->Link($x,$y,$w,$h,$link);
  197.     }
  198.    
  199.     function GDImage($im, $x, $y, $w=0, $h=0, $link='')
  200.     {
  201.         //Put the GD image $im
  202.         ob_start();
  203.         imagepng($im);
  204.         $data = ob_get_contents();     
  205.         ob_end_clean();
  206.         $this->MemImage($data, $x, $y, $w, $h, $link);
  207.     }
  208. }
  209. ?>


Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed