Tutorial 5 : Insert images within a segment - neeftarah/odtphp GitHub Wiki
The purpose of this tutorial is to show how to insert an image in a segment. This task is significantly the same thing as adding an image to the root of the document. Instead of calling the method setImage() on the object $odf, we call it on the object of type Segment.
The PHP code
<?php
require_once('../library/odf.php');
$odf = new odf("tutoriel5.odt");
$odf->setVars('titre', 'Quelques articles de l\'encyclopédie Wikipédia');
$message = "La force de cette encyclopédie en ligne réside...";
$odf->setVars('message', $message);
$listeArticles = array(
array('titre' => 'PHP',
'texte' => 'PHP, est un langage de scripts (...)',
'image' => './images/php.gif'
),
array('titre' => 'MySQL',
'texte' => 'MySQL est un système de gestion de base de données (...)',
'image' => './images/mysql.gif'
),
array('titre' => 'Apache',
'texte' => 'Apache HTTP Server, souvent appelé Apache (...)',
'image' => './images/apache.gif'
)
);
$article = $odf->setSegment('articles');
foreach($listeArticles AS $element) {
$article->titreArticle($element['titre']);
$article->texteArticle($element['texte']);
$article->setImage('image', $element['image']);
$article->merge();
}
$odf->mergeSegment($article);
$odf->exportAsAttachedFile();
?>
-
We create an Odf object by specifying the path to our template. Then, we call two times the method setVars() of the created object in order to replace {titre} and {message} tags by text content.
-
For the second step, we initialize in an array a data set that we want to fully display. The array provides a list of articles consisting of a title, a text and an image.
-
We initialize the segment "articles" with a call to the method setSegment() which takes as argument the name of the segment and returns an object of type Segment.
-
Then, We can loop on our data array. At each loop, we replace the tags {titreArticle} and {texteArticle} of segment by content of the current line of the array. For that, we directly access to the methods titreArticle() and texteArticle() of the object $article. You can also perform this replacement in a classic way by calling the method setVar() of the object $article. In the same way, we call the method setImage() in order to replace the tag image by an image of our choice. When we have made these replacements, we merge the segment by calling the method merge() of the object $article.
-
Eventually, when we have added all our desired data, we can add the segment to the document by calling the method mergeSegment() of the object $odf which takes as parameter our segment $article.
The OpenOffice model (tutoriel5.odt)
{titre}
{message}
[!-- BEGIN articles --]
{titreArticle}
{texteArticle}
{image}
[!-- END articles --]
To run this example, copy this template within an OpenOffice document named "tutoriel5.odt". In the template, an image is represented by a classic tag.
The result
tutorial odtPHP
Note : the texts in the PHP code are shorter than the result in order to make it more readable. Styles (colors, layouts) of this result come from our template.