GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2003, 11:34
BobbyDouglas's Avatar
BobbyDouglas BobbyDouglas is offline
Regular Member
 
Join Date: Aug 2003
Posts: 789
BobbyDouglas has a spectacular aura aboutBobbyDouglas has a spectacular aura about

Display Size and maybe download time


I want to be able to display the size of an image. If possilbe, also the download time it will take.

So it will first check the time it takes to download a specific file size. Then it will determine how long it will take, in terms of seconds, to download a specific file.

http://mrbobdouglas.com/photoshop/ is what page I would use this on.

What I want to do is just do something like <displaysize> then it will display the size of that particular image. If possible, it will show the amount of time to download it as well.

Any ideas on this?
__________________
Mr. Bob's Web Design - Tirelessly looking for ways to enhance the customer base of your business.
  #2  
Old 02-Nov-2003, 16:13
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Display Size and maybe download time


Tell me if this works out for you:

PHP Code:

<?php
// the image file
$img_file = '/home/mrbobdou/public_html/images/someimage.gif';

//////////////////////////////////
//   Some required functions
//////////////////////////////////
function calc_download_time( $Kb, $speed=28.8 )
{
   $speed = floatval( $speed );
   $Kb = floatval( $Kb );
   return( ceil($Kb / $speed) );
}

function convert_from_bytes( $bytes, $to=NULL )
{
   $float = floatval( $bytes );
   switch( $to )
   {
      case 'Kb' :            // Kilobit
         $float = ( $float*8 ) / 1024;
         break;
      case 'b' :             // bit
         $float *= 8;
         break;
      case 'GB' :            // Gigabyte
         $float /= 1024;
      case 'MB' :            // Megabyte
         $float /= 1024;
      case 'KB' :            // Kilobyte
         $float /= 1024;
      default :              // byte
   }
   unset( $bytes, $to );
   return( $float );
}

function get_image_size( $path2image )
{
   $img_size = 0;
   if( @file_exists($path2image) )
   {
      $contents = file_get_contents( $path2image );
      $img_size = strlen( $contents );
      unset( $contents );
   }
   else
   {
      trigger_error( '0 filesize returned for non-existant image file', E_USER_WARNING );
   }
   return( $img_size );
}
//////////////////////////////////


// get the image file size
$bytes = get_image_size( $img_file );

// convert the bytes into KiloBYTES
$KB = convert_from_bytes( $bytes, 'KB' );
  // format it to 1 decimal place
   $file_size = number_format( $KB, 1 );

// convert the bytes into KiloBITS
$Kb = convert_from_bytes( $bytes, 'Kb' );
  // calculate estimated time of download for 56Kbps modem
  $dl_time = calc_download_time( $Kb, 56 );

echo "<p>Image size: $file_size KB<br />\r\n";
echo "Estimated download time: $dl_time secs. [56Kbps]</p>\r\n";
?>

  #3  
Old 07-Nov-2003, 09:38
BobbyDouglas's Avatar
BobbyDouglas BobbyDouglas is offline
Regular Member
 
Join Date: Aug 2003
Posts: 789
BobbyDouglas has a spectacular aura aboutBobbyDouglas has a spectacular aura about
Sorry to be so late to respond, its been a very busy week.

Anyways, I uploaded the code, http://mrbobdouglas.com/test/scripts...play/index.php Works fine so far. Is there a way to calculate the speed of the user, and then figure out the download time? If thats very complicated to do then lets not bother with it.

How long did it take for you to type up the entire code to what it is right now?

1 more thing. I have about 4-5 images that I am going to make ready for download... Is there a way I can define 4-5 different sets, and then call them using like <$img1> or something to the effect?

Thanks a lot by the way
__________________
Mr. Bob's Web Design - Tirelessly looking for ways to enhance the customer base of your business.
  #4  
Old 07-Nov-2003, 10:42
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Quote:
Originally posted by BobbyDouglas
Sorry to be so late to respond, its been a very busy week.

Anyways, I uploaded the code, http://mrbobdouglas.com/test/scripts...play/index.php Works fine so far. Is there a way to calculate the speed of the user, and then figure out the download time? If thats very complicated to do then lets not bother with it.

If there's a way, I don't know it ... yet!

Quote:

How long did it take for you to type up the entire code to what it is right now?

Not more than 30 minutes, I think!? Why?

Quote:

