Script

Show Script

Showing code for: includes/showNumber.irev

Back

<?rev

-- iRev include file
-- Command for showing a number as a set of images

command showNumberFn pNum, pStyle, pDigits

   -- set up the variables for your server
   put "http://" & $_SERVER["SERVER_NAME"] & "/images/numbers/" into tParentFolder
   put ".png" into tFileExt

   -- The images must be in folders called 1, 2, 3 etc as the style is a number
   -- The image files must be called 1.png, 2.png etc
   -- They can be caled 1.jpg or 1.gif if you change the file extension variable above.

   -- Look for any supplied data
   -- Supply default data if none supplied, or if supplied data is invalid

   put pNum into tNumber
   if tNumber is empty or tNumber is not a number then
      put random(10000) into tNumber
   end if

   -- I have set up 3 folders of images to give 3 different styles
   -- these folders are called 1, 2 and 3
   -- 6th May 2009: added another set thanks to terminal.on-rev.com
   put 4 into tNumStyles
   put pStyle into tStyle
   if tStyle < 1 or tStyle > tNumStyles then
      put random(tNumStyles) into tStyle
   end if
   put tParentFolder & tStyle & "/" into tNumbersFolder

   -- optionally, the number can be padded with leading zeroes to a minimum length
   -- if tNumDigits = 0, no padding is done
   put pDigits into tNumDigits
   if tNumDigits is not a number then
      put 0 into tNumDigits
   end if

   -- pad the number with leading zeroes to get the required minimum number of digits
   -- if the number is longer than required, the complete number will be shown, regardless of this setting
   -- don't pad the original as it will be returned at the end
   put tNumber into tPaddedNumber
   if tNumDigits > 0 then
      repeat
         if the length of tPaddedNumber >= tNumDigits then
            exit repeat
         end if
         put "0" before tPaddedNumber
      end repeat
   end if

   -- now gather the relevant images for each digit
   put "convert " into tCmd
   repeat for each char c in tPaddedNumber
      put tNumbersFolder & c & tFileExt & space after tCmd
   end repeat
   
   -- use ImageMagick to combine these into a single image
   if there is a file "number.png" then delete file "samples/number.png"
   put "+append number.png" after tCmd
   get shell(tCmd)

   -- then show the combined image
   ?>

   <img align="middle" src="number.png" alt="counter"/>
   
   <?rev
   -- return the settings used in case the calling page wants to know
   return tNumber & cr & tStyle & cr & tNumDigits
end showNumberFn

?>