output an error message image when we can't load a jpeg

This commit is contained in:
Isaac Connor 2021-08-05 13:30:40 -04:00
parent 8d5207636a
commit 814e8559aa
1 changed files with 23 additions and 12 deletions

View File

@ -352,7 +352,7 @@ if ( !empty($_REQUEST['height']) ) {
if ( $errorText ) { if ( $errorText ) {
ZM\Error($errorText); ZM\Error($errorText);
} else { } else {
header("Content-type: $media_type"); header('Content-type: '.$media_type);
if ( ( $scale==0 || $scale==100 ) && ($width==0) && ($height==0) ) { if ( ( $scale==0 || $scale==100 ) && ($width==0) && ($height==0) ) {
# This is so that Save Image As give a useful filename # This is so that Save Image As give a useful filename
if ( $Event ) { if ( $Event ) {
@ -364,7 +364,7 @@ if ( $errorText ) {
} }
} else { } else {
ZM\Debug("Doing a scaled image: scale($scale) width($width) height($height)"); ZM\Debug("Doing a scaled image: scale($scale) width($width) height($height)");
$i = 0; $i = null;
if ( ! ( $width && $height ) ) { if ( ! ( $width && $height ) ) {
$i = imagecreatefromjpeg($path); $i = imagecreatefromjpeg($path);
$oldWidth = imagesx($i); $oldWidth = imagesx($i);
@ -385,22 +385,33 @@ ZM\Debug("Figuring out height using width: $height = ($width * $oldHeight) / $ol
# Slight optimisation, thumbnails always specify width and height, so we can cache them. # Slight optimisation, thumbnails always specify width and height, so we can cache them.
$scaled_path = preg_replace('/\.jpg$/', "-${width}x${height}.jpg", $path); $scaled_path = preg_replace('/\.jpg$/', "-${width}x${height}.jpg", $path);
if ( $Event ) { if ($Event) {
$filename = $Event->MonitorId().'_'.$Event->Id().'_'.$Frame->FrameId()."-${width}x${height}.jpg"; $filename = $Event->MonitorId().'_'.$Event->Id().'_'.$Frame->FrameId()."-${width}x${height}.jpg";
header('Content-Disposition: inline; filename="' . $filename . '"'); header('Content-Disposition: inline; filename="' . $filename . '"');
} }
if ( !( file_exists($scaled_path) and readfile($scaled_path) ) ) { if ( !( file_exists($scaled_path) and readfile($scaled_path) ) ) {
ZM\Debug("Cached scaled image does not exist at $scaled_path or is no good.. Creating it"); ZM\Debug("Cached scaled image does not exist at $scaled_path or is no good.. Creating it");
ob_start(); if (!$i)
if ( !$i )
$i = imagecreatefromjpeg($path); $i = imagecreatefromjpeg($path);
$iScale = imagescale($i, $width, $height); if ( !$i) {
imagejpeg($iScale); ZM\Error('Unable to load jpeg from '.$scaled_path);
imagedestroy($i); $i = imagecreatetruecolor($width, $height);
imagedestroy($iScale); $bg_colour = imagecolorallocate($i, 255, 255, 255);
$scaled_jpeg_data = ob_get_contents(); $fg_colour = imagecolorallocate($i, 0, 0, 0);
file_put_contents($scaled_path, $scaled_jpeg_data); imagefilledrectangle($im, 0, 0, $width, $height, $bg_colour);
echo $scaled_jpeg_data; imagestring($i, 1, 5, 5, 'Unable to load jpeg from ' . $scaled_path, $fg_colour);
imagejpeg($i);
} else {
ZM\Debug("Have image scaling to $width x $height");
ob_start();
$iScale = imagescale($i, $width, $height);
imagejpeg($iScale);
imagedestroy($i);
imagedestroy($iScale);
$scaled_jpeg_data = ob_get_contents();
file_put_contents($scaled_path, $scaled_jpeg_data);
echo $scaled_jpeg_data;
}
} else { } else {
ZM\Debug("Sending $scaled_path"); ZM\Debug("Sending $scaled_path");
$bytes = readfile($scaled_path); $bytes = readfile($scaled_path);