fix: frame view handles the max-width and fit-to-width scale options refs #7135

This commit is contained in:
Jash committed 2026-11-21 22:50:26 +05:30
1 parent de4e032e3c
commit a1085fae36
7 files changed
+199 -13

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
'use strict';
// changeScale in the frame view must size the image for 'NNNpx' (max width) and
// 'fit_to_width', not multiply the base size by the number in the string.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const src = fs.readFileSync(process.argv[2] ||
path.join(__dirname, '../../web/skins/classic/views/js/frame.js'), 'utf8');
function size(scale, pageWidth) {
const css = {};
const stub = {
val: () => '',
css: (k, v) => {
css[k] = v;
return stub;
},
prop: () => stub,
off: () => stub,
on: () => stub,
width: () => pageWidth,
is: () => true,
toggle: () => stub,
ready: () => stub,
};
const globals = {
$j: Object.assign(function(selector) {
if (selector === '#scale') return Object.assign({}, stub, {val: () => scale});
if (selector === '#base_width') return Object.assign({}, stub, {val: () => '1920'});
if (selector === '#base_height') return Object.assign({}, stub, {val: () => '1080'});
return stub;
}, {each: () => {}}),
SCALE_BASE: 100,
setCookie: () => {},
// fits the page width, keeping the aspect ratio
scaleToFit: (w, h) => ({width: pageWidth, height: Math.floor(pageWidth * h / w), autoScale: 0}),
endOfResize: () => {},
document: {referrer: ''},
window: {},
console: {log() {}},
};
vm.createContext(globals);
vm.runInContext(src, globals, {filename: 'frame.js'});
globals.changeScale();
return css;
}
let css = size('480px', 1000);
assert.strictEqual(css.width, '480px');
assert.strictEqual(css.height, '270px');
console.log(' ok 480px caps the width at 480');
css = size('1600px', 1000);
assert.strictEqual(css.width, '1000px');
console.log(' ok a max width wider than the page is held to the page width');
css = size('fit_to_width', 1000);
assert.strictEqual(css.width, '1000px');
assert.strictEqual(css.height, '562px');
console.log(' ok fit_to_width fits the page width');
css = size('50', 1000);
assert.strictEqual(css.width, '960px');
assert.strictEqual(css.height, '540px');
console.log(' ok 50 still halves the size');
@@ -23,6 +23,7 @@ namespace ZM {
public $virtualCalls = array();
public $imageSrcResult = null;
public function __construct($eid) {}
public function Width() { return 1920; }
public function find_virtual_frame($fid) {
$this->virtualCalls[] = $fid;
return new VirtualFrame(4.5);
@@ -49,6 +50,7 @@ namespace {
$failures++;
echo " FAIL $name\n warnings: ".var_export($warnings, true)."\n";
}
define('SCALE_BASE', 100);
function translate($s) { return $s; }
function validInt($v) { return (int)$v; }
function validNum($v) { return (float)$v; }
@@ -90,6 +92,7 @@ namespace {
$stubRow = $dbResult;
$eid = 7;
$scale = 0;
$scales = array('0'=>'Auto', '100'=>'Actual', 'fit_to_width'=>'Fit', '480px'=>'480');
$Event = new \ZM\Event($eid);
$Event->imageSrcResult = $imageSrcResult;
$Monitor = new class { public function Id() { return 1; } public function DefaultScale() { return 100; } };
+3
View File
@@ -17,6 +17,7 @@ namespace ZM {
}
class Event {
public function __construct($eid) {}
public function Width() { return 1920; }
public function MaxScore() { return 99; }
public function getImageSrc($frame, $scale=0, $captureOnly=false) {
// Mirrors web/includes/Event.php::getImageSrc, which reads
@@ -42,6 +43,7 @@ namespace {
$failures++;
echo " FAIL $name\n warnings: ".var_export($warnings, true)."\n";
}
define('SCALE_BASE', 100);
function translate($s) { return $s; }
function validInt($v) { return (int)$v; }
function validNum($v) { return (float)$v; }
@@ -79,6 +81,7 @@ namespace {
$eid = 7;
$fid = 0;
$scale = 0;
$scales = array('0'=>'Auto', '100'=>'Actual', 'fit_to_width'=>'Fit', '480px'=>'480');
$Event = new \ZM\Event($eid);
$Monitor = new class { public function Id() { return 1; } public function DefaultScale() { return 100; } };
set_error_handler('collect_warning');
+91
View File
@@ -0,0 +1,91 @@
<?php
// The classic $scales list offers 'fit_to_width' and 'NNNpx' as well as
// percentages. The frame view must not read '480px' as 480 percent (a 1920px
// image became 9216px wide), and the select must keep the chosen key.
//
// Run as: php tests/php/test_frame_view_scale.php [web-root] (default: this checkout's web/)
namespace ZM {
class Event {
public function Width() { return 1920; }
public function Height() { return 1080; }
}
}
namespace {
$failures = 0;
function check($name, $got, $want) {
global $failures;
if ($got === $want) { echo " ok $name\n"; return; }
$failures++;
echo " FAIL $name\n got: ".var_export($got, true)."\n want: ".var_export($want, true)."\n";
}
define('SCALE_BASE', 100);
function validNum($input) {
if (!is_scalar($input)) return '';
return preg_replace('/[^\d.-]/', '', $input);
}
$web = isset($argv[1]) ? $argv[1] : __DIR__.'/../../web';
$fns = file_get_contents($web.'/includes/functions.php');
if (!preg_match('/^function reScale\(.*?^\}\n/ms', $fns, $m)) { echo "FAIL - reScale not found\n"; exit(1); }
eval($m[0]);
$cfg = file_get_contents($web.'/skins/classic/includes/config.php');
if (!preg_match('/^\$scales = array\(.*?^\);\n/ms', $cfg, $m)) { echo "FAIL - \$scales not found\n"; exit(1); }
function translate($s) { return $s; }
eval($m[0]);
$src = file_get_contents($web.'/skins/classic/views/frame.php');
$start = strpos($src, "if (isset(\$_REQUEST['scale'])) {");
$end = strpos($src, '$imageData = $Event->getImageSrc(');
if ($start === false || $end === false) { echo "FAIL - scale code not found in frame.php\n"; exit(1); }
$chunk = substr($src, $start, $end - $start);
function run($request, $default = 0) {
global $chunk, $scales;
$_REQUEST = $request;
$_COOKIE = array();
$Event = new \ZM\Event();
$Monitor = new class($default) {
private $d;
public function __construct($d) { $this->d = $d; }
public function Id() { return 1; }
public function DefaultScale() { return $this->d; }
};
eval($chunk);
return array(
'width' => reScale($Event->Width(), $scale),
'height' => reScale($Event->Height(), $scale),
'selected' => isset($scaleSelected) ? $scaleSelected : null,
);
}
echo "480px caps the width:\n";
$r = run(array('scale' => '480px'));
check('width is 480', $r['width'], 480);
check('height keeps the aspect ratio', $r['height'], 270);
check('select keeps 480px', $r['selected'], '480px');
$r = run(array('scale' => '1600px'));
check('1600px stays within 1600 and close to it', $r['width'] <= 1600 && $r['width'] > 1550, true);
echo "fit_to_width is sized by frame.js, not read as a percentage:\n";
$r = run(array('scale' => 'fit_to_width'));
check('server leaves the full size', $r['width'], 1920);
check('select keeps fit_to_width', $r['selected'], 'fit_to_width');
echo "percentages are unchanged:\n";
$r = run(array('scale' => '50'));
check('50 halves the width', $r['width'], 960);
check('select keeps 50', $r['selected'], '50');
$r = run(array('scale' => '0'));
check('0 (Auto) is the full size', $r['width'], 1920);
$r = run(array(), '480px');
check('monitor default 480px caps the width', $r['width'], 480);
echo "the select is given the raw selected value:\n";
check('htmlSelect uses $scaleSelected', strpos($src, "htmlSelect('scale', \$scales, \$scaleSelected") !== false, true);
exit($failures ? 1 : 0);
}
+20 -8
View File
@@ -69,13 +69,25 @@ $nav = frameNavigation($eid, (int)$Frame->FrameId());
$alarmFrame = ( $Frame->Type() == 'Alarm' ) ? 1 : 0;
if (isset($_REQUEST['scale'])) {
$scale = validNum($_REQUEST['scale']);
$scaleSelected = $_REQUEST['scale'];
} else if (isset($_COOKIE['zmWatchScale'.$Monitor->Id()])) {
$scale = validNum($_COOKIE['zmWatchScale'.$Monitor->Id()]);
$scaleSelected = $_COOKIE['zmWatchScale'.$Monitor->Id()];
} else if (isset($_COOKIE['zmWatchScale'])) {
$scale = validNum($_COOKIE['zmWatchScale']);
$scaleSelected = $_COOKIE['zmWatchScale'];
} else {
$scale = validNum($Monitor->DefaultScale());
$scaleSelected = $Monitor->DefaultScale();
}
// $scales also offers fit_to_width and NNNpx, so keep those keys for the select and the links.
if (!is_scalar($scaleSelected) || !array_key_exists((string)$scaleSelected, $scales)) {
$scaleSelected = validNum($scaleSelected);
}
$scaleSelected = (string)$scaleSelected;
$scale = $scaleSelected;
if ($scaleSelected === 'fit_to_width') {
$scale = 0; // frame.js fits the image to the page width
} else if (preg_match('/^(\d+)px$/', $scaleSelected, $m)) {
// Max width: the percentage that gives that width, never above actual size.
$scale = $Event->Width() ? min(SCALE_BASE, (int)floor($m[1]*SCALE_BASE/$Event->Width())) : 0;
}
$scale = $scale ? $scale : 0;
@@ -119,7 +131,7 @@ echo getNavBarHTML();
<form>
<div id="scaleControl">
<label for="scale"><?php echo translate('Scale') ?></label>
<?php echo htmlSelect('scale', $scales, $scale, array('data-on-change'=>'changeScale','id'=>'scale')); ?>
<?php echo htmlSelect('scale', $scales, $scaleSelected, array('data-on-change'=>'changeScale','id'=>'scale')); ?>
</div>
<input type="hidden" name="base_width" id="base_width" value="<?php echo $Event->Width(); ?>"/>
<input type="hidden" name="base_height" id="base_height" value="<?php echo $Event->Height(); ?>"/>
@@ -134,8 +146,8 @@ echo getNavBarHTML();
<p id="image">
<?php
if ($imageData['hasAnalysisImage']) {
echo sprintf('<a href="?view=frame&amp;eid=%d&amp;fid=%d&scale=%d&amp;show=%s" title="Click to display frame %s analysis">',
$Event->Id(), $Frame->FrameId(), $scale,
echo sprintf('<a href="?view=frame&amp;eid=%d&amp;fid=%d&scale=%s&amp;show=%s" title="Click to display frame %s analysis">',
$Event->Id(), $Frame->FrameId(), urlencode($scaleSelected),
($show=='analysis'?'capture':'analysis'),
($show=='analysis'?'without':'with')
);
@@ -153,7 +165,7 @@ if ($imageData['hasAnalysisImage']) {
if ($imageData['hasAnalysisImage']) { ?></a><?php } ?>
</p>
<?php
$frame_url_base = '?view=frame&amp;eid='.$Event->Id().'&amp;scale='.$scale.'&amp;show='.$show.'&amp;fid=';
$frame_url_base = '?view=frame&amp;eid='.$Event->Id().'&amp;scale='.urlencode($scaleSelected).'&amp;show='.$show.'&amp;fid=';
?>
<p id="controls">
<a id="firstLink" <?php echo $nav['first'] ? 'href="'.$frame_url_base.$nav['first'].'" class="btn-primary"' : 'class="btn-primary disabled"' ?>><?php echo translate('First') ?></a>
+12 -3
View File
@@ -16,7 +16,16 @@ function changeScale() {
if (img) {
const baseWidth = $j('#base_width').val();
const baseHeight = $j('#base_height').val();
if (!parseInt(scale)) {
if (scale == 'fit_to_width') {
const newSize = scaleToFit(baseWidth, baseHeight, img, false);
newWidth = newSize.width;
newHeight = newSize.height;
} else if (/^\d+px$/.test(scale)) {
// Max width, as in the event view: never wider than the page either
const fit = scaleToFit(baseWidth, baseHeight, img, false);
newWidth = Math.min(parseInt(scale), fit.width);
newHeight = Math.floor(newWidth * baseHeight / baseWidth);
} else if (!parseInt(scale)) {
const newSize = scaleToFit(baseWidth, baseHeight, img, $j('#controls'));
newWidth = newSize.width;
newHeight = newSize.height;
@@ -117,7 +126,7 @@ function onStatsResize(vidwidth) {
}
function initPage() {
if (scale == '0' || scale == 'auto') changeScale();
if (!Number(scale)) changeScale(); // 0, auto, fit_to_width and NNNpx are sized here
// Don't enable the back button if there is no previous zm page to go back to
backBtn.prop('disabled', !document.referrer.length);
@@ -160,7 +169,7 @@ function initPage() {
if (getFrameStatsCookie() != 'on') {
table.toggle(false);
} else {
onStatsResize($j('#base_width').val() * scale / SCALE_BASE);
if (Number(scale)) onStatsResize($j('#base_width').val() * scale / SCALE_BASE);
}
// Manage the FRAMES Button
+2 -2
View File
@@ -1,11 +1,11 @@
<?php
global $scale;
global $scaleSelected;
global $eid;
global $fid;
global $alarmFrame;
?>
var scale = '<?php echo validJsStr($scale); ?>';
var scale = '<?php echo validJsStr($scaleSelected); ?>';
var eid = <?php echo $eid ?>;
var fid = <?php echo $fid ?>;