CristalTeam/pptx

Replace placeholders with image

Closed this issue · 5 comments

Hi,

Simple question can i display an image in a moustache tags like {{evidence_img}} from my powerpoint template with img from [this] (https://avatars.dicebear.com/api/avataaars/exciting.svg) svg file for example. Tried unescaped html tags {{{evidence_img}}} but it does not render the html img tags. instead only } are shown..

tried and able to solve it..but it does not support return statement with an array. only string are accepted. my code as per below:

// get second slide
  $slide_img = $template->getSlides()[2];

        $slide_img->images(function (){
            $attachments = ['first_img' => file_get_contents(base_path() . '/public/images/avatar/default.png'),
                            'second_img' => file_get_contents(base_path() . '/public/images/avatar/admin.png')];
            $data = null;
 // loop value of images string file get content values
            foreach ($attachments as $attachment_key => $attachment)
            {
              $data = $data . $attachment;
            }
// return the variable for image rendering
            return $data;
        });

Able to generate an image but it does not support multi image template replacement.

TZK- commented

Indeed, there is not documentation at the moment about the image templating (and other templating features).
When you want to display image through mustache syntax, you need to:

  • Add a dummy image in your slide
  • Add a description to this image. If I follow your first example, you'll need to set the description to evidence_img (note that there are no braces)

Then in your PHP code, you'll have to do like you've done in the code above. The closure you pass to image method must return the image raw content.

At the moment, it is not possible set multiple images at once per mustache tag. There are some contraints to do that with the current library behavior.

I keep the request in mind for future updates, but feel free to contribute if you can. Any technical idea to make your idea possible are welcome !

Another thing i discovered that for instance i have two image placeholders with two different image description and dummy image within the same slide. It replace the content of the slide with same image. Due to returning an array is not supported, i instantiated the images two times in my code.

Is this a bug produced? Or adding more than one image per slide is not supported?

 $slide_img->images(function (){
            $attachment = ['first_img' => file_get_contents(base_path() . '/public/images/avatar/default.png')];
            return $attachment['first_img'];
        });

      $slide_img->images(function (){
          $file = ['second_img' => file_get_contents(base_path() . '/public/images/logo/app-logo-sm.png')];
          return $file['second_img'];
      });


TZK- commented

With your code, I think it is normal that it replaces your 2 dummy images by the same image.

In your example, you did not specify which mustache placeholder you want to replace by your image. So when it performs image templating, it will just use the last callback passed to images method.

If you want to replace images the right way (My bad, I forgot to add it in my previous response):

<?php
      // images() method takes a parameter which is the current image "description" being processed
       $slide_img->images(function ($needle) {
            if ($needle !== 'your_first_description') {
                  return null; // Do not apply templating
            } 

            return file_get_contents(base_path() . '/public/images/avatar/default.png');
        });

      $slide_img->images(function ($needle) {
          if ($needle !== 'your_second_description') {
                  return null; // Do not apply templating
          } 

          return file_get_contents(base_path() . '/public/images/logo/app-logo-sm.png');
      });

That way it should work properly. Can you confirm it works ?

Yup, i can confirm it works now. Thanks for your kind help.
I will close the issue