From 2f34a0f15375fb09c577dfb334989affbc36f31c Mon Sep 17 00:00:00 2001 From: shish Date: Mon, 26 May 2008 21:56:06 +0000 Subject: [PATCH] flash file dimentions (uncompressed files only) git-svn-id: file:///home/shish/svn/shimmie2/trunk@882 7f39781d-f577-437e-ae19-be835c7a54ca --- contrib/handle_flash/main.php | 69 ++++++++++++++++++++++++++++++---- contrib/handle_flash/theme.php | 7 +++- 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/contrib/handle_flash/main.php b/contrib/handle_flash/main.php index dc015ddf..30e98d7e 100644 --- a/contrib/handle_flash/main.php +++ b/contrib/handle_flash/main.php @@ -21,7 +21,8 @@ class FlashFileHandler extends Extension { send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); $image = $this->create_image_from_data("images/$ha/$hash", $event->metadata); if(is_null($image)) { - $event->veto("Flash Handler failed to create image object from data"); + $event->veto("Flash Handler failed to create image object from data. ". + "Note: compressed flash files are currently unsupported"); return; } send_event(new ImageAdditionEvent($event->user, $image)); @@ -49,10 +50,6 @@ class FlashFileHandler extends Extension { $image = new Image(); - // FIXME: need more flash format specs :| - $image->width = 0; - $image->height = 0; - $image->filesize = $metadata['size']; $image->hash = $metadata['hash']; $image->filename = $metadata['filename']; @@ -60,12 +57,70 @@ class FlashFileHandler extends Extension { $image->tag_array = tag_explode($metadata['tags']); $image->source = $metadata['source']; + $rect = $this->swf_get_bounds($filename); + if(is_null($rect)) { + return $null; + } + $image->width = $rect[1]; + $image->height = $rect[3]; + return $image; } + private function str_to_binarray($string) { + $binary = array(); + for($j=0; $j=0; $i--) { + $binary[] = ($c >> $i) & 0x01; + } + } + return $binary; + } + + private function binarray_to_int($binarray, $start=0, $length=32) { + $int = 0; + for($i=$start; $i<$start + $length; $i++) { + $int = $int << 1; + $int = $int + ($binarray[$i] == "1" ? 1 : 0); + } + return $int; + } + + private function swf_get_bounds($filename) { + $fp = fopen($filename, "r"); + $head = fread($fp, 3); + $version = fread($fp, 1); + $length = fread($fp, 4); + + if($head == "FWS") { + $data = fread($fp, 16); + } + else if($head == "CWS") { + // inflate data + return null; + } + + $bounds = array(); + $rect_bin = $this->str_to_binarray($data); + $nbits = $this->binarray_to_int($rect_bin, 0, 5); + $bounds[] = $this->binarray_to_int($rect_bin, 5 + 0 * $nbits, $nbits) / 20; + $bounds[] = $this->binarray_to_int($rect_bin, 5 + 1 * $nbits, $nbits) / 20; + $bounds[] = $this->binarray_to_int($rect_bin, 5 + 2 * $nbits, $nbits) / 20; + $bounds[] = $this->binarray_to_int($rect_bin, 5 + 3 * $nbits, $nbits) / 20; + + return $bounds; + } + private function check_contents($file) { - // FIXME: flash magic header? - return (file_exists($file)); + if(!file_exists($file)) return false; + + $fp = fopen($file, "r"); + $head = fread($fp, 3); + fclose($fp); + if(!array_contains(array("CWS", "FWS"), $head)) return false; + + return true; } } add_event_listener(new FlashFileHandler()); diff --git a/contrib/handle_flash/theme.php b/contrib/handle_flash/theme.php index 62c238cd..b867d8d6 100644 --- a/contrib/handle_flash/theme.php +++ b/contrib/handle_flash/theme.php @@ -6,11 +6,16 @@ class FlashFileHandlerTheme extends Themelet { // FIXME: object and embed have "height" and "width" $html = " + codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' + height='{$image->height}' + width='{$image->width}' + > "; $page->add_block(new Block("Image", $html, "main", 0));