Advertisement
Guest User

custom imgsize

a guest
Aug 10th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.72 KB | None | 0 0
  1. <?php
  2. class Parser_Provider_Image {
  3.  
  4.     /**
  5.      * Get image size by reading specific parts of image - meta data, falls back to getimagesize php method which downloads all image
  6.      * @param string $imageUrl path to image being reading
  7.      * @return array
  8.      */
  9.    public function getImageSize($imageUrl) {
  10.  
  11.         $fileInfo = pathinfo($imageUrl);
  12.  
  13.         if (!empty($fileInfo['extension']) && (strtolower($fileInfo['extension']) == 'jpg' || strtolower($fileInfo['extension']) == 'jpeg')) {
  14.             $sizes =  $this->getJpegSize($imageUrl);
  15.  
  16.         } else if (!empty($fileInfo['extension']) && strtolower($fileInfo['extension']) == 'png') {
  17.             $sizes =  $this->getPngSize($imageUrl);
  18.  
  19.         } else if (!empty($fileInfo['extension']) && strtolower($fileInfo['extension']) == 'gif') {
  20.             $sizes =  $this->getGifSize($imageUrl);
  21.         }
  22.  
  23.         if (empty($sizes[0]) || empty($sizes[1])) {
  24.             return @getimagesize($imageUrl);
  25.         } else {
  26.             return $sizes;
  27.         }
  28.  
  29.    }
  30.  
  31.   /**
  32.    *
  33.    * Parse jpeg file and get file size dimensions
  34.    * @param string $imgLocation
  35.    * @return mixed array|boolean
  36.    */
  37.   protected function getJpegSize($imgLocation) {
  38.  
  39.     $handle    = @fopen($imgLocation, "rb");
  40.     $new_block = null;
  41.  
  42.     if ($handle && !feof($handle)) {
  43.         $new_block = fread($handle, 32);
  44.         $i = 0;
  45.  
  46.         if ($new_block[$i]=="\xFF" && $new_block[$i+1]=="\xD8" && $new_block[$i+2]=="\xFF" && $new_block[$i+3]=="\xE0") {
  47.             $i += 4;
  48.             if ($new_block[$i+2]=="\x4A" && $new_block[$i+3]=="\x46" && $new_block[$i+4]=="\x49" && $new_block[$i+5]=="\x46" && $new_block[$i+6]=="\x00") {
  49.                 // Read block size and skip ahead to begin cycling through blocks in search of SOF marker
  50.                 $block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
  51.                 $block_size = hexdec($block_size[1]);
  52.  
  53.                 while(!feof($handle)) {
  54.                     $i += $block_size;
  55.                     $new_block .= fread($handle, $block_size);
  56.                     if (!empty($new_block[$i]) && $new_block[$i] == "\xFF") {
  57.                         // New block detected, check for SOF marker
  58.                         $sof_marker = array("\xC0", "\xC1", "\xC2", "\xC3", "\xC5", "\xC6", "\xC7", "\xC8", "\xC9", "\xCA", "\xCB", "\xCD", "\xCE", "\xCF");
  59.                         if (in_array($new_block[$i+1], $sof_marker)) {
  60.                             // SOF marker detected. Width and height information is contained in bytes 4-7 after this byte.
  61.                             $size_data = $new_block[$i+2] . $new_block[$i+3] . $new_block[$i+4] . $new_block[$i+5] . $new_block[$i+6] . $new_block[$i+7] . $new_block[$i+8];
  62.                             $unpacked = unpack("H*", $size_data);
  63.                             $unpacked = $unpacked[1];
  64.                             $height = hexdec($unpacked[6] . $unpacked[7] . $unpacked[8] . $unpacked[9]);
  65.                             $width  = hexdec($unpacked[10] . $unpacked[11] . $unpacked[12] . $unpacked[13]);
  66.                             return array($width, $height);
  67.  
  68.                         } else {
  69.                             // Skip block marker and read block size
  70.                             $i += 2;
  71.                             $block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
  72.                             $block_size = hexdec($block_size[1]);
  73.                         }
  74.                     } else {
  75.                         return false;
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     return false;
  82.   }
  83.  
  84.   /**
  85.    *
  86.    * Get png file size from meta
  87.    * @param string $imgLocation location to img
  88.    * @return mixed array|boolean
  89.    */
  90.   protected function getPngSize($imgLocation) {
  91.         $handle = @fopen($imgLocation, "rb");
  92.         $img    = NULL;
  93.  
  94.         if (!feof($handle)) {
  95.             $img = fread($handle, 24);
  96.  
  97.             $widthUppack = unpack('H*',$img[16] . $img[17] . $img[18] . $img[19]);
  98.             $width = hexdec($widthUppack[1]);
  99.  
  100.             $heightUppack = unpack('H*',$img[20] . $img[21] . $img[22] . $img[23]);
  101.             $height = hexdec($heightUppack[1]);
  102.  
  103.             return array($width, $height);
  104.  
  105.         } else {
  106.             return false;
  107.         }
  108.    }
  109.  
  110.    /**
  111.     * Get gif size from meta
  112.     * @param string $imgLocation
  113.     * @return mixed array|boolean
  114.     */
  115.    protected function getGifSize($imgLocation) {
  116.         $handle = @fopen($imgLocation, "rb");
  117.         $img = NULL;
  118.         if (!feof($handle)) {
  119.             $img = fread($handle, 13);
  120.  
  121.             $width = unpack('S', $img[6] . $img[7]);
  122.             $height = unpack('S', $img[8] . $img[9]);
  123.  
  124.             return array($width[1],  $height[1]);
  125.  
  126.         }
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement