2010-02-23 17:12:12 +08:00
< ? php
//
// ZoneMinder web image view file, $Date: 2008-09-29 14:15:13 +0100 (Mon, 29 Sep 2008) $, $Revision: 2640 $
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
2016-12-26 23:23:16 +08:00
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2010-02-23 17:12:12 +08:00
//
2015-08-10 00:02:13 +08:00
// Calling sequence: ... /zm/index.php?view=image&path=/monid/path/image.jpg&scale=nnn&width=wwww&height=hhhhh
//
// Path is physical path to the image starting at the monitor id
//
// Scale is optional and between 1 and 400 (percent),
// Omitted or 100 = no scaling done, image passed through directly
// Scaling will increase response time slightly
//
2015-08-10 06:19:12 +08:00
// width and height are each optional, ideally supply both, but if only one is supplied the other is calculated
// These are in pixels
2015-08-10 00:02:13 +08:00
//
2015-08-10 06:19:12 +08:00
// If both scale and either width or height are specified, scale is ignored
2015-08-10 00:02:13 +08:00
//
2018-06-06 23:41:28 +08:00
if ( ! canView ( 'Events' ) ) {
2017-05-19 00:49:59 +08:00
$view = 'error' ;
return ;
2010-02-23 17:12:12 +08:00
}
2016-01-15 02:35:30 +08:00
require_once ( 'includes/Event.php' );
2016-04-26 02:59:55 +08:00
require_once ( 'includes/Frame.php' );
2016-01-15 02:35:30 +08:00
2015-11-16 05:40:25 +08:00
// Compatibility for PHP 5.4
2018-08-31 23:57:47 +08:00
if ( ! function_exists ( 'imagescale' ) ) {
2016-05-17 00:23:19 +08:00
function imagescale ( $image , $new_width , $new_height = - 1 , $mode = 0 ) {
$mode ; // Not supported
2015-11-16 05:40:25 +08:00
2016-05-17 00:23:19 +08:00
$new_height = ( $new_height == - 1 ) ? imagesy ( $image ) : $new_height ;
$imageNew = imagecreatetruecolor ( $new_width , $new_height );
imagecopyresampled ( $imageNew , $image , 0 , 0 , 0 , 0 , ( int ) $new_width , ( int ) $new_height , imagesx ( $image ), imagesy ( $image ));
2016-05-03 01:36:19 +08:00
2016-05-17 00:23:19 +08:00
return $imageNew ;
}
2015-11-16 05:40:25 +08:00
}
2010-02-23 17:12:12 +08:00
$errorText = false ;
2016-11-22 01:28:15 +08:00
$filename = '' ;
2017-01-14 03:42:10 +08:00
$Frame = null ;
$Event = null ;
2017-04-08 01:20:54 +08:00
$path = null ;
2020-03-15 02:24:39 +08:00
$media_type = 'image/jpeg' ;
2016-11-22 01:28:15 +08:00
2016-05-17 00:23:19 +08:00
if ( empty ( $_REQUEST [ 'path' ]) ) {
2017-10-11 03:11:59 +08:00
2018-05-11 21:53:24 +08:00
$show = empty ( $_REQUEST [ 'show' ]) ? 'capture' : $_REQUEST [ 'show' ];
2018-05-11 22:37:00 +08:00
if ( empty ( $_REQUEST [ 'fid' ]) ) {
header ( 'HTTP/1.0 404 Not Found' );
2019-02-22 22:19:07 +08:00
ZM\Fatal ( 'No Frame ID specified' );
2018-05-11 22:37:00 +08:00
return ;
}
if ( ! empty ( $_REQUEST [ 'eid' ]) ) {
2019-02-22 22:19:07 +08:00
$Event = ZM\Event :: find_one ( array ( 'Id' => $_REQUEST [ 'eid' ]));
2018-08-31 23:57:47 +08:00
if ( ! $Event ) {
2018-05-11 22:37:00 +08:00
header ( 'HTTP/1.0 404 Not Found' );
2019-02-22 22:19:07 +08:00
ZM\Fatal ( 'Event ' . $_REQUEST [ 'eid' ] . ' Not found' );
2018-05-11 22:37:00 +08:00
return ;
}
2020-03-11 01:51:55 +08:00
if ( $_REQUEST [ 'fid' ] == 'objdetect' ) {
// if animation file is found, return that, else return image
2020-03-15 02:18:25 +08:00
$path_anim_mp4 = $Event -> Path () . '/objdetect.mp4' ;
$path_anim_gif = $Event -> Path () . '/objdetect.gif' ;
2020-03-11 01:51:55 +08:00
$path_image = $Event -> Path () . '/objdetect.jpg' ;
2020-03-15 02:18:25 +08:00
if ( file_exists ( $path_anim_mp4 )) {
// we found the animation mp4 file
$path = $path_anim_mp4 ;
2020-03-11 01:51:55 +08:00
ZM\Logger :: Debug ( " Animation file found at $path " );
2020-03-15 02:24:39 +08:00
$media_type = 'video/mp4' ;
2020-03-15 02:18:25 +08:00
} else if ( file_exists ( $path_anim_gif )) {
// we found the animation gif file
2020-03-15 02:24:39 +08:00
$media_type = 'image/gif' ;
2020-03-15 02:18:25 +08:00
ZM\Logger :: Debug ( " Animation file found at $path " );
$path = $path_anim_gif ;
2020-03-11 01:51:55 +08:00
} else if ( file_exists ( $path_image )) {
2020-03-14 20:08:52 +08:00
// animation not found, but image found
2020-03-11 01:51:55 +08:00
ZM\Logger :: Debug ( " Image file found at $path " );
$path = $path_image ;
} else {
2020-03-14 20:08:52 +08:00
// neither animation nor image found
2020-03-11 01:51:55 +08:00
header ( 'HTTP/1.0 404 Not Found' );
ZM\Fatal ( " Object detection animation and image not found for this event " );
}
$Frame = new ZM\Frame ();
$Frame -> Id ( 'objdetect' );
2020-03-15 02:18:25 +08:00
} else if ( $_REQUEST [ 'fid' ] == 'objdetect_mp4' ) {
2020-03-14 20:08:52 +08:00
$path = $Event -> Path () . '/objdetect.mp4' ;
2020-03-11 01:51:55 +08:00
if ( ! file_exists ( $path ) ) {
header ( 'HTTP/1.0 404 Not Found' );
2020-03-14 20:08:52 +08:00
ZM\Fatal ( " File $path does not exist. You might not have enabled create_animation in objectconfig.ini. If you have, inspect debug logs for errors during creation " );
2020-03-15 02:18:25 +08:00
}
2020-03-15 02:24:39 +08:00
$Frame = new ZM\Frame ();
$Frame -> Id ( 'objdetect' );
$media_type = 'video/mp4' ;
2020-03-15 02:18:25 +08:00
} else if ( $_REQUEST [ 'fid' ] == 'objdetect_gif' ) {
2020-03-15 02:24:39 +08:00
$path = $Event -> Path () . '/objdetect.gif' ;
2020-03-15 02:18:25 +08:00
if ( ! file_exists ( $path ) ) {
header ( 'HTTP/1.0 404 Not Found' );
ZM\Fatal ( " File $path does not exist. You might not have enabled create_animation in objectconfig.ini. If you have, inspect debug logs for errors during creation " );
}
$Frame = new ZM\Frame ();
$Frame -> Id ( 'objdetect' );
2020-03-15 02:24:39 +08:00
$media_type = 'image/gif' ;
2020-03-15 02:18:25 +08:00
} else if ( $_REQUEST [ 'fid' ] == 'objdetect_jpg' ) {
2019-02-12 05:29:19 +08:00
$path = $Event -> Path () . '/objdetect.jpg' ;
2019-02-12 05:37:22 +08:00
if ( ! file_exists ( $path ) ) {
2019-02-09 02:49:00 +08:00
header ( 'HTTP/1.0 404 Not Found' );
2019-02-22 22:19:07 +08:00
ZM\Fatal ( " File $path does not exist. Please make sure store_frame_in_zm is enabled in the object detection config " );
2019-02-09 02:49:00 +08:00
}
2019-02-22 22:19:07 +08:00
$Frame = new ZM\Frame ();
2019-02-12 05:37:22 +08:00
$Frame -> Id ( 'objdetect' );
2019-02-12 05:29:19 +08:00
} else if ( $_REQUEST [ 'fid' ] == 'alarm' ) {
2019-10-31 04:57:45 +08:00
$path = $Event -> Path () . '/alarm.jpg' ;
if ( ! file_exists ( $path ) ) {
# legacy support
# look for first alarmed frame
$Frame = ZM\Frame :: find_one (
array ( 'EventId' => $_REQUEST [ 'eid' ], 'Type' => 'Alarm' ),
array ( 'order' => 'FrameId ASC' ));
if ( ! $Frame ) { # no alarms, get first one I find
$Frame = ZM\Frame :: find_one ( array ( 'EventId' => $_REQUEST [ 'eid' ]));
if ( ! $Frame ) {
ZM\Warning ( 'No frame found for event ' . $_REQUEST [ 'eid' ]);
$Frame = new ZM\Frame ();
$Frame -> Delta ( 1 );
$Frame -> FrameId ( 1 );
}
}
$Monitor = $Event -> Monitor ();
if ( $Event -> SaveJPEGs () & 1 ) {
# If we store Frames as jpgs, then we don't store an alarmed snapshot
$path = $Event -> Path () . '/' . sprintf ( '%0' . ZM_EVENT_IMAGE_DIGITS . 'd' , $Frame -> FrameId ()) . '-' . $show . '.jpg' ;
} else {
header ( 'HTTP/1.0 404 Not Found' );
ZM\Fatal ( 'No alarm jpg found for event ' . $_REQUEST [ 'eid' ]);
return ;
2018-11-13 01:43:20 +08:00
}
} else {
2019-02-22 22:19:07 +08:00
$Frame = new ZM\Frame ();
2018-05-11 22:37:00 +08:00
$Frame -> Delta ( 1 );
2019-10-31 04:57:45 +08:00
$Frame -> FrameId ( 'alarm' );
} # alarm.jpg found
} else if ( $_REQUEST [ 'fid' ] == 'snapshot' ) {
$path = $Event -> Path () . '/snapshot.jpg' ;
if ( ! file_exists ( $path ) ) {
$Frame = ZM\Frame :: find_one ( array ( 'EventId' => $_REQUEST [ 'eid' ], 'Score' => $Event -> MaxScore ()));
if ( ! $Frame )
$Frame = ZM\Frame :: find_one ( array ( 'EventId' => $_REQUEST [ 'eid' ]));
if ( ! $Frame ) {
ZM\Warning ( 'No frame found for event ' . $_REQUEST [ 'eid' ]);
$Frame = new ZM\Frame ();
$Frame -> Delta ( 1 );
if ( $Event -> SaveJPEGs () & 1 ) {
$Frame -> FrameId ( 0 );
} else {
$Frame -> FrameId ( 'snapshot' );
}
}
$Monitor = $Event -> Monitor ();
2019-08-27 03:04:59 +08:00
if ( $Event -> SaveJPEGs () & 1 ) {
2019-10-31 04:57:45 +08:00
# If we store Frames as jpgs, then we don't store a snapshot
$path = $Event -> Path () . '/' . sprintf ( '%0' . ZM_EVENT_IMAGE_DIGITS . 'd' , $Frame -> FrameId ()) . '-' . $show . '.jpg' ;
2019-04-05 00:18:46 +08:00
} else {
2019-10-31 04:57:45 +08:00
header ( 'HTTP/1.0 404 Not Found' );
ZM\Fatal ( 'No alarm jpg found for event ' . $_REQUEST [ 'eid' ]);
return ;
} # end if stored jpgs
2018-06-06 23:41:28 +08:00
} else {
2019-10-31 04:57:45 +08:00
$Frame = new ZM\Frame ();
$Frame -> Delta ( 1 );
$Frame -> FrameId ( 'snapshot' );
} # end if found snapshot.jpg
2017-11-02 21:00:01 +08:00
} else {
2017-10-11 03:11:59 +08:00
2019-02-22 22:19:07 +08:00
$Frame = ZM\Frame :: find_one ( array ( 'EventId' => $_REQUEST [ 'eid' ], 'FrameId' => $_REQUEST [ 'fid' ]));
2018-05-11 22:37:00 +08:00
if ( ! $Frame ) {
$previousBulkFrame = dbFetchOne (
'SELECT * FROM Frames WHERE EventId=? AND FrameId < ? ORDER BY FrameID DESC LIMIT 1' ,
NULL , array ( $_REQUEST [ 'eid' ], $_REQUEST [ 'fid' ])
);
$nextBulkFrame = dbFetchOne (
'SELECT * FROM Frames WHERE EventId=? AND FrameId > ? ORDER BY FrameID ASC LIMIT 1' ,
NULL , array ( $_REQUEST [ 'eid' ], $_REQUEST [ 'fid' ])
);
if ( $previousBulkFrame and $nextBulkFrame ) {
2019-02-22 22:19:07 +08:00
$Frame = new ZM\Frame ( $previousBulkFrame );
2018-05-11 22:37:00 +08:00
$Frame -> FrameId ( $_REQUEST [ 'fid' ]);
2017-10-11 03:11:59 +08:00
2018-05-11 22:37:00 +08:00
$percentage = ( $Frame -> FrameId () - $previousBulkFrame [ 'FrameId' ]) / ( $nextBulkFrame [ 'FrameId' ] - $previousBulkFrame [ 'FrameId' ]);
2017-10-18 02:53:34 +08:00
2018-05-11 22:37:00 +08:00
$Frame -> Delta ( $previousBulkFrame [ 'Delta' ] + floor ( 100 * ( $nextBulkFrame [ 'Delta' ] - $previousBulkFrame [ 'Delta' ] ) * $percentage ) / 100 );
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " Got virtual frame from Bulk Frames previous delta: " . $previousBulkFrame [ 'Delta' ] . " + nextdelta: " . $nextBulkFrame [ 'Delta' ] . ' - ' . $previousBulkFrame [ 'Delta' ] . ' * ' . $percentage );
2018-05-11 22:37:00 +08:00
} else {
2019-02-22 22:19:07 +08:00
ZM\Fatal ( 'No Frame found for event(' . $_REQUEST [ 'eid' ] . ') and frame id(' . $_REQUEST [ 'fid' ] . ')' );
2018-04-21 02:25:54 +08:00
}
2017-10-11 03:11:59 +08:00
}
2018-05-11 22:37:00 +08:00
// Frame can be non-existent. We have Bulk frames. So now we should try to load the bulk frame
2017-10-11 03:11:59 +08:00
$path = $Event -> Path () . '/' . sprintf ( '%0' . ZM_EVENT_IMAGE_DIGITS . 'd' , $Frame -> FrameId ()) . '-' . $show . '.jpg' ;
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " Path: $path " );
2016-05-12 23:55:48 +08:00
}
2018-05-11 22:37:00 +08:00
2016-05-12 23:55:48 +08:00
} else {
2018-05-11 22:37:00 +08:00
# If we are only specifying fid, then the fid must be the primary key into the frames table. But when the event is specified, then it is the frame #
2019-02-22 22:19:07 +08:00
$Frame = ZM\Frame :: find_one ( array ( 'Id' => $_REQUEST [ 'fid' ]));
2018-08-31 23:57:47 +08:00
if ( ! $Frame ) {
2018-05-11 22:37:00 +08:00
header ( 'HTTP/1.0 404 Not Found' );
2019-02-22 22:19:07 +08:00
ZM\Fatal ( 'Frame ' . $_REQUEST [ 'fid' ] . ' Not Found' );
2018-05-11 22:37:00 +08:00
return ;
}
2016-05-06 02:49:40 +08:00
2019-02-22 22:19:07 +08:00
$Event = ZM\Event :: find_one ( array ( 'Id' => $Frame -> EventId ()));
2018-08-31 23:57:47 +08:00
if ( ! $Event ) {
2018-05-11 22:37:00 +08:00
header ( 'HTTP/1.0 404 Not Found' );
2019-02-22 22:19:07 +08:00
ZM\Fatal ( 'Event ' . $Frame -> EventId () . ' Not Found' );
2018-05-11 22:37:00 +08:00
return ;
}
$path = $Event -> Path () . '/' . sprintf ( '%0' . ZM_EVENT_IMAGE_DIGITS . 'd' , $Frame -> FrameId ()) . '-' . $show . '.jpg' ;
} # end if have eid
2018-08-31 23:57:47 +08:00
if ( ! file_exists ( $path ) ) {
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " $path does not exist " );
2018-05-11 21:53:24 +08:00
# Generate the frame JPG
2018-08-31 23:57:47 +08:00
if ( ( $show == 'capture' ) and $Event -> DefaultVideo () ) {
if ( ! file_exists ( $Event -> Path () . '/' . $Event -> DefaultVideo ()) ) {
2018-03-29 04:01:58 +08:00
header ( 'HTTP/1.0 404 Not Found' );
2019-02-22 22:19:07 +08:00
ZM\Fatal ( " Can't create frame images from video because there is no video file for this event at ( " . $Event -> Path () . '/' . $Event -> DefaultVideo () );
2018-03-29 04:01:58 +08:00
}
2020-02-07 02:22:22 +08:00
$command = ZM_PATH_FFMPEG . ' -ss ' . $Frame -> Delta () . ' -i ' . $Event -> Path () . '/' . $Event -> DefaultVideo () . ' -frames:v 1 ' . $path ;
2017-04-13 04:17:19 +08:00
#$command ='ffmpeg -ss '. $Frame->Delta() .' -i '.$Event->Path().'/'.$Event->DefaultVideo().' -vf "select=gte(n\\,'.$Frame->FrameId().'),setpts=PTS-STARTPTS" '.$path;
2016-05-17 00:23:19 +08:00
#$command ='ffmpeg -v 0 -i '.$Storage->Path().'/'.$Event->Path().'/'.$Event->DefaultVideo().' -vf "select=gte(n\\,'.$Frame->FrameId().'),setpts=PTS-STARTPTS" '.$path;
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " Running $command " );
2016-05-17 00:23:19 +08:00
$output = array ();
$retval = 0 ;
exec ( $command , $output , $retval );
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " Command: $command , retval: $retval , output: " . implode ( " \n " , $output ));
2016-09-21 03:55:28 +08:00
if ( ! file_exists ( $path ) ) {
2017-05-19 00:49:59 +08:00
header ( 'HTTP/1.0 404 Not Found' );
2019-09-29 02:22:40 +08:00
ZM\Fatal ( 'Can\'t create frame images from video for this event ' . $Event -> DefaultVideo () );
2016-09-21 03:55:28 +08:00
}
2018-05-11 22:37:00 +08:00
# Generating an image file will use up more disk space, so update the Event record.
$Event -> DiskSpace ( null );
2017-10-24 08:04:02 +08:00
$Event -> save ();
2016-05-17 00:23:19 +08:00
} else {
2017-05-19 00:49:59 +08:00
header ( 'HTTP/1.0 404 Not Found' );
2019-02-22 22:19:07 +08:00
ZM\Fatal ( " Can't create frame $show images from video because there is no video file for this event at " .
2018-05-11 22:37:00 +08:00
$Event -> Path () . '/' . $Event -> DefaultVideo () );
2010-02-23 17:12:12 +08:00
}
2018-05-11 22:37:00 +08:00
} # end if ! file_exists($path)
2016-05-06 02:49:40 +08:00
2016-05-17 00:23:19 +08:00
} else {
2019-02-22 22:19:07 +08:00
ZM\Warning ( 'Loading images by path is deprecated' );
2017-01-26 06:14:30 +08:00
$dir_events = realpath ( ZM_DIR_EVENTS );
$path = realpath ( $dir_events . '/' . $_REQUEST [ 'path' ]);
$pos = strpos ( $path , $dir_events );
2017-01-25 23:05:34 +08:00
2017-05-19 00:49:59 +08:00
if ( $pos == 0 && $pos !== false ) {
2018-05-11 22:37:00 +08:00
if ( ! empty ( $user [ 'MonitorIds' ]) ) {
2017-01-26 06:14:30 +08:00
$imageOk = false ;
2018-05-11 22:37:00 +08:00
$pathMonId = substr ( $path , 0 , strspn ( $path , '1234567890' ));
foreach ( preg_split ( '/["\'\s]*,["\'\s]*/' , $user [ 'MonitorIds' ]) as $monId ) {
2017-01-26 06:14:30 +08:00
if ( $pathMonId == $monId ) {
$imageOk = true ;
break ;
2010-02-23 17:12:12 +08:00
}
2016-05-17 00:23:19 +08:00
}
2017-01-26 06:14:30 +08:00
if ( ! $imageOk )
2017-05-19 00:49:59 +08:00
$errorText = 'No image permissions' ;
2010-02-23 17:12:12 +08:00
}
2017-01-26 06:14:30 +08:00
} else {
2017-05-19 00:49:59 +08:00
$errorText = 'Invalid image path' ;
2016-05-17 00:23:19 +08:00
}
2018-08-31 23:57:47 +08:00
if ( ! file_exists ( $path ) ) {
2016-10-21 00:01:25 +08:00
header ( 'HTTP/1.0 404 Not Found' );
2019-02-22 22:19:07 +08:00
ZM\Fatal ( " Image not found at $path " );
2016-05-17 00:23:19 +08:00
}
2010-02-23 17:12:12 +08:00
}
2019-02-09 02:49:00 +08:00
# we now load the actual image to send
2018-08-31 23:57:47 +08:00
$scale = 0 ;
2018-05-11 22:37:00 +08:00
if ( ! empty ( $_REQUEST [ 'scale' ]) ) {
if ( is_numeric ( $_REQUEST [ 'scale' ]) ) {
2016-05-17 00:23:19 +08:00
$x = $_REQUEST [ 'scale' ];
2018-05-11 22:37:00 +08:00
if ( $x >= 1 and $x <= 400 )
2018-08-31 23:57:47 +08:00
$scale = $x ;
2016-05-17 00:23:19 +08:00
}
2016-05-03 01:36:19 +08:00
}
2010-02-23 17:12:12 +08:00
2018-08-31 23:57:47 +08:00
$width = 0 ;
2016-05-17 00:23:19 +08:00
if ( ! empty ( $_REQUEST [ 'width' ]) ) {
2018-05-11 22:37:00 +08:00
if ( is_numeric ( $_REQUEST [ 'width' ]) ) {
2016-05-17 00:23:19 +08:00
$x = $_REQUEST [ 'width' ];
2018-05-11 22:37:00 +08:00
if ( $x >= 10 and $x <= 8000 )
2018-08-31 23:57:47 +08:00
$width = $x ;
2016-05-17 00:23:19 +08:00
}
2016-05-03 01:36:19 +08:00
}
2018-10-23 04:13:12 +08:00
2018-08-31 23:57:47 +08:00
$height = 0 ;
2018-05-11 22:37:00 +08:00
if ( ! empty ( $_REQUEST [ 'height' ]) ) {
if ( is_numeric ( $_REQUEST [ 'height' ]) ) {
2016-05-17 00:23:19 +08:00
$x = $_REQUEST [ 'height' ];
2018-05-11 22:37:00 +08:00
if ( $x >= 10 and $x <= 8000 )
2018-08-31 23:57:47 +08:00
$height = $x ;
2016-05-17 00:23:19 +08:00
}
2016-05-03 01:36:19 +08:00
}
2015-08-10 00:02:13 +08:00
2016-05-03 01:36:19 +08:00
if ( $errorText ) {
2019-02-22 22:19:07 +08:00
ZM\Error ( $errorText );
2016-05-03 01:36:19 +08:00
} else {
2020-03-10 03:56:34 +08:00
header ( " Content-type: $image_type " );
2018-10-23 04:13:12 +08:00
if ( ( $scale == 0 || $scale == 100 ) && ( $width == 0 ) && ( $height == 0 ) ) {
2018-08-31 23:57:47 +08:00
# This is so that Save Image As give a useful filename
if ( $Event ) {
$filename = $Event -> MonitorId () . '_' . $Event -> Id () . '_' . $Frame -> FrameId () . '.jpg' ;
header ( 'Content-Disposition: inline; filename="' . $filename . '"' );
}
2018-05-11 22:37:00 +08:00
if ( ! readfile ( $path ) ) {
2019-02-22 22:19:07 +08:00
ZM\Error ( 'No bytes read from ' . $path );
2010-02-23 17:12:12 +08:00
}
2016-05-03 01:36:19 +08:00
} else {
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " Doing a scaled image: scale( $scale ) width( $width ) height( $height ) " );
2016-05-17 00:23:19 +08:00
$i = 0 ;
if ( ! ( $width && $height ) ) {
2018-05-11 22:37:00 +08:00
$i = imagecreatefromjpeg ( $path );
$oldWidth = imagesx ( $i );
$oldHeight = imagesy ( $i );
2016-05-17 00:23:19 +08:00
if ( $width == 0 && $height == 0 ) { // scale has to be set to get here with both zero
$width = $oldWidth * $scale / 100.0 ;
$height = $oldHeight * $scale / 100.0 ;
} elseif ( $width == 0 && $height != 0 ) {
$width = ( $height * $oldWidth ) / $oldHeight ;
} elseif ( $width != 0 && $height == 0 ) {
$height = ( $width * $oldHeight ) / $oldWidth ;
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " Figuring out height using width: $height = ( $width * $oldHeight ) / $oldWidth " );
2016-05-17 00:23:19 +08:00
}
2018-08-31 23:57:47 +08:00
if ( $width == $oldWidth && $height == $oldHeight ) {
2019-02-22 22:19:07 +08:00
ZM\Warning ( 'No change to width despite scaling.' );
2016-05-17 00:23:19 +08:00
}
}
# Slight optimisation, thumbnails always specify width and height, so we can cache them.
2018-08-31 23:57:47 +08:00
$scaled_path = preg_replace ( '/\.jpg$/' , " - ${ width}x${height } .jpg " , $path );
if ( $Event ) {
$filename = $Event -> MonitorId () . '_' . $Event -> Id () . '_' . $Frame -> FrameId () . " - ${ width}x${height } .jpg " ;
header ( 'Content-Disposition: inline; filename="' . $filename . '"' );
}
if ( ! ( file_exists ( $scaled_path ) and readfile ( $scaled_path ) ) ) {
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " Cached scaled image does not exist at $scaled_path or is no good.. Creating it " );
2016-05-17 00:23:19 +08:00
ob_start ();
2018-08-31 23:57:47 +08:00
if ( ! $i )
2018-05-11 22:37:00 +08:00
$i = imagecreatefromjpeg ( $path );
$iScale = imagescale ( $i , $width , $height );
imagejpeg ( $iScale );
imagedestroy ( $i );
imagedestroy ( $iScale );
2016-05-17 00:23:19 +08:00
$scaled_jpeg_data = ob_get_contents ();
2018-05-11 22:37:00 +08:00
file_put_contents ( $scaled_path , $scaled_jpeg_data );
2016-05-17 00:23:19 +08:00
echo $scaled_jpeg_data ;
2018-08-31 23:57:47 +08:00
} else {
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " Sending $scaled_path " );
2018-08-31 23:57:47 +08:00
$bytes = readfile ( $scaled_path );
if ( ! $bytes ) {
2019-02-22 22:19:07 +08:00
ZM\Error ( 'No bytes read from ' . $scaled_path );
2018-08-31 23:57:47 +08:00
} else {
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " $bytes sent " );
2018-08-31 23:57:47 +08:00
}
2016-05-17 00:23:19 +08:00
}
}
2016-05-03 01:36:19 +08:00
}
2018-08-31 23:57:47 +08:00
exit ();