Randomly not working

I am working on a script that will randomly choose a 10 photos, resize them to a max between 375 and 435 and rotate them between -20 and 20 degrees. They will eventually be layered on top of each other and the layers will change which one is on top every 5 seconds using javascript.

I had it all working fine before I added the changing the size to a random size between 375 and 435 - I was just using vertical images and resizing them to 333 x 500. Once I added the script to be able to dynamically choose a size and do it, the script will randomly not do anywhere from 1 to 4 of the images. I put them one under another with the values of the random numbers.

Any suggestions would be appreciated.

Here is the basics of the codes I’m using:
index.php (this code is written 10 times in the page)

<?php
   $rndnum=rand(1,$numofpics);
   $rndrotate=rand(-1*$maxrotation,$maxrotation);
   if ($rndrotate < 0)
   {
      $rndrotate = 360 + $rndrotate;
   }
   $rndsize = rand($minsize, $maxsize);
   print "<img src='rotategif.php?Image=$rndnum&Rotate=$rndrotate&Size=$rndsize'>";
?>

rotategif.php (complete file)

<?php
$filename = 'test' . $_GET['Image'] . '.jpg';
$degrees = $_GET['Rotate'];
$maxsize = $_GET['Size'];

header('Content-type: image/gif');

$source = imagecreatefromjpeg($filename);

list($width, $height) = getimagesize($filename);
if ($width > $height)
{
   $newwidth = $maxsize;
   $newheight = $height * ($newwidth / $width);
}
else
{
   $newheight = $maxsize;
   $newwidth = $width * ($newheight / $height);
}


$image = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($image, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

$extracolor = imagecolorallocate($image, 15, 0, 15);

$rotate = imagerotate($image, $degrees, $extracolor);

imagecolortransparent($rotate, $extracolor); 
 
imagegif($rotate);
?> 

To see the output, you can go to http://www.natural-beauty.com/Test/inedx.php

Sorry, troubleshot for an hour or more and couldn’t figure out the problem, but after I posted this… I worked on it some more and figured out that 3 of the 20 images I was using were corrupted. I think it’s all working now…