phpphppresentation

PhpPresentation imagecreatefromstring(): Data is not in a recognized format - PHP7.2


I am trying to use PhpPresentation to read a sample.pptx file using the simple instructions for readers in their docs and I am getting:

imagecreatefromstring(): Data is not in a recognized format

I have checked that I have PHP7.2-gd installed and all of the other dependencies.

My Code:

require_once 'vendor/autoload.php';

use \PhpOffice\PhpPresentation\PhpPresentation;
use \PhpOffice\PhpPresentation\IOFactory;
use \PhpOffice\PhpPresentation\Style\Color;
use \PhpOffice\PhpPresentation\Style\Alignment;

$oReader = IOFactory::createReader('PowerPoint2007');
$data = $oReader->load(__DIR__ . '/sample.pptx');
var_dump($data);

Can anybody help me understand the issue?


Solution

  • After peeking into the PHP source code, to have some insights about the "imagecreatefromstring" function, I've discovered that it handles only the following image formats:

    PHP recognizes the format of the image contained in the argument of the "imagecreatefromstring" function by checking the image signature, as explained here.
    When an unknown signature is detected, the warning "Data is not in a recognized format" is raised.
    Therefore, the only plausible explanation for the error that you are experiencing is that your PPTX file contains an image that is not in one of the above formats.
    You can view the format of the images inside your PPTX file by changing its extension from ".pptx" to ".zip" and then opening it.
    You should see something like this:

    Archive:  sample.pptx
      Length      Date    Time    Name
    ---------  ---------- -----   ----
         5207  1980-01-01 00:00   [Content_Types].xml
          ...
         6979  1980-01-01 00:00   ppt/media/image1.jpeg
         6528  1980-01-01 00:00   ppt/media/image2.jpeg
       178037  1980-01-01 00:00   ppt/media/image3.jpeg
       229685  1980-01-01 00:00   ppt/media/image4.jpeg
       164476  1980-01-01 00:00   ppt/media/image5.jpeg
         6802  1980-01-01 00:00   ppt/media/image6.png
        19012  1980-01-01 00:00   ppt/media/image7.png
        32146  1980-01-01 00:00   ppt/media/image8.png
          ...
    ---------                     -------
       795623                     74 files
    

    As you can see, my sample.pptx file contains some images in JPEG and PNG format.
    Maybe your sample file contains some slides with images in a vector format (WMF or EMF); it's unclear to me (since I didn't find any reference in the docs) if those formats are supported or not. Eventually you should try with other PPTX files, just to make sure that the problem is not related to a specific one (you can find some under "test/resources/files").
    I've searched for a list of the supported image formats for PowerPoint files, but I haven't been able to find a precise response.
    The only relevant links that I've found are the following:

    This means that also the presence in the PPTX file of an image in the TIFF or PICT (QuickDraw) format could lead to the error under consideration.