如何在php中将ppt幻灯片转换为jpeg图像

发布于 2024-12-24 18:07:54 字数 193 浏览 1 评论 0 原文

我在这个论坛上看到了一些类似的问题,但所有这些问题都是针对 .NET 平台的,因此请不要将其作为重复项关闭。我有一个 Linux 系统,我想通过 php 或 shell 脚本将幻灯片转换为图像(不太可取)。 convert 命令可以将 pdf 转换为 jpg,但不能将 ppt 转换。

任何帮助都会很棒。

I saw some similar questions on this forum but all those were for .NET platform so please don't close it as duplicate. I have a linux system and I want to convert slide to images via php or shell script(less preferable). the convert command can convert pdf to jpg's but not a ppt.

Any help would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜妞爱困 2024-12-31 18:07:54

我通过首先将 powerpoint 文件转换为 pdf 来完成此任务。这需要我在我的服务器上安装 libre office。然后使用图像魔法轻松将 pdf 转换为图像。
这是我的一些代码。它使用 https://github.com/ncjoes/office-converter (对于 ppt pdf 转换)和 https://github.com/spatie/pdf-to-image(对于pdf 到图像转换)

  //now convert file to pdf
            $converter = new OfficeConverter($newFile);
            $result = $converter->convertTo('output.pdf');

            $pdfFile = BASE_PATH.'/output.pdf';
            //now convert pdf file to images
            chdir(BASE_PATH); 

            //get total number of pages in pdf file
            $pdf = new \Spatie\PdfToImage\Pdf($pdfFile);
            $pdf->setCompressionQuality(80);
            $pages = $pdf->getNumberOfPages();

            for($i=1;$i<=$pages;$i++){
                $pdf->setPage($i)->saveImage(BASE_PATH."/image$i.jpg");

            }

I was able to accomplish this by first converting the powerpoint file to pdf. This required me installing libre office on my server. Then converting the pdf to images is easily done using image magic.
Here is some of my code. It uses https://github.com/ncjoes/office-converter (for the ppt to pdf conversion) and https://github.com/spatie/pdf-to-image (for the pdf to image conversion)

  //now convert file to pdf
            $converter = new OfficeConverter($newFile);
            $result = $converter->convertTo('output.pdf');

            $pdfFile = BASE_PATH.'/output.pdf';
            //now convert pdf file to images
            chdir(BASE_PATH); 

            //get total number of pages in pdf file
            $pdf = new \Spatie\PdfToImage\Pdf($pdfFile);
            $pdf->setCompressionQuality(80);
            $pages = $pdf->getNumberOfPages();

            for($i=1;$i<=$pages;$i++){
                $pdf->setPage($i)->saveImage(BASE_PATH."/image$i.jpg");

            }
稍尽春風 2024-12-31 18:07:54

http://code.google.com/p/jodconverter/ 似乎拥有所有建筑物块到位,甚至还有一个示例网络应用程序。

我们不久前在 http://sourceforge.net/projects/jodconverter/ 成功使用了旧版本,但具体细节我实在记不清了。

http://code.google.com/p/jodconverter/ seems to have all the building blocks in place, there is even a sample webapp.

We used the old version at http://sourceforge.net/projects/jodconverter/ successfully some time ago, but thI really can't remember the details.

夏有森光若流苏 2024-12-31 18:07:54

我认为那不可能。使用 .NET 意味着用户正在创建一个 powerpoint 应用程序的实例,并要求它将特定的幻灯片打印为 JPG 或 PDF,但对于 PHP,我认为这不可能在 Linux 系统中实现。

如果您可以在 Windows 服务器上运行,那么您可以使用 PHP 的 COM 接口来创建 COM 应用程序并启动已安装的 PowerPoint 应用程序,只要 COM 组件公开必要的方法(可能是 PRINT( ))

祝你好运

I don't think thats possible. Using .NET would means that the user is creating an instance of a powerpoint application and asking it to print a specific slide to a JPG or PDF but in the case of PHP i don't think it could be possible from a linux system.

