Reputation: 9606
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.
Upvotes: 9
Views: 11291
Reputation: 1
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.
Upvotes: 0
Reputation: 46
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");
}
Upvotes: 3
Reputation: 502
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);
?>
Upvotes: 1
Reputation: 935
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;
?>
Upvotes: 0
Reputation: 65244
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.
Upvotes: 2
Reputation: 12244
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
Upvotes: 1