如何使用php powerpoint库添加多个图像?

时间:2016-05-28 10:02:51

标签: php

你能告诉我如何使用php powerpoint库在powerpoint中绑定多个图像吗?在下面的代码中,我使用foreach循环在powerpoint中添加多个图像,但只添加了一个图像,所以请帮助我。

<?php 
require_once("db_config.php");
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPPowerPoint.php';
include 'PHPPowerPoint/IOFactory.php';
?>
<html>
<h3 align="center">Welcome <?php echo $_SESSION['user_name'];?>  
<br>
<a href="logout.php">Logout</a>
</h3>
<body>
<?php 
echo "<div align='center'>";
echo "<form method='post' action=''>";
echo "<table align='center' border='1'>
<tr>
<th></th>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>UserName</th>
</tr>";
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='checkbox' name='user_id[]' value='".$row['user_id']."' />  </td>";
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "</tr>"; 
}
echo "</table><br>";
echo "<input type='submit' name='submit' value='Export PPT' />";
echo "</form></div>";
?>
</body>
</html>
<?php
if(isset($_POST['submit']) && isset($_POST['user_id'])){
$user_id = $_POST['user_id'];

foreach($user_id as $selected){
echo $selected."</br>";

$result = mysql_query("SELECT * FROM users WHERE user_id ='".$selected."'");
$row = mysql_fetch_array($result);

$img = '<img src="images/'.$row["image_name"].'" height="200" width="300"    />';

$objPHPPowerPoint = new PHPPowerPoint();
// block sets slide logo.
$currentSlide = $objPHPPowerPoint->getActiveSlide();
$shape = $currentSlide->createDrawingShape();
$shape->setPath('images/'.$row["image_name"]);
$shape->setWidth(640); 
$shape->setHeight(480);
$shape->setOffsetX(10);
$shape->setOffsetY(10);

// block sets text for first slide.
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(700);
$shape->setWidth(600);
$shape->setOffsetX(10);
$shape->setOffsetY(500);
$shape->getAlignment()->setHorizontal(        PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER );
$textRun = $shape->createTextRun('FirstName:'.$row["first_name"].'   Lastname:'.$row["last_name"].' UserName:'.$row["user_name"]);
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(30);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#FFFF' ) );

// block sets text for first slide ends.

$filename = str_replace('.php', '.pptx', __FILE__);
$newname = "PresentationReport-" . date('Y-m-d-H-i-s') . ".pptx";
$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save(str_replace('.php', '.pptx', __FILE__));

// block to download file.
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=" . $newname);
ob_clean();
flush();
readfile($filename);
exit();
}
}
else
{

echo "<div align='center'>Please choose the user</div>";
exit;
}
?>

1 个答案:

答案 0 :(得分:1)

How can i add multiple images using php powerpoint library?

您的代码段中可以识别出两个问题:

1 - 你正在为foreach中的每个元素调用“new Powerpoint”,并且由此而来 你总是覆盖上一张幻灯片。

2 - 您将形状定位在相同的x / y偏移中:

$shape->setOffsetX(10);
$shape->setOffsetY(10);

所以在foreach中所有的形状都会堆叠起来。 对于功能性解决方案,您需要安装一个有效的算法,如:

我将按幻灯片拍摄4张照片,例如:

$resultsFromDataBase = $resultsFromDataBase;

// Array chunk to create a "logic" 4 photos ... jump to the new slide
$resultsBySlide = array_chunk($resultsFromDataBase, 4);

// Each photo needs your independent X offset
// Note the fake values, you need to calculate them
$positionsByIndex = [
    0 => 10,
    1 => 20,
    2 => 30,
    3 => 40
];

foreach ($resultsBySlide as $i => $rows) {
    // 4 photos -> Jump the slide and create another one
    $ppt->createSlide();

    // SlideCount -1 gets your current index
    $slideIndex = $ppt->getSlideCount()-1;
    $currentSlide = $objPHPPowerPoint->getActiveSlide(); 

    foreach ($rows as $position => $row) {
        $shape = $currentSlide->createDrawingShape();
        $shape->setPath('images/'.$row["image_name"]);
        $shape->setWidth(640); 
        $shape->setHeight(480);
        $shape->setOffsetX($positionsByIndex[$position]); // Here the point
        $shape->setOffsetY(10);
    }
} 

最后一点但同样重要:不要在项目中使用mysql_ * 使用:PDOmysqli_*代替