Forum


Replies: 7   Views: 5981
Image in templage : size change
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by jeremie  · 08-07-2013 - 19:12

There seems to be 3 methods for inserting an image in a template: replaceTemplateVariableByHTML, addTemplateVariable and addTemplateImage. Each of these methods inserts an image with dimensions that are different from the original image. - replaceTemplateVariableByHTML reduces the image size in the final document - addTemplateVariable and addTemplateImage both increase the image size in the final document You can see it in the examples : - when running Template_imageSimple.php, the source image, named logo_phpdocx.gif, is 179 x 118 px, but the image inserted in example_template_imageSimple.docx is 183 x 122 px - when running Template_image.php, the source image, named image.png, is 223 x 182 px, but the image inserted in example_template_image.docx is 301 x 247 px I also tried to use replaceTemplateVariableByHTML with many variations of the following code : $docx->replaceTemplateVariableByHTML('variable', 'block', '', $option_array); but this method produces an image smaller than the source image. I have spent a long time trying to figure out a way to work around the issue, but I haven't found anything. Please advise.


Posted by jeremie  · 08-07-2013 - 19:14

There seems to be 3 methods for inserting an image in a template: replaceTemplateVariableByHTML, addTemplateVariable and addTemplateImage.



Each of these methods inserts an image with dimensions that are different from the original image.



- replaceTemplateVariableByHTML reduces the image size in the final document



- addTemplateVariable and addTemplateImage both increase the image size in the final document



You can see it in the examples :



- when running Template_imageSimple.php, the source image, named logo_phpdocx.gif, is 179 x 118 px, but the image inserted in example_template_imageSimple.docx is 183 x 122 px



- when running Template_image.php, the source image, named image.png, is 223 x 182 px, but the image inserted in example_template_image.docx is 301 x 247 px



I also tried to use replaceTemplateVariableByHTML with many variations of the following code :



$docx->replaceTemplateVariableByHTML('variable', 'block', '', $option_array);



but this method produces an image smaller than the source image.



I have spent a long time trying to figure out a way to work around the issue, but I haven't found anything. Please advise.


Posted by jorgelj  · 09-07-2013 - 08:57

Hello,



Before adding the images they are resized based on format and dpi to get the best quality for DOCX documents.



If you need to do any custom change, you can check CreateImage.inc file on createImage method, where there's a switch where size is calculated if mime type is png, jpg or gif.



Regards.


Posted by jeremie  · 09-07-2013 - 20:34

I've tried to change CreateImage.inc but it didn't work. I went as far as supressing the whole "switch ($attributes['mime'])".



 



What changes must be made?


Posted by jorgelj  · 10-07-2013 - 08:36

Hello,



It depends on the method. If you're using replaceTemplateVariableByHTML you must check getImageDimensions method in HTML2WordML file. And replaceImage in CreateTemplate.inc for addTemplateImage.



We're checking how to refactore that code to get a single method.



Regards.


Posted by jorgelj  · 10-07-2013 - 08:51

Hello,



Another solution for addTemplateImage is that you can set width, height and dpi using custom values. For example:



* width (mixed): the value in cm (float) or 'auto' (use image size)

* height (mixed): the value in cm (float) or 'auto' (use image size)

* dpi (int): dots per inch. This parameter is only taken into account if width or height are set to auto.



public function addTemplateImage($var, $pathImage, $options = array())



and the same in addImage. If you're using HTML you can set width and height in image tag.



Regards.


Posted by jeremie  · 10-07-2013 - 19:38

I can't use HTML since it produces an image smaller than the source image. The following code :



$docx->replaceTemplateVariableByHTML('image', 'inline', '<img src="logo-prospect-2.jpg" width="200" height="100" ></img>');



 



gives an image that is actually 155 x 80.



 



For using addTemplateImage, I need to know the size of the image in cm or the dpi. I only know the size of the image in pixels. How cant I convert from pixels to cm ?