예제 #1
0
 public function convertImageData()
 {
     $format = (int) $this->getField('BitmapFormat');
     $width = (int) $this->getField('BitmapWidth');
     $height = (int) $this->getField('BitmapHeight');
     $bitmapdata = gzuncompress($this->getField('ZlibBitmapData'));
     $bitmapReader = new Media_SWF_Parser();
     $bitmapReader->input($bitmapdata);
     $im = imagecreatetruecolor($width, $height);
     if ($im === false) {
         throw new Exception('Cannot Initialize new GD image stream.');
     }
     $color_palette = array();
     if ($format == 3) {
         // color palette
         $palette_size = (int) $this->getField('BitmapColorTableSize');
         if ($this->code == 20) {
             for ($i = 0; $i <= $palette_size; $i++) {
                 $r = $bitmapReader->getUI8();
                 $g = $bitmapReader->getUI8();
                 $b = $bitmapReader->getUI8();
                 $color_palette[$i] = imagecolorallocate($im, $r, $g, $b);
             }
         } else {
             // alpha
             imagealphablending($im, false);
             imagesavealpha($im, true);
             for ($i = 0; $i <= $palette_size; $i++) {
                 $r = $bitmapReader->getUI8();
                 $g = $bitmapReader->getUI8();
                 $b = $bitmapReader->getUI8();
                 $a = $bitmapReader->getUI8();
                 $a = (1 - $a / 255) * 127;
                 $color_palette[$i] = imagecolorallocatealpha($im, $r, $g, $b, $a);
             }
         }
         // widthの読み出しbyte数を32bitで丸める(must be rounded up to the next 32-bit word boundary)
         $padding = ($width + 3 & -4) - $width;
         for ($y = 0; $y < $height; ++$y) {
             for ($x = 0; $x < $width; ++$x) {
                 $bi = $bitmapReader->getUI8();
                 imagesetpixel($im, $x, $y, $color_palette[$bi]);
             }
             $bi = $bitmapReader->incrementOffset($padding, 0);
             // skip
         }
     } else {
         // non parette
         if ($format == 4) {
             // PIX15
             for ($y = 0; $y < $height; ++$y) {
                 for ($x = 0; $x < $width; ++$x) {
                     $a = $bitmapReader->getUIBit();
                     // Pix15Reserved always 0
                     $r = $bitmapReader->getUIBits(5);
                     $g = $bitmapReader->getUIBits(5);
                     $b = $bitmapReader->getUIBits(5);
                     $c = imagecolorallocate($im, $r, $g, $b);
                     imagesetpixel($im, $x, $y, $c);
                 }
             }
         } elseif ($this->code == 20) {
             // PIX24
             for ($y = 0; $y < $height; ++$y) {
                 for ($x = 0; $x < $width; ++$x) {
                     $a = $bitmapReader->getUI8();
                     // 0 // Pix24Reserved always0
                     $r = $bitmapReader->getUI8();
                     $g = $bitmapReader->getUI8();
                     $b = $bitmapReader->getUI8();
                     $c = imagecolorallocate($im, $r, $g, $b);
                     imagesetpixel($im, $x, $y, $c);
                 }
             }
         } else {
             // alpha
             imagealphablending($im, false);
             imagesavealpha($im, true);
             for ($y = 0; $y < $height; ++$y) {
                 for ($x = 0; $x < $width; ++$x) {
                     $a = $bitmapReader->getUI8();
                     $a = (1 - $a / 255) * 127;
                     $r = $bitmapReader->getUI8();
                     $g = $bitmapReader->getUI8();
                     $b = $bitmapReader->getUI8();
                     $c = imagecolorallocatealpha($im, $r, $g, $b, $a);
                     imagesetpixel($im, $x, $y, $c);
                 }
             }
         }
     }
     ob_start();
     imagepng($im);
     $image_data = ob_get_contents();
     ob_end_clean();
     imagedestroy($im);
     return $image_data;
 }