Guest User

Untitled

a guest
Sep 15th, 2010
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1. <?php
  2.     set_time_limit(0);
  3.    
  4.     $magick_commands = array ('gm convert', 'convert');
  5.    
  6.     $image_rendition_geometries = array(
  7.         '138',
  8.         '240',
  9.         '400',
  10.         '507',
  11.         '800',
  12.         '1034',
  13.         '1200',
  14.         '1600',
  15.         '2000'
  16.     );
  17.  
  18.     // Make sure you have input.png
  19.     $input_file = 'input.png';
  20.  
  21.     // Since GraphicsMagick and ImageMagick accepts the same parameters, just iterate
  22.     // over the commands and concatenate with the same paramters.  
  23.     foreach ($magick_commands as $magick_command)
  24.     {
  25.         $start = getTime();
  26.  
  27.         // Iterate through the process several times to get a good average
  28.         for ($i = 1; $i <= 10; $i++)
  29.         {
  30.             // Iterate over the defined geometries. Having multiple geometries
  31.             // allows assement of average performance at different geometries
  32.             foreach ($image_rendition_geometries as $image_rendition_geometry)
  33.             {
  34.                 // Create output filename like this: 'gm convert-138.png', 'convert-2000.png', etc.
  35.                 $output_file = "'$magick_command-$image_rendition_geometry" . ".png'";
  36.                
  37.                 // Execute magick command
  38.                 $command = "$magick_command -units PixelsPerInch -density 96x96 -quality 100 -resize " . $image_rendition_geometry . "x $input_file $output_file";
  39.                 exec($command);
  40.             }
  41.         }
  42.  
  43.         $end =  getTime();
  44.         $time_difference = round($end - $start, 4);
  45.  
  46.         $iteration = $i - 1;
  47.         echo ("'$magick_command' took $time_difference seconds to execute $iteration iteration(s). <br/>");
  48.     }
  49.  
  50.     // Get filesize of the generated images
  51.     reset($magick_commands);
  52.     $file_sizes = array();
  53.     foreach ($magick_commands as $magick_command)
  54.     {
  55.         reset($image_rendition_geometries);
  56.         foreach ($image_rendition_geometries as $image_rendition_geometry)
  57.         {
  58.             $output_file = "$magick_command-$image_rendition_geometry" . ".png";
  59.             $file_sizes[$magick_command][$image_rendition_geometry] = filesize($output_file);
  60.         }
  61.     }
  62.  
  63.     // Display the average file sizes
  64.     reset($magick_commands);
  65.     foreach ($magick_commands as $magick_command)
  66.     {
  67.         print_r("Average filesize of $magick_command: " .  number_format(array_sum($file_sizes[$magick_command]) / count($image_rendition_geometries), 0, '', ',') . ' bytes. <br/>');
  68.     }  
  69.    
  70.     function getTime() {
  71.         $timer = explode( ' ', microtime() );
  72.         $timer = $timer[1] + $timer[0];
  73.         return $timer;
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment