Mega Code Archive

 
Categories / Php / Graphics
 

Database Image Size

<? 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. */ { //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; 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; 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; 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; } ?>