Merge branch 'storageareas' of github.com:connortechnology/ZoneMinder into storageareas

This commit is contained in:
Isaac Connor 2018-05-12 19:44:40 -04:00
commit 47ba435471
11 changed files with 193 additions and 185 deletions

View File

@ -53,6 +53,7 @@ use HTTP::Cookies;
my $ChannelID = 1; # Usually...
my $DefaultFocusSpeed = 50; # Should be between 1 and 100
my $DefaultIrisSpeed = 50; # Should be between 1 and 100
my ($user,$pass,$host,$port);
sub open {
my $self = shift;
@ -65,7 +66,6 @@ sub open {
#
# Extract the username/password host/port from ControlAddress
#
my ($user,$pass,$host,$port);
if ( $self->{Monitor}{ControlAddress} =~ /^([^:]+):([^@]+)@(.+)/ ) { # user:pass@host...
$user = $1;
$pass = $2;
@ -126,6 +126,8 @@ sub PutCmd {
Control Device should be set to \"$1\".
Control Device currently set to \"$self->{Monitor}{ControlDevice}\".");
$self->{Monitor}{ControlDevice} = $1;
$self->{UA}->credentials("$host:$port", $self->{Monitor}{ControlDevice}, $user, $pass);
return PutCmd($self,$cmd,$content);
}
}
}

View File

@ -108,9 +108,18 @@ sub zmDbConnect {
, $Config{ZM_DB_PASS}
);
};
Error("Error reconnecting to db: errstr:$DBI::errstr error val:$@") if (! $dbh) or $@;
$dbh->trace(0) if $dbh;
}
if ( !$dbh or $@ ) {
Error("Error reconnecting to db: errstr:$DBI::errstr error val:$@");
} else {
$dbh->{AutoCommit} = 1;
Fatal('Can\'t set AutoCommit on in database connection')
unless $dbh->{AutoCommit};
$dbh->{mysql_auto_reconnect} = 1;
Fatal('Can\'t set mysql_auto_reconnect on in database connection')
unless $dbh->{mysql_auto_reconnect};
$dbh->trace( 0 );
} # end if success connecting
} # end if ! connected
return $dbh;
} # end sub zmDbConnect

View File

