Image overlay problem FPDF
Solved
zoubila44
Posted messages
114
Status
Member
-
zoubila44 Posted messages 114 Status Member -
zoubila44 Posted messages 114 Status Member -
Hello everyone,
I have a problem: I have a string ($chaine) that can contain either text or an image (in the form
). The issue is that I don’t know what’s inside $chaine, so I managed to handle it to generate my images and my text. The problem is that the images appear at the top of the PDF and not within the text sentences. I can’t hard-code coordinates in the code because I’m processing several strings and displaying them one after another, and the images (in the example there are two) overlap in the upper-left corner of my PDF.
I would like:
http://hpics.li/3336b71 => the PDF I obtain
http://hpics.li/3ea613e => the PDF I would like to obtain
Here is my code:
<?php
require('FPDF/fpdf.php');
$chaine="Salut tout le monde, je vais vous montrer les images de guitare et de basse tout d'abord voici la <span>guitare</span> <img src='images/guitare.png' width='50' height='50'/> ensuite va venir la <span> basse </span> <img src='images/basse.jpg' width='50' height='50' />, à toi de jouer";
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetXY('','');
$pdf->SetFont('Helvetica','B',16);
$pdf->SetFillColor(6,211,248);
$pdf->SetTextColor(243,11,180);
$pdf->Cell(0,10,'INSTRUMENTS',1,1,'C',TRUE);
$pdf->Ln(10);
$pdf->SetFillColor(20,150,50);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(0,10,'voici differents instruments',0,1,'L',TRUE);
$pdf->Ln(40);
if (strpos($chaine, '<img ')!=FALSE) {
$tab = filtreQuest($chaine);
foreach ($tab as $key => $value) {
if (is_array($value)==TRUE) {
$pdf->Image($value['src'],'','',$value['width'],$value['height']);
$pdf->Ln(5);
} else{
$pdf->MultiCell(0,10,$value,1,1,'L',FALSE);
$pdf->Ln(5);
}
}
}else{
$pdf->MultiCell(0,10,$chaine,1,1,'L',FALSE);
$pdf->Ln(5);
}
$pdf->Output();
function filtreQuest($chaine){
$chaine=str_replace('<span>', '', $chaine);
$chaine=str_replace('</span>', '', $chaine);
$chaine=str_replace('à', 'a', $chaine);
$chaine=str_replace('é', 'e', $chaine);
if(strpos($chaine, '<img')){
$chaine=str_replace('<', '|', $chaine);
$chaine=str_replace('/>', '|', $chaine);
$tab=explode('|', $chaine);
foreach ($tab as $key => $value) {
if (strpos($value, 'img ')===0) {
$text[$key]=f_get_info_image($value);
} else{
$text[$key]=$value;
}
}
}
return $text;
}
function f_get_info_image($imgBalise){
$res=array('src'=>'','height'=>'', 'width'=>'');
$imgBalise = str_replace('"', '|', $imgBalise);
$imgBalise = str_replace('"', '|', $imgBalise);
$pos_src=strpos($imgBalise, 'src=|');
if($pos_src===false){}else{
$endPosSrc=strpos($imgBalise, '|',$pos_src+5);
$res["src"]=substr($imgBalise, $pos_src+5, $endPosSrc-($pos_src+5));
}
$pos_h=strpos($imgBalise, 'height=|');
if($pos_h===false){}else{
$endPosH=strpos($imgBalise, '|',$pos_h+8);
$res["height"]=substr($imgBalise, $pos_h+8, $endPosH-($pos_h+8));
}
$pos_w=strpos($imgBalise, 'width=|');
if($pos_w===false){}else{
$endPosW=strpos($imgBalise, '|',$pos_w+7);
$res["width"]=substr($imgBalise, $pos_w+7, $endPosW-($pos_w+7));
}
return $res;
}
J’ai pris une chaîne de caractères bidon avec des images bidons mais cela donne un aperçu de ce que j’ai à traiter
J’ai peur de ne pas m’être bien fait comprendre, n’hésitez pas à demander plus d’explications
Merci d’avance
2 answers
-
$image = $value['src']; $largeur_img = $value['width']; $hauteur_img = $value['height']; $pdf->Cell( 40, ($hauteur_img+3), $pdf->Image($image,$pdf->GetX(), $pdf->GetY(), $largeur_img,$hauteur_img), 0, 1, 'L', false ); $pdf->Ln(1);
http://s000.tinyupload.com/?file_id=48922656315373566330
http://www.fpdf.org/fr/doc/cell.htm
là je pense que tu dois jouer avec les options pour trouver la bonne proportion avec le texte et les images...
Ou simplement utiliser un plugin du genre write_html
http://www.fpdf.org/fr/script/script42.php
qui permet d'envoyer ton html dans le pdf.
Bonne continuation. -
I corrected it, with the solution found here: http://stackoverflow.com/questions/3471441/inserting-an-image-with-php-and-fpdf I tested your code and it works for me. I made a few changes compared to your code...
foreach ($tab as $key => $value) { if (is_array($value)) { // $image = $value['src']; $pdf->Cell( 40, 40, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false ); //$pdf->Image($value['src'],0,10,$value['width'],$value['height']); $pdf->Ln(20); } else{ $pdf->MultiCell(0,10,$value,1,1,'L',FALSE); $pdf->Ln(5); } }-
Hi Kalo and thanks for your reply! I didn't have time to test yet, I'll do it as soon as possible, but in the line
$pdf->Cell( 40, 40, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false );
what does 33.78 correspond to? Because in my HTML images I retrieve the source/height/width and I need to use them :s From what I can see it doesn't seem to work like that I'll tell you more once I’ve tested thanks again for your reply -
Code tester
the images appear below the text
http://hpics.li/5009fc8
here are the small modifications I had found, I reach a PDF roughly similar to yours
<?php
require('FPDF/fpdf.php');
$chaine="Hello everyone, I will show you guitar and bass images first here's the <span>guitare</span> <img src='images/guitare.png' width='50' height='50'/> then will come the <span> bass </span> <img src='images/basse.jpg' width='50' height='50' />, your turn";
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetXY('','');
$pdf->SetFont('Helvetica','B',16);
$pdf->SetFillColor(6,211,248);
$pdf->SetTextColor(243,11,180);
$pdf->Cell(0,10,'INSTRUMENTS',1,1,'C',TRUE);
$pdf->Ln(10);
$pdf->SetFillColor(20,150,50);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(0,10,'voici differents instruments',0,1,'L',TRUE);
$pdf->Ln(40);
if (strpos($chaine, '<img ')!=FALSE) {
$tab = filtreQuest($chaine);
foreach ($tab as $key => $value) {
$x=$pdf->GetX();
$y=$pdf->GetY();
if (is_array($value)==TRUE) {
$pdf->Image($value['src'],$x,$y+10,$value['width'],$value['height']);
$pdf->Ln($value['height']);
}
else{
$pdf->SetXY($x,$y);
$pdf->MultiCell(0,10,$value,1,1,'L',FALSE);
$pdf->Ln(5);
}
}
}else{
$pdf->SetXY($x,$y);
$pdf->MultiCell(0,10,$chaine,1,1,'L',FALSE);
$pdf->Ln(5);
}
$pdf->Output();
function filtreQuest($chaine){
$chaine=str_replace('<span>', '', $chaine);
$chaine=str_replace('</span>', '', $chaine);
$chaine=str_replace('à', 'a', $chaine);
$chaine=str_replace('é', 'e', $chaine);
if(strpos($chaine, '<img')){
$chaine=str_replace('<', '|', $chaine);
$chaine=str_replace('/>', '|', $chaine);
$tab=explode('|', $chaine);
foreach ($tab as $key => $value) {
if (strpos($value, 'img ')===0) {
$text[$key]=f_get_info_image($value);
} else{
$text[$key]=$value;
}
}
}
return $text;
}
function f_get_info_image($imgBalise){
$res=array('src'=>'','height'=>'', 'width'=>'');
$imgBalise = str_replace('"', '|', $imgBalise);
$imgBalise = str_replace("'", '|', $imgBalise);
$pos_src=strpos($imgBalise, 'src=|');
if($pos_src===false){}else{
$endPosSrc=strpos($imgBalise, "|",$pos_src+5);
$res["src"]=substr($imgBalise, $pos_src+5, $endPosSrc-($pos_src+5));
}
$pos_h=strpos($imgBalise, 'height=|');
if($pos_h===false){}else{
$endPosH=strpos($imgBalise, "|",$pos_h+8);
$res["height"]=substr($imgBalise, $pos_h+8, $endPosH-($pos_h+8));
}
$pos_w=strpos($imgBalise, 'width=|');
if($pos_w===false){}else{
$endPosW=strpos($imgBalise, "|",$pos_w+7);
$res["width"]=substr($imgBalise, $pos_w+7, $endPosW-($pos_w+7));
}
return $res;
}
?>
-