ming slideshow

"Antony Briggs" <[email protected]> Sun, 9 Nov 2003 13:43:20 -0000
Newsgroups gmane.comp.web.ming.general
Message-ID <[email protected]>
Hi Guys,

I posted a little while ago with a problem on creating an image slideshow on
a windows box. I have since moved to Linux and things seemed to be going
fine, except now I'm having similar problems again :-(

A little background:

(for a demo of the current version, have a look at:
http://www.hamstercottages.co.uk)

The php-ming script is sourcing a load of jpegs, and cross fading between
them. My initial version created 3 SWFSprite()'s and each one was positioned
differently on the screen and cross faded between one-third of the available
images. (please see demo to understand what I'm banging on about).

This seemed to be working beautifully and I thought the job was put to bed.
But the client rang the next day saying the movie loaded really slowly on
his 128k isdn connection and can I speed it up?

So I loaded the resulting swf file into flash and saw that it was loading
all the jpegs in the first frame (which contained the 3 SWFSprite()'s )
instead of streaming them in. "Pah!" I said. so re-wrote the script so all
the jpegs are in the main timeline and for each frame the php manages what
stage of the fade each jpeg is in.

While developing the code, I had it restricted to 14 images and it works
very well. I downloaded the resulting swf and viewed it in flash as before,
and it now streams the jpegs in, with a maximum peak of about 2k/sec.
"Fantastic!" I said and removed the restriction. And now I'm back to where I
started. The script is producing some output, but flash flayer won't load
it.

Anyone any ideas?

My Linux ISP is running ming 0.2a compiled into php with
'--with-ming=shared'. If I upload a recent mod_ming.so and add it to
php.ini, will it override the compiled in version? Will a newer version make
any difference?

I've attached the script to this message in case anyone would like to play
with it. It downloads the necessary images to /tmp and re-sizes them to 150
x 100 before running the clever stuff so you should be able to run it out of
the box.

If anyone's got any clues I'd be very appreciative.

Turrah,

Antony Briggs

_______________________________________________
ming-fun mailing list
[email protected]
http://www.opaque.net/mailman/listinfo/ming-fun
genhompageflashab.php (application/octet-stream, 8 KB)
<?
error_reporting (E_ALL);

function NewImg( $filename, $orig_size, $maxxsize, $maxysize, $fileout) {

	$img = imagecreatefromjpeg( $filename);
	if( $img) {
	
		$CurAspect = (float)((float)($orig_size[0]) / (float)($orig_size[1]));
		$NewAspect = (float)((float)($maxxsize) / (float)($maxysize));
			
		if( $CurAspect > $NewAspect) {
			$intXSize = $maxxsize;
			$intYSize = ($intXSize / $orig_size[0]) * $orig_size[1];
		} else {
			$intYSize = $maxysize;
			$intXSize = ($intYSize / $orig_size[1]) * $orig_size[0];
		}

		if( true) {
			// GD2
			$im_out = ImageCreateTrueColor($intXSize,$intYSize);
			ImageCopyResampled($im_out, $img, 0, 0, 0, 0,
				$intXSize, $intYSize,$orig_size[0], $orig_size[1]);
		
			imagejpeg($im_out, $fileout, 40);
		} else {
			// GD
			$im_out = ImageCreate($intXSize,$intYSize);
			ImageCopyResized($im_out, $img, 0, 0, 0, 0,
				$intXSize, $intYSize,$orig_size[0], $orig_size[1]);
		
			imagejpeg($im_out, $fileout, 85);
		}
	}
}

//	$hamURL = "http://www.lucana.co.uk/hamster_cottages";
	$hamURL = "http://www.hamstercottages.co.uk";

  $movieWidth = 400;
  $movieHeight = 200;
  $fadeRate = 24.0;
  $maxWidth = 150;
  $maxHeight = 100;
	
  $m = new SWFMovie();
  $m->setRate($fadeRate);
  $m->setDimension($movieWidth, $movieHeight);
  $m->setBackground(0xff, 0xff, 0xff);

  $f = fopen("$hamURL/properties/getImages.php",'r');
  $images = "";
	do {
	    $data = fread($f, 8192);
	    if (strlen($data) == 0) {
	        break;
	    }
		$images .= $data;
	} while(true);

    fclose($f);

	$count = 0;
    $images = explode("\r\n", $images);
	$Pics = array();
	while ((list ($key, $val) = each ($images))) {
		if( strlen($val) == 0) continue;
		$count++;
		if( $count == 14)  break;
		$tempNam =tempnam("/tmp", "ham");
		$fin = fopen("$hamURL/properties/$val",'rb');
		$fout = fopen($tempNam, "w");

		do {
		    $data = fread($fin, 8192);
		    if (strlen($data) == 0) {
		        break;
		    }
			fwrite($fout, $data);
		} while(true);

		fclose($fin);
		fclose($fout);

		$Pics[] = array("name" => $tempNam, "origname" => $val);
	}

	$now18thYear = tempnam("/tmp", "ham");
	$fin = fopen("$hamURL/images/18thyear.jpg",'rb');
	$fout = fopen($now18thYear, "w");

	do {
		$data = fread($fin, 8192);
		if (strlen($data) == 0) {
			break;
		}
		fwrite($fout, $data);
	} while(true);

	fclose($fin);
	fclose($fout);

	reset( $Pics);

	while ((list ($key, $val) = each ($Pics))) {
		$size = getimagesize($val['name']);
		NewImg( $val['name'], $size, 150, 100, $val['name']);
	}

	$b = new SWFBitmap(fopen($now18thYear, "r"));
	$i = $m->add($b);
	$i->moveTo( 0, 62);
	$i->scaleTo(0.99, 0.99);
	$m->nextFrame();
	
	$playlist = array();
	
	$playlist[1] = array();
	$playlist[2] = array();
	$playlist[3] = array();

	$playlist[1]['action'] = "new";

	$playlist[2]['action'] = "pause";
	$playlist[2]['count'] = 0;
	$playlist[2]['max'] = 2*$fadeRate;
	$playlist[2]['next'] = "new";

	$playlist[3]['action'] = "pause";
	$playlist[3]['count'] = 0;
	$playlist[3]['max'] = 4*$fadeRate;
	$playlist[3]['next'] = "new";
	
	reset( $Pics);
	
	$PicsCopy = $Pics;

	while( !(count($Pics) == 0 && $playlist[1]['action'] == "fadeout")) {
		for( $iCount = 1; $iCount <= 3; $iCount++) {
			if( $playlist[$iCount]['action'] == "remove") {
				$i = $playlist[$iCount]['handle'];
				$m->remove($i);

				$playlist[$iCount]['action'] = "new";
			}
			if( $playlist[$iCount]['action'] == "new") {
				
				if( count($Pics)) {
					$pic = array_pop($Pics);
				} else {
					if( $iCount == 2 || $iCount == 3) {
						if( $iCount == 2)
							$thePic = (int)count($PicsCopy)/3;
						else
							$thePic = (int)(count($PicsCopy)/3)*2;
						$pic = $PicsCopy[$thePic];
					}
				}
				
//				echo $iCount.": ".$pic['origname']." (".count($Pics).")<br>\r\n";
								
				$pathBits = explode("/", $pic['origname']);
				$propref = $pathBits[0];
				
				//Import a bitmap
				$b = new SWFBitmap(fopen($pic['name'], "r"));
				//Get it's width and height
				$w = $b->getWidth();
				$h = $b->getHeight();
		
				//Convert Bitmap to a shape for Flash.
				//This process is automated upon import in Flash, but with
				//Ming we have have to do it ourselves. I see it as more control:)
				$s = new SWFShape();
				$f = $s->addFill($b);
				$s->setRightFill($f);
				//draw corners for the bitmap shape
				$s->drawLine($w, 0);
				$s->drawLine(0, $h);
				$s->drawLine(-$w, 0);
				$s->drawLine(0, -$h);
		
				//Create a Button and add the bitmap to it
				$b = new SWFButton();
				$b->addShape($s, SWFBUTTON_HIT | SWFBUTTON_UP | SWFBUTTON_DOWN | SWFBUTTON_OVER);
				
				// set the click action
				$b->addAction(new SWFAction("getURL('properties/$propref/$propref.asp','_self');"),	SWFBUTTON_MOUSEUP);
				
				//add our button to this movieclip
				$i = $m->add($b);
				switch( $iCount) {
				case 1:
					$i->moveTo( 80, 80);
					break;
				case 2:
					$i->moveTo( ($movieWidth/2) - ($maxWidth/2)+35, 2);
					break;
				case 3:
					$i->moveTo( $movieWidth-$maxWidth-10, $movieHeight-$maxHeight-10);
					break;
				}
			
				$playlist[$iCount]['handle'] = $i;

				$playlist[$iCount]['action'] = "fadein";
				$playlist[$iCount]['count'] = 0;
			}
			if( $playlist[$iCount]['action'] == "fadein") {
				$i = $playlist[$iCount]['handle'];
				$fadeCount = $playlist[$iCount]['count'];
				if( $fadeCount < $fadeRate) {
					$alpha = (float)((float)$fadeCount/(float)($fadeRate-1));
					$i->multColor(1.000000, 1.000000, 1.000000, $alpha);
					$m->nextFrame();
					$playlist[$iCount]['count']++;
				} else {
					$playlist[$iCount]['action'] = "pause";
					$playlist[$iCount]['count'] = 0;
					$playlist[$iCount]['max'] = 6*$fadeRate;
					$playlist[$iCount]['next'] = "fadeout";
				}
			}	
			if( $playlist[$iCount]['action'] == "pause") {
				$fadeCount = $playlist[$iCount]['count'];
				$fadeMax = $playlist[$iCount]['max'];
				if( $fadeCount < $fadeMax) {
					$playlist[$iCount]['count']++;
				} else {
					$playlist[$iCount]['action'] = $playlist[$iCount]['next'];
					$playlist[$iCount]['count'] = 0;
				}
			}
			if( $playlist[$iCount]['action'] == "fadeout") {
				$i = $playlist[$iCount]['handle'];
				$fadeCount = $playlist[$iCount]['count'];
				if( $fadeCount < $fadeRate ) {
					$alpha = (float)((float)$fadeCount/(float)($fadeRate-1));
					$alpha = 1 - $alpha;
					$i->multColor(1.000000, 1.000000, 1.000000, $alpha);
					$m->nextFrame();
					$playlist[$iCount]['count']++;
				} else {
					$playlist[$iCount]['action'] = "remove";
				}
			}	
		}
//		var_dump($playlist);
	}

	$playlist[1]['action'] = "fadeout";
	$playlist[1]['count'] = 0;

	$playlist[2]['action'] = "pause";
	$playlist[2]['count'] = 0;
	$playlist[2]['max'] = 2*$fadeRate;
	$playlist[2]['next'] = "fadeout";

	$playlist[3]['action'] = "pause";
	$playlist[3]['count'] = 0;
	$playlist[3]['max'] = 4*$fadeRate;
	$playlist[3]['next'] = "fadeout";

	$bExit = false;
	
	while( $playlist[3]['action'] != "dead") {
		for( $iCount = 1; $iCount <= 3; $iCount++) {
			if( $playlist[$iCount]['action'] == "remove") {
				$i = $playlist[$iCount]['handle'];
				$m->remove($i);

				$playlist[$iCount]['action'] = "dead";
				$bExit = true;
			}
			if( $playlist[$iCount]['action'] == "pause") {
				$fadeCount = $playlist[$iCount]['count'];
				$fadeMax = $playlist[$iCount]['max'];
				if( $fadeCount < $fadeMax) {
					$playlist[$iCount]['count']++;
				} else {
					$playlist[$iCount]['action'] = $playlist[$iCount]['next'];
					$playlist[$iCount]['count'] = 0;
				}
			}
			if( $playlist[$iCount]['action'] == "fadeout") {
				$i = $playlist[$iCount]['handle'];
				$fadeCount = $playlist[$iCount]['count'];
				if( $fadeCount < $fadeRate ) {
					$alpha = (float)((float)$fadeCount/(float)($fadeRate-1));
					$alpha = 1 - $alpha;
					$i->multColor(1.000000, 1.000000, 1.000000, $alpha);
					$m->nextFrame();
					$playlist[$iCount]['count']++;
				} else {
					$playlist[$iCount]['action'] = "remove";
				}
			}	
		}
	}

	
//	$m->add(new SWFAction("gotoAndPlay(".($fadeRate*3).");"));
//	$m->nextFrame();

	reset( $Pics);

	while ((list ($key, $val) = each ($Pics))) {
		unlink($val['name']);
	}

	unlink($now18thYear);

	header('Content-type: application/x-shockwave-flash');
	$m->output();
?>