Skip to content

Article Pictures Viewer Developer Guide

Ed Mozley edited this page Dec 1, 2026 · 1 revision

Pictures in an article open enlarged - developer guide

#474, raised by @Dr4inder. The Knowledge half shipped in 5.15.0; the portal Help Centre half in 5.17.0, when the code moved out of knowledge.js into a shared file. The user's view: Knowledge - Β§ Pictures in an article (5.15.0), and Portal Help Centre - Β§ Pictures in an article.

There is no new viewer. A click on a picture in an article hands the article's pictures, as a list of addresses, to the viewer Files already has (FilesViewer.openUrls() in assets/js/files-viewer.js) - the same window, zoom, Fit, full screen, Download, previous / next and Slideshow as a file in Files. All of it is in the browser: about 40 lines in assets/js/article-pictures.js, one call from each page, and one CSS rule for the pointer.

πŸ“ Files

File What
assets/js/article-pictures.js New in 5.17.0. ArticlePictures.init(body, pictureName) and ArticlePictures.linkIsPicture(link) - moved here from knowledge.js unchanged in behaviour
assets/js/knowledge.js kbInitArticlePictures(body) - now a one-line wrapper, called from renderArticleDetail()
knowledge/index.php Loads article-pictures.js just before knowledge.js
assets/js/files-viewer.js openUrls() (5.15.0) and the gallery's go(), which shows an item with its own src instead of asking view.php
self-service/includes/footer.php $needsPictureViewer: loads float-window.css, float-window.js, files-icons.js, files-viewer.js and article-pictures.js
self-service/help-centre.php Sets $needsPictureViewer = true and calls ArticlePictures.init() once the article is drawn
lang/en/knowledge.php, lang/en/self-service.php knowledge.detail.picture and self-service.help_centre.picture - Picture {n}
assets/css/knowledge.css, assets/css/self-service.css .kb-pictures-zoomable img { cursor: zoom-in; }

No schema change and no new endpoint.

πŸ–ΌοΈ Why no Files token - and so no Files access on the portal

Everything a file in Files shows comes through api/files/view.php with a short-lived token in the person's session. An article's pictures are not Files items: they are already on the page, served by api/knowledge/image.php. So the list handed to the viewer carries each picture's own address as src:

var list = pictures.map(function (p, i) {
    return {
        kind: 'image',
        name: (p.getAttribute('alt') || '').trim() || (pictureName ? pictureName(i + 1) : String(i + 1)),
        src: srcOf(p), downloadUrl: srcOf(p)
    };
});
window.FilesViewer.openUrls(list, pictures.indexOf(img));

and the gallery never calls Files' API for an item that has one:

// GH #474: an item with its own address (a picture in an article) is not a Files
// item - it is shown from that address, exactly as openUrl() shows one.
var load = list[i].src ? Promise.resolve(urlInfo(list[i])) : api({ action: 'open', id: list[i].id, version: 0 });

That is why the portal can use the viewer at all: a customer has no Files session and no Files permissions, and needs none. Who may see the picture is decided where it always was - api/knowledge/image.php serves an analyst session or a portal session (KnowledgeViewer::forPortalUser()) under the article's own rules, and nobody signed in gets nothing. The viewer adds no new way to reach a picture; it fetches the same address the <img> already loaded.

  • kind: 'image' is set on purpose. urlInfo() otherwise guesses the kind from the name's extension (kindOf()), and the name here is alt text or Picture 3, which has none.
  • downloadUrl is the same address, so Download saves the picture the reader is already allowed to see.
  • TRAP: $needsPictureViewer must stay pictures-only. The footer's comment says so: the portal is handed the article's own image addresses, never a Files token. Using the same switch to open a Files item on the portal would need view.php, which only analysts can call.

πŸ”— Which links open the viewer

Imported articles often wrap each picture in a link to the picture itself - typically a small copy linked to the full-size one (<a href="image.php?id=374"><img src="image.php?id=381"></a>, a wiki export). Followed, that link left the article for the bare image, and Back was the only way home. linkIsPicture() decides which links the viewer may take over:

