attachments::image::variant (public)

 attachments::image::variant path width [ identity ]

Defined in packages/attachments/tcl/image-procs.tcl

Return a cached, smaller bitmap, or the original when conversion is unavailable. Callers must authorize access to the original BEFORE calling this helper. An empty width requests the original. Identity should include the CR revision ID for committed images; drafts are distinguished by their private path.

Parameters:
path (required)
width (required)
identity (optional)

Partial Call Graph (max 5 caller/called nodes):
packages/forums/www/markdown-image.tcl packages/forums/ www/markdown-image.tcl attachments::image::variant attachments::image::variant packages/forums/www/markdown-image.tcl->attachments::image::variant packages/news/www/markdown-image.tcl packages/news/ www/markdown-image.tcl packages/news/www/markdown-image.tcl->attachments::image::variant ad_log ad_log (public) attachments::image::variant->ad_log attachments::image::directory attachments::image::directory (private) attachments::image::variant->attachments::image::directory attachments::image::widths attachments::image::widths (public) attachments::image::variant->attachments::image::widths image::convert_binary image::convert_binary (public) attachments::image::variant->image::convert_binary

Testcases:
No testcase defined.
Source code:
    if {$width eq ""} { return $path }
    if {$width ni [widths]} { error "Unsupported attachment image width" }
    if {![file isfile $path]
        || [catch {ns_imgtype $path} type] || $type ni {png jpeg gif}
        || [catch {ns_imgsize $path} dimensions]} {
        return $path
    }
    lassign $dimensions original_width original_height
    # Do not enlarge images or decode exceptionally large bitmaps.
    if {$original_width <= $width || $original_height <= 0
        || $original_width * wide($original_height) > 40000000} {
        return $path
    }
    set directory [directory]
    set fingerprint [ns_sha1 [list v1 $identity [file normalize $path]  [file mtime $path] [file size $path] $width $type]]
    set cached [file join $directory $fingerprint.$type]
    if {[file readable $cached]} { return $cached }
    set binary [::image::convert_binary]
    if {$binary eq ""} { return $path }

    # Bound concurrent conversions; a busy converter falls back to the original.
    set mutex [nsv_get attachments_image_cache mutex]
    if {[ns_mutex trylock $mutex] != 0} { return $path }
    set temporary ""
    try {
        if {[file readable $cached]} { return $cached }
        file mkdir $directory
        file attributes $directory -permissions 0700
        set channel [file tempfile temporary [file join $directory converting-]]
        close $channel
        # Explicit output format: cache temporary files have no extension.
        # Coalesce/optimize preserves animated GIFs rather than keeping one frame.
        exec $binary -limit thread 1 -limit memory 64MiB -limit map 128MiB  -limit disk 256MiB -limit time 30 $path -coalesce  -resize ${width}x> -layers Optimize ${type}:$temporary 2>@1
        if {[ns_imgtype $temporary] ne $type
            || [lindex [ns_imgsize $temporary] 0] != $width} {
            error "Unexpected resized attachment image"
        }
        file attributes $temporary -permissions 0600
        file rename -force -- $temporary $cached
        return $cached
    } on error {message options} {
        ad_log warning "Could not resize attachment image: $message"
        return $path
    } finally {
        if {$temporary ne ""} { catch {file delete -- $temporary} }
        ns_mutex unlock $mutex
    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: