From b8c8ff38070d79f6733bc7a4c642157d8f3e59c6 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 2 May 2016 13:36:19 -0400 Subject: [PATCH] fix if structuring --- web/views/image.php | 164 +++++++++++++++++++++----------------------- 1 file changed, 79 insertions(+), 85 deletions(-) diff --git a/web/views/image.php b/web/views/image.php index 8fe5ab558..14891b4c7 100644 --- a/web/views/image.php +++ b/web/views/image.php @@ -47,16 +47,16 @@ header( 'Content-type: image/jpeg' ); // Compatibility for PHP 5.4 if (!function_exists('imagescale')) { - function imagescale($image, $new_width, $new_height = -1, $mode = 0) - { - $mode; // Not supported + function imagescale($image, $new_width, $new_height = -1, $mode = 0) + { + $mode; // Not supported - $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)); - - return $imageNew; - } + $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)); + + return $imageNew; + } } $Storage = NULL; @@ -68,10 +68,10 @@ if ( empty($_REQUEST['path']) ) $Event = new Event( $_REQUEST['eid'] ); $Storage = $Event->Storage(); $path = $Event->Relative_Path().'/'.sprintf("%'.0".ZM_EVENT_IMAGE_DIGITS.'d',$_REQUEST['fid']).'-capture.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 = new Frame( $_REQUEST['fid'] ); - $Event = new Event( $Frame->EventId() ); + } 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 = new Frame( $_REQUEST['fid'] ); + $Event = new Event( $Frame->EventId() ); $Storage = $Event->Storage(); $path = $Event->Relative_Path().'/'.sprintf("%'.0".ZM_EVENT_IMAGE_DIGITS.'d',$Frame->FrameId()).'-capture.jpg'; } @@ -81,88 +81,82 @@ if ( empty($_REQUEST['path']) ) } else { - $Storage = new Storage(); - $path = $_REQUEST['path']; - if ( !empty($user['MonitorIds']) ) - { - $imageOk = false; - $pathMonId = substr( $path, 0, strspn( $path, "1234567890" ) ); - foreach ( preg_split( '/["\'\s]*,["\'\s]*/', $user['MonitorIds'] ) as $monId ) - { - if ( $pathMonId == $monId ) - { - $imageOk = true; - break; - } - } - if ( !$imageOk ) - $errorText = "No image permissions"; - } + $Storage = new Storage(); + $path = $_REQUEST['path']; + if ( !empty($user['MonitorIds']) ) + { + $imageOk = false; + $pathMonId = substr( $path, 0, strspn( $path, "1234567890" ) ); + foreach ( preg_split( '/["\'\s]*,["\'\s]*/', $user['MonitorIds'] ) as $monId ) + { + if ( $pathMonId == $monId ) + { + $imageOk = true; + break; + } + } + if ( !$imageOk ) + $errorText = "No image permissions"; + } } $scale=0; if( !empty($_REQUEST['scale']) ) - if (is_numeric($_REQUEST['scale'])) - { - $x = $_REQUEST['scale']; - if($x >= 1 and $x <= 400) - $scale=$x; - } +if (is_numeric($_REQUEST['scale'])) +{ + $x = $_REQUEST['scale']; + if($x >= 1 and $x <= 400) + $scale=$x; +} $width=0; if( !empty($_REQUEST['width']) ) - if (is_numeric($_REQUEST['width'])) - { - $x = $_REQUEST['width']; - if($x >= 10 and $x <= 8000) - $width=$x; - } +if (is_numeric($_REQUEST['width'])) +{ + $x = $_REQUEST['width']; + if($x >= 10 and $x <= 8000) + $width=$x; +} $height=0; if( !empty($_REQUEST['height']) ) - if (is_numeric($_REQUEST['height'])) - { - $x = $_REQUEST['height']; - if($x >= 10 and $x <= 8000) - $height=$x; - } +if (is_numeric($_REQUEST['height'])) +{ + $x = $_REQUEST['height']; + if($x >= 10 and $x <= 8000) + $height=$x; +} -if ( $errorText ) - Error( $errorText ); -else - if( ($scale==0 || $scale==100) && $width==0 && $height==0 ) - if ( ! readfile( $Storage->Path().'/'.$path ) ) { - Error("No bytes read from ". $Storage->Path() . '/'.$path ); +if ( $errorText ) { + Error( $errorText ); +} else { + if ( ( $scale==0 || $scale==100 ) && $width==0 && $height==0 ) { + if ( ! readfile( $Storage->Path().'/'.$path ) ) { + Error("No bytes read from ". $Storage->Path() . '/'.$path ); + } + } else { + Error("Doing a scaled image: scale($scale) width($width) height($height)"); + $i = imagecreatefromjpeg ( $Storage->Path().'/'.$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; + } elseif ($width==0 && $height!=0) { + $width = ($height * $oldWidth) / $oldHeight; + } elseif ($width!=0 && $height==0) { + $height = ($width * $oldHeight) / $oldWidth; + } + if($width==$oldWidth && $height==$oldHeight) {// See if we really need to scale + imagejpeg($i); + imagedestroy($i); + } else { // we do need to scale + $iScale = imagescale($i, $width, $height); + imagejpeg($iScale); + imagedestroy($i); + imagedestroy($iScale); + } } - else - { - $i = imagecreatefromjpeg ( $Storage->Path().'/'.$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; - } - elseif ($width==0 && $height!=0) - { - $width = ($height * $oldWidth) / $oldHeight; - } - elseif ($width!=0 && $height==0) - { - $height = ($width * $oldHeight) / $oldWidth; - } - if($width==$oldWidth && $height==$oldHeight) // See if we really need to scale - { - imagejpeg($i); - imagedestroy($i); - } - else // we do need to scale - { - $iScale = imagescale($i, $width, $height); - imagejpeg($iScale); - imagedestroy($i); - imagedestroy($iScale); - } - } +} ?>