1 more thing. I have about 4-5 images that I am going to make ready for download... Is there a way I can define 4-5 different sets, and then call them using like <$img1> or something to the effect?

Thanks a lot by the way

I knew you would be asking me this sooner or later.... but you'd have to wait a bit for me to paste some code next - for now I have to settle my domain name transfer thingy first.... I am running out of time!
  #5  
Old 07-Nov-2003, 11:42
BobbyDouglas's Avatar
BobbyDouglas BobbyDouglas is offline
Regular Member
 
Join Date: Aug 2003
Posts: 789
BobbyDouglas has a spectacular aura aboutBobbyDouglas has a spectacular aura about
Quote:
Not more than 30 minutes, I think!? Why?

- Just because its something so simple, but it seems so complicated to me. I wish I knew how to write code like that so fast. I really need to buy a PHP book. Sooner or later I will.

Quote:
I knew you would be asking me this sooner or later.... but you'd have to wait a bit for me to paste some code next - for now I have to settle my domain name transfer thingy first.... I am running out of time!

- Its not a problem. Whenever you get a chance.
__________________
Mr. Bob's Web Design - Tirelessly looking for ways to enhance the customer base of your business.
  #6  
Old 08-Nov-2003, 01:18
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Quote:
1 more thing. I have about 4-5 images that I am going to make ready for download... Is there a way I can define 4-5 different sets, and then call them using like <$img1> or something to the effect?

OK, let's assume you want to offer the following images for download
Code:
my_dog.jpg my_cat.jpg my_hamster.jpg

Also assume that these files are located in one location on your webserver e.g. /home/mybobdou/public_html/images/download/

The newer version of the sample code/page above could look something like this now:
PHP Code:

<?php
// the images for downloading
$imgs[]  = 'my_dog.jpg';
$imgs[]  = 'my_cat.jpg';
$imgs[]  = 'my_hamster.jpg';

// define the url to link to the images
// e.g. <a href="/images/download/my_dog.jpg">my_dog.jpg</a>
define( 'URL_IMAGES_DOWNLOAD', '/images/download/' );

// define the path to these images
define( 'PATH_IMAGES_DOWNLOAD', '/home/mybobdou/public_html'.URL_IMAGES_DOWNLOAD );

//////////////////////////////////
//   Some required functions
//////////////////////////////////
function display_image_download_link( $img )
{
  // get the image file size
  $bytes = get_image_size( PATH_IMAGES_DOWNLOAD.$img );
  
  // convert the bytes into KiloBYTES
  $KB = convert_from_bytes( $bytes, 'KB' );
    // format it to 1 decimal place
    $file_size = number_format( $KB, 1 );

  // convert the bytes into KiloBITS
  $Kb = convert_from_bytes( $bytes, 'Kb' );
    // calculate estimated time of download for 56Kbps modem
    $dl_time = calc_download_time( $Kb, 56 );
  
  echo "  <li><a href=\"".URL_IMAGES_DOWNLOAD."$img\">$img</a> ".
       "[size: $file_size KB] ".
       "[est. download time (56Kbps): $dl_time secs.]</li>\r\n";
  unset( $img, $bytes, $KB, $Kb, $file_size, $dl_time );
}

function calc_download_time( $Kb, $speed=28.8 )
{
   $speed = floatval( $speed );
   $Kb = floatval( $Kb );
   return( ceil($Kb / $speed) );
}

function convert_from_bytes( $bytes, $to=NULL )
{
   $float = floatval( $bytes );
   switch( $to )
   {
      case 'Kb' :            // Kilobit
         $float = ( $float*8 ) / 1024;
         break;
      case 'b' :             // bit
         $float *= 8;
         break;
      case 'GB' :            // Gigabyte
         $float /= 1024;
      case 'MB' :            // Megabyte
         $float /= 1024;
      case 'KB' :            // Kilobyte
         $float /= 1024;
      default :              // byte
   }
   unset( $bytes, $to );
   return( $float );
}

function get_image_size( $path2image )
{
   $img_size = 0;
   if( @file_exists($path2image) )
   {
      $contents = file_get_contents( $path2image );
      $img_size = strlen( $contents );
      unset( $contents );
   }
   else
   {
      trigger_error( '0 filesize returned for non-existant image file', E_USER_WARNING );
   }
   return( $img_size );
}
//////////////////////////////////

echo "<ul>\r\n";
foreach( $imgs as $img )
{
  display_image_download_link( $img );
}
echo "</ul>\r\n";
?>

 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.