I just wrote a function for this earlier tonight. This gets the image size from the server, and checks if it is too large and/or too wide to fit on the word document. It then finds the appropriate scaling factor to size it either at 100% width, height auto, or height 100%, width auto, depending on which ratio is bigger. Word uses 96dpi for images, so all the math is based off that.
getScalingFactor( $imgDimension, $docDimension ) {
return 99 - ( ( $docDimension / $imgDimension ) * 100 );
}
if( in_array( $file_data['extension'], $allowed_images ) ) {
//get image data
list( $imgWidth, $imgHeight ) = $image_sizes = getimagesize( $file_to_append );
//max doc sizes at 96dpi
$docWidth = 718;
$docHeight = 1122;
$widthDif = 0;
$heightDif = 0;
$widthDif = $imgWidth - $docWidth;
$heightDif = $imgHeight - $docHeight;
//get scaling number
if( $heightDif > 0 && $widthDif >0 ) {
//both are oversized, which is bigger?
if( $heightDif >= $widthDif ) {
//height >= width, scale by height
$scaling = getScalingFactor( $imgHeight, $docHeight );
} else {
//width > height, scale by width
$scaling = getScalingFactor( $imgWidth, $docWidth );
}
} elseif( $heightDif > 0 ) {
//height has a larger difference than height. Let's scale to match
$scaling = $func->getScalingFactor( $imgHeight, $docHeight );
} elseif( $widthDif > 0 ) {
//width has a larger difference than height. Let's scale to match
$scaling = $func->getScalingFactor( $imgWidth, $docWidth );
} else {
$scaling = 100;
}
//prep the options
$imgOptions = array(
'name' => $file_to_append,
'scaling' => $scaling,
'dpi' => 96,
//'sizeX' => '718px',
//'sizeY' => '1122px',
'spacingTop' => 0,
'spacingBottom' => 0,
'spacingLeft' => 0,
'spacingRight' => 0,
'textWrap' => 0,
'border' => 0,
'borderDiscontinuous' => 1
);
//create and add the image to a document
$docx = new createDocx();
$docx->addImage( $imgOptions );
$docOptions = array(
'marginTop' => 0,
'marginRight' => 0,
'marginBottom' => 0,
'marginLeft' => 0,
'marginHeader' => 0,
'marginFooter' => 0
);
$docx->modifyPageLayout( 'A4', $docOptions );
$docx->createDocx( 'one-page-with-image-spanning-fully' );
}