#native_company# #native_desc#
#native_cta#

Database Image Size

By Jose Edu
on November 26, 2001

Version: 0.2

Type: Function

Category: Graphics

License: GNU General Public License

Description: This code will retrieve the size of an image stored in a Database as a BLOB. It uses a variable with the BLOB data in it to do it so and it will return a two element array with the width an height of that image. It has support for JPEG, GIF and PNG as well.

<?

/* Gnovus Networks & Software (TM)
   Coded by: Joe666.
   
   This simple function will return a two element array with the width and height of an image
   from a database.  The PHP function will get such data using a filename, but what if the 
   data is a BLOB field stored in a PHP variable ?.  Well pass that variable as the parameter
   of this function and it will return the following array:
   
   		dimensions[0] --> Image Width
		dimensions[1] --> Image Height
		
   This will get sizes just for GIF and JPEG files since that's all I need by now.
   
   If you are able to correct,optimize or update in any form this code please let us 
   know at the following address:
   
   	[email protected]
	
   This code is free for you to use, copy or modify it in anywhere you want as long as you
   do not remove our name from it.  It is provided "as is" without any warranty so use it 
   at your own risk.
   
   Hopefully you'll find it useful.
   
   Gnovus Networks & Software.
   www.gnovus.com
   Mexico.
*/

/* May 7th, 2001.

   We are now adding support for PNG Files
*/
/* Nov 23th, 2001
	Now get resized images 
	By Jose Eduardo Mascarell Moreno. ([email protected])
	
	Linkz Multimedia.
	www.linkz.es
	Spain.
*/

function dbimagesize($photo)
{
	//Verify it is a JPEG file
	if(ord($photo[0]) == 255 && ord($photo[1]) == 216 && ord($photo[2]) == 255 && ord($photo[3]) == 224)
	{	
		//If it is a JPEG file then retieve it's width and height
		$i = 4;  //Skip the JPEG header
		do
		{
			$offset = (ord($photo[$i]) << 8) + (ord($photo[$i+1]));		
			for($j=0;$j<$offset && $i<strlen($photo);$j++,$i++);		
			if(ord($photo[$i]) == 255 && (ord($photo[$i+1]) & 240) == 192)
				break;
			$i+=2;		
		}while($i<strlen($photo));
		
		$i+=5;
		
		$width  = ((ord($photo[$i+2]) << 8) + ord($photo[$i+3]));
		$height = ((ord($photo[$i])   << 8) + ord($photo[$i+1]));
		
		$dimensions[] = $width;
		$dimensions[] = $height;
	
		return $dimensions;
	}
	
	//Verify it is a GIF file
	if(strcmp(substr($photo,0,3),"GIF") == 0)
	{
		//if it is GIF file then retrieve it's width and height
		$width  = ((ord($photo[7]) << 8) + ord($photo[6]));
		$height = ((ord($photo[9]) << 8) + ord($photo[8]));

		$dimensions[] = $width;
		$dimensions[] = $height;
	
		return $dimensions;		
	}
	
	//Verify it is a PNG file
	if(ord($photo[0]) == 137 && ord($photo[1]) == 80 && ord($photo[2]) == 78 &&
	   ord($photo[3]) == 71  && ord($photo[4]) == 13 && ord($photo[5]) == 10 &&
	   ord($photo[6]) == 26  && ord($photo[7]) == 10)
	{
		//Since the beginning of file contains the proper sequence of bytes then
		//we have a PNG file.  The next steps are: 
		//
		// 1. Next 4 bytes are the size of the IHDR, this seems to always be 00 00 00 0D, skip them
		// 2. Since 13 (0D) is the size of the IHDR, get the next 4 bytes (IHDR), this is the
		//    type of header.
		// 3. Now get the Width of Image given by the following 4 bytes.
		// 4. Finally get the next 4 bytes to retrieve the Height of the image
		
		//Ok, skip the first 8 bytes, 4 bytes of Chunk size and 4 bytes of Chunk type
		//that means we must begin at array position 16 to get the Width of file.
		
		$width  = ((ord($photo[16]) << 24)+(ord($photo[17]) << 16)+(ord($photo[18]) << 8)+(ord($photo[19])));
		$height = ((ord($photo[20]) << 24)+(ord($photo[21]) << 16)+(ord($photo[22]) << 8)+(ord($photo[23])));

		$dimensions[] = $width;
		$dimensions[] = $height;
	
		return $dimensions;		
	}
	
	return false;
}

