AttachImageSize

Summary: Generate width and height attributes for attached images
Version: 20090106
Maintainer: Petko
Categories: Images, PHP55, PHP72
License: GPL2+

Description

Generate width and height attributes for attached images

The default Attach: markup for attached images doesn't include the image width and height attributes in the HTML tag. This recipe makes it possible to override this, and to automatically set these attributes.

Installation

Place the following code in local/config.php :

function LinkImageSize($pagename, $imap, $path, $alt, $txt, $fmt=NULL) {
  global $FmtV, $UploadFileFmt;
  if (preg_match('!^(.*)/([^/]+)$!', $path, $match)) {
    $pagename = MakePageName($pagename, $match[1]);
    $path = $match[2];
  }
  $upname = MakeUploadName($pagename, $path);
  $filepath = FmtPageName("$UploadFileFmt/$upname", $pagename);
  $s = @getimagesize($filepath);
  $FmtV['$ImageSize'] = @$s[3];
  $r = LinkUpload($pagename, $imap, $path, $alt, $txt, $fmt);
  $FmtV['$ImageSize'] = '';
  return $r;
}
$LinkFunctions['Attach:'] = 'LinkImageSize';
$ImgTagFmt="<img src='\$LinkUrl' alt='\$LinkAlt' title='\$LinkAlt' \$ImageSize />";
$FmtV['$ImageSize'] = '';

This function is a derivative work from core PmWiki functions, hence licensed under the GNU GPL v2 or later.

Notes

See this message by Pm for limits of such a method. Notably:

  • it only works with images attached to the wiki page and displayed via Attach: markup, not with other images (external, or via Path: or http: links);
  • if the getimagesize() PHP function is not enabled, the attributes are not set;
  • if an author tries to override the size via a wikistyle, it may not work well in all browsers.

Release Notes

  • 20090106 - first public release

See Also

Contributors

The recipe is written and maintained by Petko. Parts of code from scripts/upload.php by Pm.

Comments

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.