MaximumImageSize

Note: The recipes here are for PmWiki versions 0.6 and 1.0 only. For PmWiki 2.0 recipes, see Cookbook.


Goal

Prevent images wider than a certain number of pixels from getting automatically inlined, ruining the usability of the site.

Solution

Add the following code into your local configuration file and change the value of $ImageMaxWidth to taste:

 $ImageMaxWidth = 600;

 function MyFmtUrlLink($pat,$ref,$txt) {
   global  $InterMapUrls, $ImgExtPattern, $UrlLinkFmt, $UrlImgFmt, $UrlLinkTextFmt, $ImageMaxWidth;
   $link = $UrlLinkFmt; $rtxt=$ref;
   $isimage=false;

   if (!is_null($txt)) { $rtxt=$txt; $link=$UrlLinkTextFmt; }
   elseif (preg_match("/$ImgExtPattern\$/",$ref)) 
   {
  	 $isimage=true;
   }
   if (preg_match('/^([^:]*):/',$ref,$match)) {
     if (@$InterMapUrls[$match[1]]) 
       $ref=preg_replace("/^$pat\$/",$InterMapUrls[$match[1]],$ref);
   }
   if ($isimage) 
   {
     list($width, $height, $type, $attr) = getimagesize($ref);
     if ($width < $ImageMaxWidth) $link=$UrlImgFmt; 
   }   
   $link = str_replace('$Url',$ref,$link);
   return str_replace('$LinkText',$rtxt,$link);
 }

 $FmtUrlLink = 'MyFmtUrlLink';

See Also

For a more full-featured approach, have a look at PictureGallery.

Comments & Bugs

This code performs an additional access to the disk subsystem (through the getimage() function) for every displayed or linked image. This can lower system performance for applications that rely heavily on this functionality. In practice, it is unlikely to present a heavy burden since the information we are after is either already present in the buffer/disk cache, or would be loaded there soon anyway by the httpd process in order to serve the content.

No bugs are known at this time.

Acknowledgements

I would like to thank Pm and the rest of the PmWiki team for producing such a cleanly designed and flexible implementation.

Contributors

  • RanieriArgentini

pmwiki-2.3.38 -- Last modified by {{Simon}}

from IP: 85.171.160.186 ip should be disabled by default for security reasons