📄 jpeg.php
字号:
} else { // No COM segment found return FALSE; }}/******************************************************************************* End of Function: get_jpeg_Comment******************************************************************************//******************************************************************************** Function: put_jpeg_Comment** Description: Creates a new JPEG Comment segment from a string, and inserts* this segment into the supplied JPEG header array** Parameters: jpeg_header_data - a JPEG header data array in the same format* as from get_jpeg_header_data, into which the* new Comment segment will be put* $new_Comment - a string containing the new Comment** Returns: jpeg_header_data - the JPEG header data array with the new* JPEG Comment segment added*******************************************************************************/function put_jpeg_Comment( $jpeg_header_data, $new_Comment ){ //Cycle through the header segments for( $i = 0; $i < count( $jpeg_header_data ); $i++ ) { // If we find an COM header, if ( strcmp ( $jpeg_header_data[$i]['SegName'], "COM" ) == 0 ) { // Found a preexisting Comment block - Replace it with the new one and return. $jpeg_header_data[$i]['SegData'] = $new_Comment; return $jpeg_header_data; } } // No preexisting Comment block found, find where to put it by searching for the highest app segment $i = 0; while ( ( $i < count( $jpeg_header_data ) ) && ( $jpeg_header_data[$i]["SegType"] >= 0xE0 ) ) { $i++; } // insert a Comment segment new at the position found of the header data. array_splice($jpeg_header_data, $i , 0, array( array( "SegType" => 0xFE, "SegName" => $GLOBALS[ "JPEG_Segment_Names" ][ 0xFE ], "SegDesc" => $GLOBALS[ "JPEG_Segment_Descriptions" ][ 0xFE ], "SegData" => $new_Comment ) ) ); return $jpeg_header_data;}/******************************************************************************* End of Function: put_jpeg_Comment******************************************************************************//******************************************************************************** Function: Interpret_Comment_to_HTML** Description: Generates html showing the contents of any JPEG Comment segment** Parameters: jpeg_header_data - the JPEG header data, as retrieved* from the get_jpeg_header_data function** Returns: output - the HTML*******************************************************************************/function Interpret_Comment_to_HTML( $jpeg_header_data ){ // Create a string to receive the output $output = ""; // read the comment segment $comment = get_jpeg_Comment( $jpeg_header_data ); // Check if the comment segment was valid if ( $comment !== FALSE ) { // Comment exists - add it to the output $output .= "<h2 class=\"JPEG_Comment_Main_Heading\">JPEG Comment</h2>\n"; $output .= "<p class=\"JPEG_Comment_Text\">$comment</p>\n"; } // Return the result return $output;}/******************************************************************************* End of Function: Interpret_Comment_to_HTML******************************************************************************//******************************************************************************** Function: get_jpeg_intrinsic_values** Description: Retreives information about the intrinsic characteristics of the* jpeg image, such as Bits per Component, Height and Width.** Parameters: jpeg_header_data - the JPEG header data, as retrieved* from the get_jpeg_header_data function** Returns: array - An array containing the intrinsic JPEG values* FALSE - if the comment segment couldnt be found*******************************************************************************/function get_jpeg_intrinsic_values( $jpeg_header_data ){ // Create a blank array for the output $Outputarray = array( ); //Cycle through the header segments until Start Of Frame (SOF) is found or we run out of segments $i = 0; while ( ( $i < count( $jpeg_header_data) ) && ( substr( $jpeg_header_data[$i]['SegName'], 0, 3 ) != "SOF" ) ) { $i++; } // Check if a SOF segment has been found if ( substr( $jpeg_header_data[$i]['SegName'], 0, 3 ) == "SOF" ) { // SOF segment was found, extract the information $data = $jpeg_header_data[$i]['SegData']; // First byte is Bits per component $Outputarray['Bits per Component'] = ord( $data{0} ); // Second and third bytes are Image Height $Outputarray['Image Height'] = ord( $data{ 1 } ) * 256 + ord( $data{ 2 } ); // Forth and fifth bytes are Image Width $Outputarray['Image Width'] = ord( $data{ 3 } ) * 256 + ord( $data{ 4 } ); // Sixth byte is number of components $numcomponents = ord( $data{ 5 } ); // Following this is a table containing information about the components for( $i = 0; $i < $numcomponents; $i++ ) { $Outputarray['Components'][] = array ( 'Component Identifier' => ord( $data{ 6 + $i * 3 } ), 'Horizontal Sampling Factor' => ( ord( $data{ 7 + $i * 3 } ) & 0xF0 ) / 16, 'Vertical Sampling Factor' => ( ord( $data{ 7 + $i * 3 } ) & 0x0F ), 'Quantization table destination selector' => ord( $data{ 8 + $i * 3 } ) ); } } else { // Couldn't find Start Of Frame segment, hence can't retrieve info return FALSE; } return $Outputarray;}/******************************************************************************* End of Function: get_jpeg_intrinsic_values******************************************************************************//******************************************************************************** Function: Interpret_intrinsic_values_to_HTML** Description: Generates html showing some of the intrinsic JPEG values which* were retrieved with the get_jpeg_intrinsic_values function** Parameters: values - the JPEG intrinsic values, as read from get_jpeg_intrinsic_values** Returns: OutputStr - A string containing the HTML*******************************************************************************/function Interpret_intrinsic_values_to_HTML( $values ){ // Check values are valid if ( $values != FALSE ) { // Write Heading $OutputStr = "<h2 class=\"JPEG_Intrinsic_Main_Heading\">Intrinsic JPEG Information</h2>\n"; // Create Table $OutputStr .= "<table class=\"JPEG_Intrinsic_Table\" border=1>\n"; // Put image height and width into table $OutputStr .= "<tr class=\"JPEG_Intrinsic_Table_Row\"><td class=\"JPEG_Intrinsic_Caption_Cell\">Image Height</td><td class=\"JPEG_Intrinsic_Value_Cell\">" . $values['Image Height'] . " pixels</td></tr>\n"; $OutputStr .= "<tr class=\"JPEG_Intrinsic_Table_Row\"><td class=\"JPEG_Intrinsic_Caption_Cell\">Image Width</td><td class=\"JPEG_Intrinsic_Value_Cell\">" . $values['Image Width'] . " pixels</td></tr>\n"; // Put colour depth into table if ( count( $values['Components'] ) == 1 ) { $OutputStr .= "<tr class=\"JPEG_Intrinsic_Table_Row\"><td class=\"JPEG_Intrinsic_Caption_Cell\">Colour Depth</td><td class=\"JPEG_Intrinsic_Value_Cell\">" . $values['Bits per Component'] . " bit Monochrome</td></tr>\n"; } else { $OutputStr .= "<tr class=\"JPEG_Intrinsic_Table_Row\"><td class=\"JPEG_Intrinsic_Caption_Cell\">Colour Depth</td><td class=\"JPEG_Intrinsic_Value_Cell\">" . ($values['Bits per Component'] * count( $values['Components'] ) ) . " bit</td></tr>\n"; } // Close Table $OutputStr .= "</table>\n"; // Return html return $OutputStr; }}/******************************************************************************* End of Function: Interpret_intrinsic_values_to_HTML******************************************************************************//******************************************************************************** Function: get_jpeg_image_data** Description: Retrieves the compressed image data part of the JPEG file** Parameters: filename - the filename of the JPEG file to read** Returns: compressed_data - A string containing the compressed data* FALSE - if retrieval failed*******************************************************************************/function get_jpeg_image_data( $filename ){ // prevent refresh from aborting file operations and hosing file ignore_user_abort(true); // Attempt to open the jpeg file $filehnd = @fopen($filename, 'rb'); // Check if the file opened successfully if ( ! $filehnd ) { // Could't open the file - exit return FALSE; } // Read the first two characters $data = network_safe_fread( $filehnd, 2 ); // Check that the first two characters are 0xFF 0xDA (SOI - Start of image) if ( $data != "\xFF\xD8" ) { // No SOI (FF D8) at start of file - close file and return; fclose($filehnd); return FALSE; } // Read the third character $data = network_safe_fread( $filehnd, 2 ); // Check that the third character is 0xFF (Start of first segment header) if ( $data{0} != "\xFF" ) { // NO FF found - close file and return fclose($filehnd); return; } // Flag that we havent yet hit the compressed image data $hit_compressed_image_data = FALSE; // Cycle through the file until, one of: 1) an EOI (End of image) marker is hit, // 2) we have hit the compressed image data (no more headers are allowed after data) // 3) or end of file is hit while ( ( $data{1} != "\xD9" ) && (! $hit_compressed_image_data) && ( ! feof( $filehnd ) )) { // Found a segment to look at. // Check that the segment marker is not a Restart marker - restart markers don't have size or data after them if ( ( ord($data{1}) < 0xD0 ) || ( ord($data{1}) > 0xD7 ) ) { // Segment isn't a Restart marker // Read the next two bytes (size) $sizestr = network_safe_fread( $filehnd, 2 ); // convert the size bytes to an integer $decodedsize = unpack ("nsize", $sizestr); // Read the segment data with length indicated by the previously read size $segdata = network_safe_fread( $filehnd, $decodedsize['size'] - 2 ); } // If this is a SOS (Start Of Scan) segment, then there is no more header data - the compressed image data follows if ( $data{1} == "\xDA" ) { // Flag that we have hit the compressed image data - exit loop after reading the data $hit_compressed_image_data = TRUE; // read the rest of the file in // Can't use the filesize function to work out
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -