@@ -303,10 +303,67 @@ private function getImageArgs($varNameWithArgs)
303303 return $ varInlineArgs ;
304304 }
305305
306- private function prepareImageAttrs ($ replaceImage , $ varInlineArgs )
306+ private function chooseImageDimension ($ baseValue , $ inlineValue , $ defaultValue )
307+ {
308+ $ value = $ baseValue ;
309+ if (is_null ($ value ) && isset ($ inlineValue )) {
310+ $ value = $ inlineValue ;
311+ }
312+ if (!preg_match ('/^([0-9]*(cm|mm|in|pt|pc|px|%|em|ex|)|auto)$/i ' , $ value )) {
313+ $ value = null ;
314+ }
315+ if (is_null ($ value )) {
316+ $ value = $ defaultValue ;
317+ }
318+ if (is_numeric ($ value )) {
319+ $ value .= 'px ' ;
320+ }
321+
322+ return $ value ;
323+ }
324+
325+ private function fixImageWidthHeightRatio (&$ width , &$ height , $ actualWidth , $ actualHeight )
307326 {
308- $ sizeRegexp = '/^([0-9]*(cm|mm|in|pt|pc|px|%|em|ex|)|auto)$/i ' ;
327+ $ imageRatio = $ actualWidth / $ actualHeight ;
328+
329+ if (($ width === '' ) && ($ height === '' )) { // defined size are empty
330+ $ width = $ actualWidth . 'px ' ;
331+ $ height = $ actualHeight . 'px ' ;
332+ } elseif ($ width === '' ) { // defined width is empty
333+ $ heightFloat = (float ) $ height ;
334+ $ widthFloat = $ heightFloat * $ imageRatio ;
335+ $ matches = array ();
336+ preg_match ("/\d([a-z%]+)$/ " , $ height , $ matches );
337+ $ width = $ widthFloat . $ matches [1 ];
338+ } elseif ($ height === '' ) { // defined height is empty
339+ $ widthFloat = (float ) $ width ;
340+ $ heightFloat = $ widthFloat / $ imageRatio ;
341+ $ matches = array ();
342+ preg_match ("/\d([a-z%]+)$/ " , $ width , $ matches );
343+ $ height = $ heightFloat . $ matches [1 ];
344+ } else { // we have defined size, but we need also check it aspect ratio
345+ $ widthMatches = array ();
346+ preg_match ("/\d([a-z%]+)$/ " , $ width , $ widthMatches );
347+ $ heightMatches = array ();
348+ preg_match ("/\d([a-z%]+)$/ " , $ height , $ heightMatches );
349+ // try to fix only if dimensions are same
350+ if ($ widthMatches [1 ] == $ heightMatches [1 ]) {
351+ $ dimention = $ widthMatches [1 ];
352+ $ widthFloat = (float ) $ width ;
353+ $ heightFloat = (float ) $ height ;
354+ $ definedRatio = $ widthFloat / $ heightFloat ;
309355
356+ if ($ imageRatio > $ definedRatio ) { // image wider than defined box
357+ $ height = ($ widthFloat / $ imageRatio ) . $ dimention ;
358+ } elseif ($ imageRatio < $ definedRatio ) { // image higher than defined box
359+ $ width = ($ heightFloat * $ imageRatio ) . $ dimention ;
360+ }
361+ }
362+ }
363+ }
364+
365+ private function prepareImageAttrs ($ replaceImage , $ varInlineArgs )
366+ {
310367 // get image path and size
311368 $ width = null ;
312369 $ height = null ;
@@ -322,84 +379,23 @@ private function prepareImageAttrs($replaceImage, $varInlineArgs)
322379 $ imgPath = $ replaceImage ;
323380 }
324381
382+ $ width = $ this ->chooseImageDimension ($ width , isset ($ varInlineArgs ['width ' ]) ? $ varInlineArgs ['width ' ] : null , 115 );
383+ $ height = $ this ->chooseImageDimension ($ height , isset ($ varInlineArgs ['height ' ]) ? $ varInlineArgs ['height ' ] : null , 70 );
384+
325385 $ imageData = @getimagesize ($ imgPath );
326386 if (!is_array ($ imageData )) {
327387 throw new Exception (sprintf ('Invalid image: %s ' , $ imgPath ));
328388 }
329389 list ($ actualWidth , $ actualHeight , $ imageType ) = $ imageData ;
330- $ imageMimeType = image_type_to_mime_type ($ imageType );
331-
332- // choose width
333- if (is_null ($ width ) && isset ($ varInlineArgs ['width ' ])) {
334- $ width = $ varInlineArgs ['width ' ];
335- }
336- if (!preg_match ($ sizeRegexp , $ width )) {
337- $ width = null ;
338- }
339- if (is_null ($ width )) {
340- $ width = 115 ;
341- }
342- if (is_numeric ($ width )) {
343- $ width .= 'px ' ;
344- }
345-
346- // choose height
347- if (is_null ($ height ) && isset ($ varInlineArgs ['height ' ])) {
348- $ height = $ varInlineArgs ['height ' ];
349- }
350- if (!preg_match ($ sizeRegexp , $ height )) {
351- $ height = null ;
352- }
353- if (is_null ($ height )) {
354- $ height = 70 ;
355- }
356- if (is_numeric ($ height )) {
357- $ height .= 'px ' ;
358- }
359390
360391 // fix aspect ratio (by default)
361392 if (empty ($ varInlineArgs ['ratio ' ]) || $ varInlineArgs ['ratio ' ] !== 'f ' ) {
362- $ imageRatio = $ actualWidth / $ actualHeight ;
363-
364- if (($ width === '' ) && ($ height === '' )) { // defined size are empty
365- $ width = $ actualWidth . 'px ' ;
366- $ height = $ actualHeight . 'px ' ;
367- } elseif ($ width === '' ) { // defined width is empty
368- $ heightFloat = (float ) $ height ;
369- $ widthFloat = $ heightFloat * $ imageRatio ;
370- $ matches = array ();
371- preg_match ("/\d([a-z%]+)$/ " , $ height , $ matches );
372- $ width = $ widthFloat . $ matches [1 ];
373- } elseif ($ height === '' ) { // defined height is empty
374- $ widthFloat = (float ) $ width ;
375- $ heightFloat = $ widthFloat / $ imageRatio ;
376- $ matches = array ();
377- preg_match ("/\d([a-z%]+)$/ " , $ width , $ matches );
378- $ height = $ heightFloat . $ matches [1 ];
379- } else { // we have defined size, but we need also check it aspect ratio
380- $ widthMatches = array ();
381- preg_match ("/\d([a-z%]+)$/ " , $ width , $ widthMatches );
382- $ heightMatches = array ();
383- preg_match ("/\d([a-z%]+)$/ " , $ height , $ heightMatches );
384- // try to fix only if dimensions are same
385- if ($ widthMatches [1 ] == $ heightMatches [1 ]) {
386- $ dimention = $ widthMatches [1 ];
387- $ widthFloat = (float ) $ width ;
388- $ heightFloat = (float ) $ height ;
389- $ definedRatio = $ widthFloat / $ heightFloat ;
390-
391- if ($ imageRatio > $ definedRatio ) { // image wider than defined box
392- $ height = ($ widthFloat / $ imageRatio ) . $ dimention ;
393- } elseif ($ imageRatio < $ definedRatio ) { // image higher than defined box
394- $ width = ($ heightFloat * $ imageRatio ) . $ dimention ;
395- }
396- }
397- }
393+ $ this ->fixImageWidthHeightRatio ($ width , $ height , $ actualWidth , $ actualHeight );
398394 }
399395
400396 $ imageAttrs = array (
401397 'src ' => $ imgPath ,
402- 'mime ' => $ imageMimeType ,
398+ 'mime ' => image_type_to_mime_type ( $ imageType ) ,
403399 'width ' => $ width ,
404400 'height ' => $ height ,
405401 );
@@ -508,7 +504,7 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
508504 // add image to relations
509505 $ this ->tempDocumentRelations [$ partFileName ] = str_replace ('</Relationships> ' , $ xmlImageRelation , $ this ->tempDocumentRelations [$ partFileName ]) . '</Relationships> ' ;
510506
511- // collect prepared replaces
507+ // replace variable
512508 $ varNameWithArgsFixed = self ::ensureMacroCompleted ($ varNameWithArgs );
513509 $ matches = array ();
514510 if (preg_match ('/(<[^<]+>)([^<]*)( ' . preg_quote ($ varNameWithArgsFixed ) . ')([^>]*)(<[^>]+>)/Uu ' , $ partContent , $ matches )) {
0 commit comments