@ -429,54 +429,10 @@ sub databaseLevel {
if ( $this->{databaseLevel} != $databaseLevel ) {
if ( $databaseLevel > NOLOG and $this->{databaseLevel} <= NOLOG ) {
if ( !$this->{dbh} ) {
my $socket;
my ( $host, $portOrSocket ) = ( $Config{ZM_DB_HOST} =~ /^([^:]+)(?::(.+))?$/ );
if ( defined($portOrSocket) ) {
if ( $portOrSocket =~ /^\// ) {
$socket = ';mysql_socket='.$portOrSocket;
} else {
$socket = ';host='.$host.';port='.$portOrSocket;
}
} else {
$socket = ';host='.$Config{ZM_DB_HOST};
}
my $sslOptions = '';
if ( $Config{ZM_DB_SSL_CA_CERT} ) {
$sslOptions = join(';','',
'mysql_ssl=1',
'mysql_ssl_ca_file='.$Config{ZM_DB_SSL_CA_CERT},
'mysql_ssl_client_key='.$Config{ZM_DB_SSL_CLIENT_KEY},
'mysql_ssl_client_cert='.$Config{ZM_DB_SSL_CLIENT_CERT}
);
}
$this->{dbh} = DBI->connect( 'DBI:mysql:database='.$Config{ZM_DB_NAME}
.$socket.$sslOptions
, $Config{ZM_DB_USER}
, $Config{ZM_DB_PASS}
);
if ( !$this->{dbh} ) {
$databaseLevel = NOLOG;
Error( 'Unable to write log entries to DB, can\'t connect to database '
.$Config{ZM_DB_NAME}
.' on host '
.$Config{ZM_DB_HOST}
);
} else {
$this->{dbh}->{AutoCommit} = 1;
Fatal('Can\'t set AutoCommit on in database connection' )
unless( $this->{dbh}->{AutoCommit} );
$this->{dbh}->{mysql_auto_reconnect} = 1;
Fatal('Can\'t set mysql_auto_reconnect on in database connection' )
unless( $this->{dbh}->{mysql_auto_reconnect} );
$this->{dbh}->trace( 0 );
}
$this->{dbh} = ZoneMinder::Database::zmDbConnect();
}
} elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG ) {
if ( $this->{dbh} ) {
$this->{dbh}->disconnect();
undef($this->{dbh});
}
undef($this->{dbh});
}
$this->{databaseLevel} = $databaseLevel;
}
@ -586,7 +542,7 @@ sub logPrint {
print(STDERR $message) if $level <= $this->{termLevel};
if ( $level <= $this->{databaseLevel} ) {
if ( ( $this->{dbh} and $this->{dbh}->ping() ) or ( $this->{dbh} = zmDbConnect() ) ) {
if ( ( $this->{dbh} and $this->{dbh}->ping() ) or ( $this->{dbh} = ZoneMinder::Database::zmDbConnect() ) ) {
my $sql = 'INSERT INTO Logs ( TimeKey, Component, Pid, Level, Code, Message, File, Line ) VALUES ( ?, ?, ?, ?, ?, ?, ?, NULL )';
$this->{sth} = $this->{dbh}->prepare_cached($sql);

View File

@ -453,7 +453,7 @@ sub start {
$cmd_hash{$process->{command}} = $pid_hash{$cpid} = $process;
sigprocmask(SIG_SETMASK, $sigset) or Fatal("Can't restore SIGCHLD: $!");
Debug("unblocko child");
Debug("unblocking child");
} elsif ( defined($cpid) ) {
# Force reconnection to the db.
$dbh = zmDbConnect(1);

View File

@ -24,7 +24,6 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
Group::Group() {
Warning("Instantiating default Group Object. Should not happen.");
@ -33,30 +32,31 @@ Group::Group() {
strcpy(name, "Default");
}
Group::Group( MYSQL_ROW &dbrow ) {
// The order of columns is: Id, ParentId, Name
Group::Group(MYSQL_ROW &dbrow) {
unsigned int index = 0;
id = atoi( dbrow[index++] );
parent_id = dbrow[index] ? atoi( dbrow[index] ): 0; index++;
strncpy( name, dbrow[index++], sizeof(name)-1 );
id = atoi(dbrow[index++]);
parent_id = dbrow[index] ? atoi(dbrow[index]): 0; index++;
strncpy(name, dbrow[index++], sizeof(name)-1);
}
/* If a zero or invalid p_id is passed, then the old default path will be assumed. */
Group::Group( unsigned int p_id ) {
Group::Group(unsigned int p_id) {
id = 0;
if ( p_id ) {
char sql[ZM_SQL_SML_BUFSIZ];
snprintf( sql, sizeof(sql), "SELECT Id, ParentId, Name FROM Group WHERE Id=%d", p_id );
Debug(2,"Loading Group for %d using %s", p_id, sql );
snprintf(sql, sizeof(sql), "SELECT Id, ParentId, Name FROM Group WHERE Id=%d", p_id);
Debug(2,"Loading Group for %d using %s", p_id, sql);
zmDbRow dbrow;
if ( ! dbrow.fetch( sql ) ) {
Error( "Unable to load group for id %d: %s", p_id, mysql_error( &dbconn ) );
if ( !dbrow.fetch(sql) ) {
Error("Unable to load group for id %d: %s", p_id, mysql_error(&dbconn));
} else {
unsigned int index = 0;
id = atoi( dbrow[index++] );
parent_id = dbrow[index] ? atoi( dbrow[index] ): 0; index++;
strncpy( name, dbrow[index++], sizeof(name)-1 );
Debug( 1, "Loaded Group area %d '%s'", id, this->Name() );
id = atoi(dbrow[index++]);
parent_id = dbrow[index] ? atoi(dbrow[index]): 0; index++;
strncpy(name, dbrow[index++], sizeof(name)-1);
Debug(1, "Loaded Group area %d '%s'", id, this->Name());
}
}
if ( ! id ) {

View File

@ -1543,7 +1543,7 @@ bool Monitor::Analyse() {
}
if ( score ) {
if ( (state == IDLE || state == TAPE || state == PREALARM ) ) {
if ( Event::PreAlarmCount() >= (alarm_frame_count-1) ) {
if ( Event::PreAlarmCount() > alarm_frame_count ) {
Info( "%s: %03d - Gone into alarm state", name, image_count );
shared_data->state = state = ALARM;
if ( signal_change || (function != MOCORD && state != ALERT) ) {
@ -2799,19 +2799,21 @@ Monitor::Snapshot *Monitor::getSnapshot() const {
return &image_buffer[ shared_data->last_write_index%image_buffer_count ];
}
std::list<Group *> Monitor::Groups() {
std::vector<Group *> Monitor::Groups() {
// At the moment, only load groups once.
if ( ! groups.size() ) {
std::string sql = stringtf("SELECT GroupId FROM Groups_Monitors WHERE MonitorId=%d",id);
std::string sql = stringtf(
"SELECT Id,ParentId,Name FROM Groups WHERE Groups.Id IN "
"(SELECT GroupId FROM Groups_Monitors WHERE MonitorId=%d)",id);
MYSQL_RES *result = zmDbFetch(sql.c_str());
if ( !result ) {
Error("Can't load groups: %s", mysql_error(&dbconn));
return groups;
}
int n_groups = mysql_num_rows(result);
Debug( 1, "Got %d groups", n_groups );
Debug(1, "Got %d groups", n_groups);
while ( MYSQL_ROW dbrow = mysql_fetch_row(result) ) {
groups.push_back( new Group(dbrow) );
groups.push_back(new Group(dbrow));
}
if ( mysql_errno(&dbconn) ) {
Error("Can't fetch row: %s", mysql_error(&dbconn));
@ -2820,3 +2822,13 @@ std::list<Group *> Monitor::Groups() {
}
return groups;
}
StringVector Monitor::GroupNames() {
StringVector groupnames;
for(Group * g: Groups()) {
groupnames.push_back(std::string(g->Name()));
Debug(1,"Groups: %s", g->Name());
}
return groupnames;
}

View File

@ -344,7 +344,7 @@ protected:
int n_linked_monitors;
MonitorLink **linked_monitors;
std::list<Group *> groups;
std::vector<Group *> groups;
public:
explicit Monitor( int p_id );
@ -506,7 +506,8 @@ public:
bool DumpSettings( char *output, bool verbose );
void DumpZoneImage( const char *zone_string=0 );
std::list<Group *> Groups();
std::vector<Group *> Groups();
StringVector GroupNames();
static int LoadMonitors(std::string sql, Monitor **&monitors, Purpose purpose); // Returns # of Monitors loaded, 0 on failure.
#if ZM_HAS_V4L

View File

@ -94,26 +94,22 @@ const std::string stringtf( const std::string format, ... )
return( tempString );
}
bool startsWith( const std::string &haystack, const std::string &needle )
{
return( haystack.substr( 0, needle.length() ) == needle );
bool startsWith(const std::string &haystack, const std::string &needle) {
return( haystack.substr(0, needle.length()) == needle );
}
StringVector split( const std::string &string, const std::string &chars, int limit )
{
StringVector split(const std::string &string, const std::string &chars, int limit) {
StringVector stringVector;
std::string tempString = string;
std::string::size_type startIndex = 0;
std::string::size_type endIndex = 0;
//Info( "Looking for '%s' in '%s', limit %d", chars.c_str(), string.c_str(), limit );
do
{
do {
// Find delimiters
endIndex = string.find_first_of( chars, startIndex );
//Info( "Got endIndex at %d", endIndex );
if ( endIndex > 0 )
{
if ( endIndex > 0 ) {
//Info( "Adding '%s'", string.substr( startIndex, endIndex-startIndex ).c_str() );
stringVector.push_back( string.substr( startIndex, endIndex-startIndex ) );
}
@ -121,8 +117,7 @@ StringVector split( const std::string &string, const std::string &chars, int lim
break;
// Find non-delimiters
startIndex = tempString.find_first_not_of( chars, endIndex );
if ( limit && (stringVector.size() == (unsigned int)(limit-1)) )
{
if ( limit && (stringVector.size() == (unsigned int)(limit-1)) ) {
stringVector.push_back( string.substr( startIndex ) );
break;
}
@ -130,22 +125,21 @@ StringVector split( const std::string &string, const std::string &chars, int lim
} while ( startIndex != std::string::npos );
//Info( "Finished with %d strings", stringVector.size() );
return( stringVector );
return stringVector;
}
const std::string join(const StringVector &v, const char * delim ) {
const std::string join(const StringVector &v, const char * delim=",") {
std::stringstream ss;
for(size_t i = 0; i < v.size(); ++i) {
if(i != 0)
ss << ",";
for (size_t i = 0; i < v.size(); ++i) {
if ( i != 0 )
ss << delim;
ss << v[i];
}
return ss.str();
}
const std::string base64Encode( const std::string &inString )
{
const std::string base64Encode(const std::string &inString) {
static char base64_table[64] = { '\0' };
if ( !base64_table[0] )

@ -1 +1 @@
Subproject commit ca91b87fda8e006e4fca2ed870f24f9a29c2905d
Subproject commit ea90c0cd7f6e24333a90885e563b5d30b793db29

View File

@ -10,9 +10,31 @@ class Event {
'MonitorId',
'StorageId',
'Name',
'DiskSpace',
'Cause',
'StartTime',
'EndTime',
'Width',
'Height',
'Length',
'Frames',
'AlarmFrames',
'DefaultVideo',
'SaveJPEGs',
'TotScore',
'AvgScore',
'MaxScore',
'Archived',
'Videoed',
'Uploaded',
'Emailed',
'Messages',
'Executed',
'Notes',
'StateId',
'Orientation',
'DiskSpace',
'Scheme',
'Locked',
);
public function __construct( $IdOrRow = null ) {
$row = NULL;

View File

@ -60,80 +60,90 @@ $path = null;
if ( empty($_REQUEST['path']) ) {
if ( ! empty($_REQUEST['fid']) ) {
if ( $_REQUEST['fid'] == 'snapshot' ) {
$Event = new Event( $_REQUEST['eid'] );
$Frame = new Frame();
$Frame->FrameId('snapshot');
$path = $Event->Path().'/snapshot.jpg';
} else {
$show = empty($_REQUEST['show']) ? 'capture' : $_REQUEST['show'];
$show = empty($_REQUEST['show']) ? 'capture' : $_REQUEST['show'];
if ( ! empty($_REQUEST['eid'] ) ) {
$Event = Event::find_one(array('Id'=> $_REQUEST['eid']));
if ( ! $Event ) {
header('HTTP/1.0 404 Not Found');
Fatal('Event ' . $_REQUEST['eid'].' Not found');
return;
}
$Frame = Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'FrameId'=>$_REQUEST['fid']));
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 ) {
$Frame = new Frame( $previousBulkFrame );
$Frame->FrameId( $_REQUEST['fid'] );
$percentage = ($Frame->FrameId() - $previousBulkFrame['FrameId']) / ($nextBulkFrame['FrameId'] - $previousBulkFrame['FrameId']);
$Frame->Delta( $previousBulkFrame['Delta'] + floor( 100* ( $nextBulkFrame['Delta'] - $previousBulkFrame['Delta'] ) * $percentage )/100 );
Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousBulkFrame['Delta'] . " + nextdelta:" . $nextBulkFrame['Delta'] . ' - ' . $previousBulkFrame['Delta'] . ' * ' . $percentage );
} else {
Fatal("No Frame found for event(".$_REQUEST['eid'].") and frame id(".$_REQUEST['fid'].")");
}
}
// Frame can be non-existent. We have Bulk frames. So now we should try to load the bulk frame
} else {
# 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 #
$Frame = Frame::find_one(array('Id'=>$_REQUEST['fid']));
if ( ! $Frame ) {
header('HTTP/1.0 404 Not Found');
Fatal('Frame ' . $_REQUEST['fid'] . ' Not Found');
return;
}
$Event = Event::find_one(array('Id'=>$Frame->EventId()));
if ( ! $Event ) {
header('HTTP/1.0 404 Not Found');
Fatal('Event ' . $Frame->EventId() . ' Not Found');
return;
}
}
$path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d',$Frame->FrameId()).'-'.$show.'.jpg';
}
} else {
if ( empty($_REQUEST['fid']) ) {
header('HTTP/1.0 404 Not Found');
Fatal('No Frame ID specified');
return;
}
if ( ! file_exists( $path ) ) {
Logger::Debug( "$path does not exist");
# Generate the frame JPG
if ( !empty($_REQUEST['eid']) ) {
$Event = Event::find_one(array('Id'=>$_REQUEST['eid']));
if ( ! $Event ) {
header('HTTP/1.0 404 Not Found');
Fatal('Event ' . $_REQUEST['eid'].' Not found');
return;
}
if ( $_REQUEST['fid'] == 'snapshot' ) {
$Frame = Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'Score'=>$Event->MaxScore()));
if ( ! $Frame )
$Frame = Frame::find_one(array('EventId'=>$_REQUEST['eid']));
if ( ! $Frame ) {
$Frame = new Frame();
$Frame->Delta(1);
$Frame->FrameId('snapshot');
}
$path = $Event->Path().'/snapshot.jpg';
} else {
$Frame = Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'FrameId'=>$_REQUEST['fid']));
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 ) {
$Frame = new Frame($previousBulkFrame);
$Frame->FrameId($_REQUEST['fid']);
$percentage = ($Frame->FrameId() - $previousBulkFrame['FrameId']) / ($nextBulkFrame['FrameId'] - $previousBulkFrame['FrameId']);
$Frame->Delta($previousBulkFrame['Delta'] + floor( 100* ( $nextBulkFrame['Delta'] - $previousBulkFrame['Delta'] ) * $percentage )/100);
Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousBulkFrame['Delta'] . " + nextdelta:" . $nextBulkFrame['Delta'] . ' - ' . $previousBulkFrame['Delta'] . ' * ' . $percentage );
} else {
Fatal('No Frame found for event('.$_REQUEST['eid'].') and frame id('.$_REQUEST['fid'].')');
}
}
// Frame can be non-existent. We have Bulk frames. So now we should try to load the bulk frame
$path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d',$Frame->FrameId()).'-'.$show.'.jpg';
}
} else {
# 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 #
$Frame = Frame::find_one(array('Id'=>$_REQUEST['fid']));
if ( ! $Frame ) {
header('HTTP/1.0 404 Not Found');
Fatal('Frame ' . $_REQUEST['fid'] . ' Not Found');
return;
}
$Event = Event::find_one(array('Id'=>$Frame->EventId()));
if ( ! $Event ) {
header('HTTP/1.0 404 Not Found');
Fatal('Event ' . $Frame->EventId() . ' Not Found');
return;
}
$path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d',$Frame->FrameId()).'-'.$show.'.jpg';
} # end if have eid
if ( ! file_exists($path) ) {
Logger::Debug("$path does not exist");
# Generate the frame JPG
if ( $show == 'capture' and $Event->DefaultVideo() ) {
if ( ! file_exists($Event->Path().'/'.$Event->DefaultVideo()) ) {
header('HTTP/1.0 404 Not Found');
Fatal("Can't create frame images from video becuase there is no video file for this event at (".$Event->Path().'/'.$Event->DefaultVideo() );
Fatal("Can't create frame images from video because there is no video file for this event at (".$Event->Path().'/'.$Event->DefaultVideo() );
}
$command ='ffmpeg -ss '. $Frame->Delta() .' -i '.$Event->Path().'/'.$Event->DefaultVideo().' -frames:v 1 '.$path;
#$command ='ffmpeg -ss '. $Frame->Delta() .' -i '.$Event->Path().'/'.$Event->DefaultVideo().' -vf "select=gte(n\\,'.$Frame->FrameId().'),setpts=PTS-STARTPTS" '.$path;
#$command ='ffmpeg -v 0 -i '.$Storage->Path().'/'.$Event->Path().'/'.$Event->DefaultVideo().' -vf "select=gte(n\\,'.$Frame->FrameId().'),setpts=PTS-STARTPTS" '.$path;
Logger::Debug( "Running $command" );
Logger::Debug("Running $command");
$output = array();
$retval = 0;
exec( $command, $output, $retval );
@ -142,13 +152,15 @@ Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousB
header('HTTP/1.0 404 Not Found');
Fatal("Can't create frame images from video for this event (".$Event->DefaultVideo() );
}
$Event->DiskSpace( null );
# Generating an image file will use up more disk space, so update the Event record.
$Event->DiskSpace(null);
$Event->save();
} else {
header('HTTP/1.0 404 Not Found');
Fatal("Can't create frame images from video becuase there is no video file for this event (".$Event->DefaultVideo() );
Fatal("Can't create frame $show images from video because there is no video file for this event at ".
$Event->Path().'/'.$Event->DefaultVideo() );
}
}
} # end if ! file_exists($path)
} else {
Warning('Loading images by path is deprecated');
@ -157,10 +169,10 @@ Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousB
$pos = strpos($path, $dir_events);
if ( $pos == 0 && $pos !== false ) {
if ( ! empty( $user['MonitorIds'] ) ) {
if ( ! empty($user['MonitorIds']) ) {
$imageOk = false;
$pathMonId = substr( $path, 0, strspn( $path, '1234567890' ) );
foreach ( preg_split( '/["\'\s]*,["\'\s]*/', $user['MonitorIds'] ) as $monId ) {
$pathMonId = substr($path, 0, strspn($path, '1234567890'));
foreach ( preg_split('/["\'\s]*,["\'\s]*/', $user['MonitorIds']) as $monId ) {
if ( $pathMonId == $monId ) {
$imageOk = true;
break;
@ -172,39 +184,39 @@ Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousB
} else {
$errorText = 'Invalid image path';
}
if ( ! file_exists( $path ) ) {
if ( ! file_exists($path) ) {
header('HTTP/1.0 404 Not Found');
Fatal("Image not found at $path");
}
}
$scale=0;
if( !empty($_REQUEST['scale']) ) {
if (is_numeric($_REQUEST['scale'])) {
if ( !empty($_REQUEST['scale']) ) {
if ( is_numeric($_REQUEST['scale']) ) {
$x = $_REQUEST['scale'];
if($x >= 1 and $x <= 400)
if ( $x >= 1 and $x <= 400 )
$scale=$x;
}
}
$width=0;
if ( !empty($_REQUEST['width']) ) {
if (is_numeric($_REQUEST['width'])) {
if ( is_numeric($_REQUEST['width']) ) {
$x = $_REQUEST['width'];
if($x >= 10 and $x <= 8000)
if ( $x >= 10 and $x <= 8000 )
$width=$x;
}
}
$height=0;
if( !empty($_REQUEST['height']) ) {
if (is_numeric($_REQUEST['height'])) {
if ( !empty($_REQUEST['height']) ) {
if ( is_numeric($_REQUEST['height']) ) {
$x = $_REQUEST['height'];
if($x >= 10 and $x <= 8000)
if ( $x >= 10 and $x <= 8000 )
$height=$x;
}
}
header( 'Content-type: image/jpeg' );
header('Content-type: image/jpeg');
# This is so that Save Image As give a useful filename
if ( $Event ) {
@ -215,19 +227,19 @@ ob_clean();
flush();
if ( $errorText ) {
Error( $errorText );
Error($errorText);
} else {
if ( ( $scale==0 || $scale==100 ) && $width==0 && $height==0 ) {
if ( ! readfile( $path ) ) {
Error("No bytes read from ". $path );
if ( !readfile($path) ) {
Error('No bytes read from '. $path);
}
} else {
Logger::Debug("Doing a scaled image: scale($scale) width($width) height($height)");
$i = 0;
if ( ! ( $width && $height ) ) {
$i = imagecreatefromjpeg( $path );
$oldWidth = imagesx( $i );
$oldHeight = imagesy( $i );
$i = imagecreatefromjpeg($path);
$oldWidth = imagesx($i);
$oldHeight = imagesy($i);
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;
@ -237,23 +249,23 @@ if ( $errorText ) {
$height = ($width * $oldHeight) / $oldWidth;
}
if ( $width == $oldWidth && $height == $oldHeight) {
Warning( 'No change to width despite scaling.' );
Warning('No change to width despite scaling.');
}
}
# Slight optimisation, thumbnails always specify width and height, so we can cache them.
$scaled_path = preg_replace('/\.jpg$/', "-${width}x${height}.jpg", $path );
if ( ! file_exists( $scaled_path ) or ! readfile( $scaled_path ) ) {
Logger::Debug( "Cached scaled image does not exist at $scaled_path or is no good.. Creating it");
if ( ! file_exists($scaled_path) or ! readfile($scaled_path) ) {
Logger::Debug("Cached scaled image does not exist at $scaled_path or is no good.. Creating it");
ob_start();
if ( ! $i )
$i = imagecreatefromjpeg( $path );
$iScale = imagescale( $i, $width, $height );
imagejpeg( $iScale );
imagedestroy( $i );
imagedestroy( $iScale );
$i = imagecreatefromjpeg($path);
$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 );
file_put_contents($scaled_path, $scaled_jpeg_data);
ob_end_clean();
echo $scaled_jpeg_data;
}