/** True for a link that holds only one picture and points at a picture. */
function linkIsPicture(link) {
    if (!link || link.querySelectorAll('img').length !== 1 || link.textContent.trim() !== '') return false;
    var href = link.getAttribute('href') || '';
    return /api\/knowledge\/image\.php/i.test(href) || /\.(png|jpe?g|gif|webp|bmp|svg)(\?|#|$)/i.test(href)
        || href === link.querySelector('img').getAttribute('src');
}

All three must hold: exactly one <img> inside, no text, and an href that is a Knowledge picture (api/knowledge/image.php), ends in a picture extension (png, jpg, jpeg, gif, webp, bmp, svg, before any ? or #), or is exactly the picture's own src.

When it holds, the viewer opens at the link's address, because that is often the full-size picture:

var srcOf = function (img) {
    var link = img.closest('a');
    return link && body.contains(link) && linkIsPicture(link) ? link.href : img.src;
};
  • TRAP: every other link stays a link. A picture inside a link to a web page, a ticket or another article is the author's way of saying "go there"; the click handler returns before preventDefault(), so the browser follows it:

    var link = img.closest('a');
    if (link && body.contains(link) && !linkIsPicture(link)) return;   // a real link round a picture stays a link
  • body.contains(link) is there because closest('a') walks out of the article body - a link round the whole article view must not count.

  • link.href (resolved, absolute) is what is handed over; getAttribute('href') (as written) is what is tested, so a relative image.php?id= matches the pattern.

πŸ–±οΈ Clicks the viewer leaves alone

if (!img || !body.contains(img) || event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;

Only a plain left click opens the viewer. Ctrl-click, Cmd-click, Shift-click and middle-click fall through to the browser, so on a picture wrapped in a link they still open the link in a new tab or window, as anywhere else on the web. One listener on the body handles every picture (event delegation), so a picture added later in the same body would be caught too - but the list to step through is gathered once, in init().

πŸ“± On a phone

openUrls() decides by the window's width:

if (list.length === 1 || window.innerWidth <= 768) {
    if (window.innerWidth <= 768) return openPhone(urlInfo(list[index]));
    return openUrl(Object.assign({ key: opts.key }, list[index]));
}
return openList(list, index, { key: opts.key });
Case Opens
768px wide or less openPhone() - the viewer full screen over the page with a back arrow, the clicked picture only (no previous / next) - as open() does for a file in Files on a phone
One picture in the article openUrl() - a floating window over the page (FloatWin.page()), focus moved into it so Esc closes it straight away
More than one openList() - one window that steps through them (arrows, n of total, Slideshow), opened at the one clicked

🧩 The two callers

Knowledge keeps its old function name, so nothing else in knowledge.js changed:

function kbInitArticlePictures(body) {
    if (window.ArticlePictures) window.ArticlePictures.init(body, n => window.t('knowledge.detail.picture', { n: n }));
}

renderArticleDetail() calls it on .article-content-body after the body is drawn. float-window.js, files-icons.js and files-viewer.js were already on every analyst page - includes/waffle-menu.php calls documentsPanelAssets() so documents open in place (#303) - so knowledge/index.php only adds article-pictures.js, before knowledge.js.

The Help Centre draws the article body through safeArticleHtml() (safeHtmlFragment() from safe-html.js, or plain escaped text if that file did not load), then:

if (window.ArticlePictures) {
    window.ArticlePictures.init(container.querySelector('.hc-body'),
        n => window.t('self-service.help_centre.picture', { n: n }));
}

The portal had none of the viewer's files, so self-service/includes/footer.php loads them only for a page that asks:

<?php if (!empty($needsPictureViewer)): ?>
<link rel="stylesheet" href="../assets/css/float-window.css?v=3">
<script src="../assets/js/float-window.js?v=3"></script>
<script src="../assets/js/files-icons.js?v=4"></script>
<script src="../assets/js/files-viewer.js?v=13"></script>
<script src="../assets/js/article-pictures.js?v=1"></script>
<?php endif; ?>
  • TRAP: order matters. files-viewer.js reads window.FloatWin.esc as it loads, so float-window.js must come first; article-pictures.js only needs FilesViewer when a picture is clicked, but keep it last anyway.
  • It fails quietly. init() returns at once when FilesViewer.openUrls is missing or the body has no pictures - the article reads as before, pictures and all, just not clickable. The .kb-pictures-zoomable class (the zoom-in pointer) is only added when the viewer will actually open.

βž• Using it on another page

  1. Make sure float-window.js, files-icons.js and files-viewer.js are on the page (analyst pages have them; a portal page sets $needsPictureViewer = true before including the footer, which also brings article-pictures.js).
  2. On an analyst page, add <script src="../assets/js/article-pictures.js?v=1"></script>.
  3. After drawing the HTML, call ArticlePictures.init(bodyElement, n => window.t('your.key', { n: n })) - the second argument names a picture with no alt text.
  4. Add .kb-pictures-zoomable img { cursor: zoom-in; } to the page's CSS if it is not there.
  5. Call init() once per drawn body. Each call adds a click listener, so calling it twice on the same element would open the viewer twice. Both current callers redraw the article into a new element each time, which is why they can call it on every open.

Only hand it HTML whose picture addresses the reader may already load - it adds no permission check of its own.

πŸ§ͺ Tests

No test in tests/ covers this - it is browser behaviour over the existing viewer, and nothing on the server changed. When changing article-pictures.js, check by hand in both Knowledge and the Help Centre:

Case Expected
One picture a floating window; Esc closes it
Several pictures one window, previous / next, n of total, Slideshow
A picture wrapped in a link to its full-size copy the viewer, at the link's address - the article is not left
A picture inside a link to a web page or another record the link is followed
Ctrl-click or middle-click on a linked picture a new tab, as before
A window 768px wide or less the clicked picture full screen, with a back arrow
The portal, signed in as a customer pictures come from api/knowledge/image.php; nothing is asked of api/files/

See also

FreeITSM

Getting Started

Modules

Multi-tenancy (planned)

Responses to feedback

Blue sky thinking

Bugs resolved

Pull requests

Links

Clone this wiki locally