function resize_dbimagesize($photo,$const_resize)
/*
This function is useful to get the images resized.
$const_resize --> Define the max size (width or heigth) of the image. 

Autor: Jose Ed. Mascarell Moreno
E-mail:[email protected]

Linkz Multimedia www.linkz.es
Valencia (Spain)

*/
{
	//Verify it is a JPEG file
	if(ord($photo[0]) == 255 && ord($photo[1]) == 216 && ord($photo[2]) == 255 && ord($photo[3]) == 224)
	{	
		//If it is a JPEG file then retieve it's width and height
		$i = 4;  //Skip the JPEG header
		do
		{
			$offset = (ord($photo[$i]) << 8) + (ord($photo[$i+1]));		
			for($j=0;$j<$offset && $i<strlen($photo);$j++,$i++);		
			if(ord($photo[$i]) == 255 && (ord($photo[$i+1]) & 240) == 192)
				break;
			$i+=2;		
		}while($i<strlen($photo));
		
		$i+=5;
		
		$width  = ((ord($photo[$i+2]) << 8) + ord($photo[$i+3]));
		$height = ((ord($photo[$i])   << 8) + ord($photo[$i+1]));
		/* Code added by Jose Ed. Mascarell Moreno
			[email protected].
		*/
		$dimensions[] = $width;
		$dimensions[] = $height;
		if ($dimensions[0]>=$dimensions[1]){
		$dimensions[1]=$dimensions[1]/($dimensions[0]/$const_resize);
		$dimensions[0]=$const_resize;}
		else{
		$dimensions[0]=$dimensions[0]/($dimensions[1]/$const_resize);
		$dimensions[1]=$const_resize;
		}
		/* End Added Code */

		return $dimensions;
	}
	
	//Verify it is a GIF file
	if(strcmp(substr($photo,0,3),"GIF") == 0)
	{
		//if it is GIF file then retrieve it's width and height
		$width  = ((ord($photo[7]) << 8) + ord($photo[6]));
		$height = ((ord($photo[9]) << 8) + ord($photo[8]));

		$dimensions[] = $width;
		$dimensions[] = $height;
		/* Code added by Jose Ed. Mascarell Moreno
			[email protected].
		*/
		if ($dimensions[0]>=$dimensions[1]){
		$dimensions[1]=$dimensions[1]/($dimensions[0]/$const_resize);
		$dimensions[0]=$const_resize;}
		else{
		$dimensions[0]=$dimensions[0]/($dimensions[1]/$const_resize);
		$dimensions[1]=$const_resize;
		}
		/* End Added Code */

		return $dimensions;		
	}
	
	//Verify it is a PNG file
	if(ord($photo[0]) == 137 && ord($photo[1]) == 80 && ord($photo[2]) == 78 &&
	   ord($photo[3]) == 71  && ord($photo[4]) == 13 && ord($photo[5]) == 10 &&
	   ord($photo[6]) == 26  && ord($photo[7]) == 10)
	{
		//Since the beginning of file contains the proper sequence of bytes then
		//we have a PNG file.  The next steps are: 
		//
		// 1. Next 4 bytes are the size of the IHDR, this seems to always be 00 00 00 0D, skip them
		// 2. Since 13 (0D) is the size of the IHDR, get the next 4 bytes (IHDR), this is the
		//    type of header.
		// 3. Now get the Width of Image given by the following 4 bytes.
		// 4. Finally get the next 4 bytes to retrieve the Height of the image
		
		//Ok, skip the first 8 bytes, 4 bytes of Chunk size and 4 bytes of Chunk type
		//that means we must begin at array position 16 to get the Width of file.
		
		$width  = ((ord($photo[16]) << 24)+(ord($photo[17]) << 16)+(ord($photo[18]) << 8)+(ord($photo[19])));
		$height = ((ord($photo[20]) << 24)+(ord($photo[21]) << 16)+(ord($photo[22]) << 8)+(ord($photo[23])));

		$dimensions[] = $width;
		$dimensions[] = $height;
		/* Code added by Jose Ed. Mascarell Moreno
			[email protected].
		*/
		if ($dimensions[0]>=$dimensions[1]){
		$dimensions[1]=$dimensions[1]/($dimensions[0]/$const_resize);
		$dimensions[0]=$const_resize;}
		else{
		$dimensions[0]=$dimensions[0]/($dimensions[1]/$const_resize);
		$dimensions[1]=$const_resize;
		}
		/* End Added Code */

		return $dimensions;		
	}
	
	return false;
}
?>