PHP网站中PPT幻灯片转换为JPG图片的方法及代码
来源:绵阳动力网络公司  时间:2021-07-19  阅读:10

PHP网站中PPT幻灯片转换为JPG图片的方法及代码

在PHP的网站建设中,曾经遇到过一个学科类的网站开发,其中有一个课件栏目,客户的需求是将后台上传的PPT幻灯片转换为JPG图片显示出来。根据需求来制定解决方法。如果PPT数量不多可以用截图的方法来解决,但客户要上传几十个PPT,内容有几百页,截图的方法行不通。于是在网站上搜索能自动转换的方法,经过不断的搜索和实践,现将具体的方法和代码记录下来,希望对大家网站建设中遇到这样的问题有所帮助。

一、安装COM组件

1、如php版本>5.3.15,需要保证ext文件夹下有php_com_dotnet.dell 并在php.ini中加入。

extension=php_com_dotnet.dll

2、去除com.allow_dcom = true前面的‘;'号。

com.allow_dcom = true

二、代码内容:

$powerpnt = new COM("powerpoint.application") or die("Unable to instantiate Powerpoint");
$file='E:/APP/OTHER/qwe.pptx';
$presentation = $powerpnt->Presentations->Open(realpath($file), false, false, false) or die("Unable to open presentation");
foreach($presentation->Slides as $slide)
{
$slideName = "Slide_" . $slide->SlideNumber;
$uploadsFolder = 'iii';
$exportFolder = realpath($uploadsFolder);
$slide->Export($exportFolder."//".$slideName.".jpg", "jpg", "600", "400");
}
$presentation->Close(); 
$powerpnt->Quit();
$powerpnt = null;

?>

三、内容扩展:

从shell脚本中,您可以使用Unoconv,它是LibreOffice的简单命令行包装器,可以使您转换为合理的质量。

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

提交PPT(或PPTX)文件以转换为JPEG的代码如下(documentation中的更多信息):

/ 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);
?>

好了,通过以上三步就可以自动实现PHP转JPG了,希望今天的内容对你有所帮助,记得收藏哦。

 
  • 电话咨询

  • 0816-2318288