In the event you can go on windows server, then you could use the COM interface of PHP to create a COM application and start an installed PowerPoint application and do the same thing as long as the COM component is exposing the necessary methods (probably PRINT())

Good luck

顾挽 2024-12-31 18:07:54

在 shell 脚本中,您可以使用 Unoconv 这是一个简单的命令行包装器 LibreOffice 这将使您能够转换到合理的质量。

对于可以直接从 PHP(以及 Linux 上)调用的更高质量输出的解决方案,您可以使用专用的文件转换 API,例如 扎姆扎尔

提交 PPT(或 PPTX)文件以转换为 JPEG 的代码如下(更多信息请参见

<?php
// Build request
$endpoint = "https://api.zamzar.com/v1/jobs";
$apiKey = "YOUR_KEY";
$sourceFilePath = "/tmp/my.ppt"; // Or PPTX
$targetFormat = "jpg";

$sourceFile = curl_file_create($sourceFilePath);    
$postData = array(
  "source_file" => $sourceFile,
  "target_format" => $targetFormat
);

// Send request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":");
$body = curl_exec($ch);
curl_close($ch);

// Process response (with link to converted files)
$response = json_decode($body, true);
print_r($response);
?>

From a shell script you could use Unoconv which is a simple command line wrapper to LibreOffice which would enable you to convert to a reasonable quality.

For a solution with higher quality output that can be called directly from PHP (and on Linux) you could use a dedicated file conversion API such as Zamzar.

The code to submit a PPT (or PPTX) file for conversion into JPEG would be as follows (more info in the documentation):

<?php
// Build request
$endpoint = "https://api.zamzar.com/v1/jobs";
$apiKey = "YOUR_KEY";
$sourceFilePath = "/tmp/my.ppt"; // Or PPTX
$targetFormat = "jpg";

$sourceFile = curl_file_create($sourceFilePath);    
$postData = array(
  "source_file" => $sourceFile,
  "target_format" => $targetFormat
);

// Send request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":");
$body = curl_exec($ch);
curl_close($ch);

// Process response (with link to converted files)
$response = json_decode($body, true);
print_r($response);
?>
计㈡愣 2024-12-31 18:07:54

您好,您需要在 php.ini 中启用 COM,然后您可以尝试一下吗

<?php

$ppApp = new COM("PowerPoint.Application");
$ppApp->Visible = True;
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
$ppName = "MySlides.ppt";
$FileName = "MyPP";
//*** Open Document ***//
$ppApp->Presentations->Open(realpath($ppName));
//*** Save Document ***//
$ppApp->ActivePresentation->SaveAs($strPath."/".$FileName,17);  //'*** 18=PNG, 19=BMP **'
//$ppApp->ActivePresentation->SaveAs(realpath($FileName),17);
$ppApp->Quit;
$ppApp = null;

?>

hi you need to enable COM in php.ini then you can try this out

<?php

$ppApp = new COM("PowerPoint.Application");
$ppApp->Visible = True;
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
$ppName = "MySlides.ppt";
$FileName = "MyPP";
//*** Open Document ***//
$ppApp->Presentations->Open(realpath($ppName));
//*** Save Document ***//
$ppApp->ActivePresentation->SaveAs($strPath."/".$FileName,17);  //'*** 18=PNG, 19=BMP **'
//$ppApp->ActivePresentation->SaveAs(realpath($FileName),17);
$ppApp->Quit;
$ppApp = null;

?>

¢好甜 2024-12-31 18:07:54

.pptx 文件只是一个压缩文件。因此,要获取 .jpeg 文件,您所需要做的就是解压缩该文件,然后使用 .jpeg 文件:

然后包含图像文件的文件夹位于 \ppt\media\ 文件夹中。

https://www.php.net/manual/en/ziparchive.extractto。 php 提供了用于提取压缩文件的 php 代码。

A .pptx file is just a zipped file. So all you need to do to get the .jpeg files is to unzip the file and then use the .jpeg files:

Then the folder with the image files is in \ppt\media\ folder.

https://www.php.net/manual/en/ziparchive.extractto.php gives php code to extract the zipped file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文