From 5598cce2603522c86da24500bb86ebeafbd426d4 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Tue, 23 Dec 2014 17:42:44 -0500 Subject: [PATCH] Add getImageSrc function to the API via component --- .../Controller/Component/ImageComponent.php | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 web/api/app/Controller/Component/ImageComponent.php diff --git a/web/api/app/Controller/Component/ImageComponent.php b/web/api/app/Controller/Component/ImageComponent.php new file mode 100644 index 000000000..def1f4e46 --- /dev/null +++ b/web/api/app/Controller/Component/ImageComponent.php @@ -0,0 +1,83 @@ += 100 || !function_exists( 'imagecreatefromjpeg' ) ) { + $imagePath = $thumbPath = $isAnalImage?$analPath:$captPath; + $imageFile = $config['ZM_DIR_EVENTS']."/".$imagePath; + $thumbFile = $config['ZM_DIR_EVENTS']."/".$thumbPath; + } else { + if ( version_compare( phpversion(), "4.3.10", ">=") ) + $fraction = sprintf( "%.3F", $scale/100 ); + else + $fraction = sprintf( "%.3f", $scale/100 ); + $scale = (int)round( $scale ); + + $thumbCaptPath = preg_replace( "/\.jpg$/", "-$scale.jpg", $thumbCaptPath ); + $thumbAnalPath = preg_replace( "/\.jpg$/", "-$scale.jpg", $thumbAnalPath ); + + if ( $isAnalImage ) + { + $imagePath = $analPath; + $thumbPath = $thumbAnalPath; + } + else + { + $imagePath = $captPath; + $thumbPath = $thumbCaptPath; + } + + $imageFile = $config['ZM_DIR_EVENTS']."/".$imagePath; + //$thumbFile = ZM_DIR_EVENTS."/".$thumbPath; + $thumbFile = $thumbPath; + if ( $overwrite || !file_exists( $thumbFile ) || !filesize( $thumbFile ) ) + { + // Get new dimensions + list( $imageWidth, $imageHeight ) = getimagesize( $imageFile ); + $thumbWidth = $imageWidth * $fraction; + $thumbHeight = $imageHeight * $fraction; + + // Resample + $thumbImage = imagecreatetruecolor( $thumbWidth, $thumbHeight ); + $image = imagecreatefromjpeg( $imageFile ); + imagecopyresampled( $thumbImage, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imageWidth, $imageHeight ); + + if ( !imagejpeg( $thumbImage, $thumbFile ) ) + Error( "Can't create thumbnail '$thumbPath'" ); + } + } + + $imageData = array( + 'eventPath' => $eventPath, + 'imagePath' => $imagePath, + 'thumbPath' => $thumbPath, + 'imageFile' => $imageFile, + 'thumbFile' => $thumbFile, + 'imageClass' => $alarmFrame?"alarm":"normal", + 'isAnalImage' => $isAnalImage, + 'hasAnalImage' => $hasAnalImage, + ); + + return( $imageData ); + + } +} +?>