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 != "ÿØ") {
        // 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] != "ÿ") {
        // 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] != "Ù" && !$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] == "Ú") {
            // 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
            // how much to read, as it won't work for files being read by http or ftp
            // So instead read 1Mb at a time till EOF
            $compressed_data = "";
            do {
                $compressed_data .= network_safe_fread($filehnd, 1048576);
            } while (!feof($filehnd));
            // Strip off EOI and anything after
            $EOI_pos = strpos($compressed_data, "ÿÙ");
            $compressed_data = substr($compressed_data, 0, $EOI_pos);
        } else {
            // Not an SOS - Read the next two bytes - should be the segment marker for the next segment
            $data = network_safe_fread($filehnd, 2);
            // Check that the first byte of the two is 0xFF as it should be for a marker
            if ($data[0] != "ÿ") {
                // Problem - NO FF foundclose file and return";
                fclose($filehnd);
                return;
            }
        }
    }
    // Close File
    fclose($filehnd);
    // Alow the user to abort from now on
    ignore_user_abort(false);
    // Return the compressed data if it was found
    if ($hit_compressed_image_data) {
        return $compressed_data;
    } else {
        return FALSE;
    }
}
Exemple #2
0
function read_IFD_universal($filehnd, $Tiff_offset, $Byte_Align, $Tag_Definitions_Name, $local_offsets = FALSE, $read_next_ptr = TRUE)
{
    if ($filehnd == NULL || feof($filehnd)) {
        return array(FALSE, 0);
    }
    // Record the Name of the Tag Group used for this IFD in the output array
    $OutputArray['Tags Name'] = $Tag_Definitions_Name;
    // Record the offset of the TIFF header in the output array
    $OutputArray['Tiff Offset'] = $Tiff_offset;
    // First 2 bytes of IFD are number of entries in the IFD
    $No_Entries_str = network_safe_fread($filehnd, 2);
    $No_Entries = get_IFD_Data_Type($No_Entries_str, 3, $Byte_Align);
    // If the data is corrupt, the number of entries may be huge, which will cause errors
    // This is often caused by a lack of a Next-IFD pointer
    if ($No_Entries > 10000) {
        // Huge number of entries - abort
        echo "<p>Error: huge number of EXIF entries - EXIF is probably Corrupted</p>\n";
        return array(FALSE, 0);
    }
    // If the data is corrupt or just stupid, the number of entries may zero,
    // Indicate this by returning false
    if ($No_Entries === 0) {
        // No entries - abort
        return array(FALSE, 0);
    }
    // Save the file position where first IFD record starts as non-standard offsets
    // need to know this to calculate an absolute offset
    $IFD_first_rec_pos = ftell($filehnd);
    // Read in the IFD structure
    $IFD_Data = network_safe_fread($filehnd, 12 * $No_Entries);
    // Check if the entire IFD was able to be read
    if (strlen($IFD_Data) != 12 * $No_Entries) {
        // Couldn't read the IFD Data properly, Some Casio files have no Next IFD pointer, hence cause this error
        echo "<p>Error: EXIF Corrupted</p>\n";
        return array(FALSE, 0);
    }
    // Last 4 bytes of a standard IFD are the offset to the next IFD
    // Some NON-Standard IFD implementations do not have this, hence causing problems if it is read
    // If the Next IFD pointer has been requested to be read,
    if ($read_next_ptr) {
        // Read the pointer to the next IFD
        $Next_Offset_str = network_safe_fread($filehnd, 4);
        $Next_Offset = get_IFD_Data_Type($Next_Offset_str, 4, $Byte_Align);
    } else {
        // Otherwise set the pointer to zero ( no next IFD )
        $Next_Offset = 0;
    }
    // Initialise current position to the start
    $pos = 0;
    // Loop for reading IFD entries
    for ($i = 0; $i < $No_Entries; $i++) {
        // First 2 bytes of IFD entry are the tag number ( Unsigned Short )
        $Tag_No_str = substr($IFD_Data, $pos, 2);
        $Tag_No = get_IFD_Data_Type($Tag_No_str, 3, $Byte_Align);
        $pos += 2;
        // Next 2 bytes of IFD entry are the data format ( Unsigned Short )
        $Data_Type_str = substr($IFD_Data, $pos, 2);
        $Data_Type = get_IFD_Data_Type($Data_Type_str, 3, $Byte_Align);
        $pos += 2;
        // If Datatype is not between 1 and 12, then skip this entry, it is probably corrupted or custom
        if ($Data_Type > 12 || $Data_Type < 1) {
            $pos += 8;
            continue 1;
            // Stop trying to process the tag any further and skip to the next one
        }
        // Next 4 bytes of IFD entry are the data count ( Unsigned Long )
        $Data_Count_str = substr($IFD_Data, $pos, 4);
        $Data_Count = get_IFD_Data_Type($Data_Count_str, 4, $Byte_Align);
        $pos += 4;
        if ($Data_Count > 100000) {
            echo "<p>Error: huge EXIF data count - EXIF is probably Corrupted</p>\n";
            // Some Casio files have no Next IFD pointer, hence cause errors
            return array(FALSE, 0);
        }
        // Total Data size is the Data Count multiplied by the size of the Data Type
        $Total_Data_Size = $GLOBALS['IFD_Data_Sizes'][$Data_Type] * $Data_Count;
        $Data_Start_pos = -1;
        // If the total data size is larger than 4 bytes, then the data part is the offset to the real data
        if ($Total_Data_Size > 4) {
            // Not enough room for data - offset provided instead
            $Data_Offset_str = substr($IFD_Data, $pos, 4);
            $Data_Start_pos = get_IFD_Data_Type($Data_Offset_str, 4, $Byte_Align);
            // In some NON-STANDARD makernotes, the offset is relative to the start of the current IFD entry
            if ($local_offsets) {
                // This is a NON-Standard IFD, seek relative to the start of the current tag
                fseek($filehnd, $IFD_first_rec_pos + $pos - 8 + $Data_Start_pos);
            } else {
                // This is a normal IFD, seek relative to the start of the TIFF header
                fseek($filehnd, $Tiff_offset + $Data_Start_pos);
            }
            // Read the data block from the offset position
            $DataStr = network_safe_fread($filehnd, $Total_Data_Size);
        } else {
            // The data block is less than 4 bytes, and is provided in the IFD entry, so read it
            $DataStr = substr($IFD_Data, $pos, $Total_Data_Size);
        }
        // Increment the position past the data
        $pos += 4;
        // Now create the entry for output array
        $Data_Array = array();
        // Read the data items from the data block
        if ($Data_Type != 2 && $Data_Type != 7) {
            // The data type is Numerical, Read the data items from the data block
            for ($j = 0; $j < $Data_Count; $j++) {
                $Part_Data_Str = substr($DataStr, $j * $GLOBALS['IFD_Data_Sizes'][$Data_Type], $GLOBALS['IFD_Data_Sizes'][$Data_Type]);
                $Data_Array[] = get_IFD_Data_Type($Part_Data_Str, $Data_Type, $Byte_Align);
            }
        } elseif ($Data_Type == 2) {
            // The data type is String(s)   (type 2)
            // Strip the last terminating Null
            $DataStr = substr($DataStr, 0, strlen($DataStr) - 1);
            // Split the data block into multiple strings whereever there is a Null
            $Data_Array = explode("", $DataStr);
        } else {
            // The data type is Unknown (type 7)
            // Do nothing to data
            $Data_Array = $DataStr;
        }
        // If this is a Sub-IFD entry,
        if (array_key_exists($Tag_No, $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name]) && "SubIFD" == $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name][$Tag_No]['Type']) {
            // This is a Sub-IFD entry, go and process the data forming Sub-IFD and use its output array as the new data for this entry
            fseek($filehnd, $Tiff_offset + $Data_Array[0]);
            $Data_Array = read_Multiple_IFDs($filehnd, $Tiff_offset, $Byte_Align, $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name][$Tag_No]['Tags Name']);
        }
        $desc = "";
        $units = "";
        // Check if this tag exists in the list of tag definitions,
        if (array_key_exists($Tag_No, $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name])) {
            if (array_key_exists('Description', $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name][$Tag_No])) {
                $desc = $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name][$Tag_No]['Description'];
            }
            if (array_key_exists('Units', $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name][$Tag_No])) {
                $units = $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name][$Tag_No]['Units'];
            }
            // Tag exists in definitions, append details to output array
            $OutputArray[$Tag_No] = array("Tag Number" => $Tag_No, "Tag Name" => $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name][$Tag_No]['Name'], "Tag Description" => $desc, "Data Type" => $Data_Type, "Type" => $GLOBALS["IFD_Tag_Definitions"][$Tag_Definitions_Name][$Tag_No]['Type'], "Units" => $units, "Data" => $Data_Array);
        } else {
            // Tag doesnt exist in definitions, append unknown details to output array
            $OutputArray[$Tag_No] = array("Tag Number" => $Tag_No, "Tag Name" => "Unknown Tag #" . $Tag_No, "Tag Description" => "", "Data Type" => $Data_Type, "Type" => "Unknown", "Units" => "", "Data" => $Data_Array);
        }
        // Some information of type "Unknown" (type 7) might require information about
        // how it's position and byte alignment in order to be decoded
        if ($Data_Type == 7) {
            $OutputArray[$Tag_No]['Offset'] = $Data_Start_pos;
            $OutputArray[$Tag_No]['Byte Align'] = $Byte_Align;
        }
        ////////////////////////////////////////////////////////////////////////
        // Special Data handling
        ////////////////////////////////////////////////////////////////////////
        // Check if this is a Print Image Matching entry
        if ($OutputArray[$Tag_No]['Type'] == "PIM") {
            // This is a Print Image Matching entry, decode it.
            $OutputArray[$Tag_No] = Decode_PIM($OutputArray[$Tag_No], $Tag_Definitions_Name);
        }
        // Interpret the entry into a text string using a custom interpreter
        $text_val = get_Tag_Text_Value($OutputArray[$Tag_No], $Tag_Definitions_Name);
        // Check if a text string was generated
        if ($text_val !== FALSE) {
            // A string was generated, append it to the output array entry
            $OutputArray[$Tag_No]['Text Value'] = $text_val;
            $OutputArray[$Tag_No]['Decoded'] = TRUE;
        } else {
            // A string was NOT generated, append a generic string to the output array entry
            $OutputArray[$Tag_No]['Text Value'] = get_IFD_value_as_text($OutputArray[$Tag_No]) . " " . $units;
            $OutputArray[$Tag_No]['Decoded'] = FALSE;
        }
        // Check if this entry is the Maker Note
        if ($Tag_Definitions_Name == "EXIF" && $Tag_No == 37500) {
            // Save some extra information which will allow Makernote Decoding with the output array entry
            $OutputArray[$Tag_No]['Offset'] = $Data_Start_pos;
            $OutputArray[$Tag_No]['Tiff Offset'] = $Tiff_offset;
            $OutputArray[$Tag_No]['ByteAlign'] = $Byte_Align;
            // Save a pointer to this entry for Maker note processing later
            $GLOBALS["Maker_Note_Tag"] =& $OutputArray[$Tag_No];
        }
        // Check if this is a IPTC/NAA Record within the EXIF IFD
        if (($Tag_Definitions_Name == "EXIF" || $Tag_Definitions_Name == "TIFF") && $Tag_No == 33723) {
            // This is a IPTC/NAA Record, interpret it and put result in the data for this entry
            $OutputArray[$Tag_No]['Data'] = get_IPTC($DataStr);
            $OutputArray[$Tag_No]['Decoded'] = TRUE;
        }
        // Change: Check for embedded XMP as of version 1.11
        // Check if this is a XMP Record within the EXIF IFD
        if (($Tag_Definitions_Name == "EXIF" || $Tag_Definitions_Name == "TIFF") && $Tag_No == 700) {
            // This is a XMP Record, interpret it and put result in the data for this entry
            $OutputArray[$Tag_No]['Data'] = read_XMP_array_from_text($DataStr);
            $OutputArray[$Tag_No]['Decoded'] = TRUE;
        }
        // Change: Check for embedded IRB as of version 1.11
        // Check if this is a Photoshop IRB Record within the EXIF IFD
        if (($Tag_Definitions_Name == "EXIF" || $Tag_Definitions_Name == "TIFF") && $Tag_No == 34377) {
            // This is a Photoshop IRB Record, interpret it and put result in the data for this entry
            $OutputArray[$Tag_No]['Data'] = unpack_Photoshop_IRB_Data($DataStr);
            $OutputArray[$Tag_No]['Decoded'] = TRUE;
        }
        // Exif Thumbnail
        // Check that both the thumbnail length and offset entries have been processed,
        // and that this is one of them
        if (($Tag_No == 513 && array_key_exists(514, $OutputArray) || $Tag_No == 514 && array_key_exists(513, $OutputArray)) && $Tag_Definitions_Name == "TIFF") {
            // Seek to the start of the thumbnail using the offset entry
            fseek($filehnd, $Tiff_offset + $OutputArray[513]['Data'][0]);
            // Read the thumbnail data, and replace the offset data with the thumbnail
            $OutputArray[513]['Data'] = network_safe_fread($filehnd, $OutputArray[514]['Data'][0]);
        }
        // Casio Thumbnail
        // Check that both the thumbnail length and offset entries have been processed,
        // and that this is one of them
        if (($Tag_No == 0x4 && array_key_exists(0x3, $OutputArray) || $Tag_No == 0x3 && array_key_exists(0x4, $OutputArray)) && $Tag_Definitions_Name == "Casio Type 2") {
            // Seek to the start of the thumbnail using the offset entry
            fseek($filehnd, $Tiff_offset + $OutputArray[0x4]['Data'][0]);
            // Read the thumbnail data, and replace the offset data with the thumbnail
            $OutputArray[0x4]['Data'] = network_safe_fread($filehnd, $OutputArray[0x3]['Data'][0]);
        }
        // Minolta Thumbnail
        // Check that both the thumbnail length and offset entries have been processed,
        // and that this is one of them
        if (($Tag_No == 0x88 && array_key_exists(0x89, $OutputArray) || $Tag_No == 0x89 && array_key_exists(0x88, $OutputArray)) && $Tag_Definitions_Name == "Olympus") {
            // Seek to the start of the thumbnail using the offset entry
            fseek($filehnd, $Tiff_offset + $OutputArray[0x88]['Data'][0]);
            // Read the thumbnail data, and replace the offset data with the thumbnail
            $OutputArray[0x88]['Data'] = network_safe_fread($filehnd, $OutputArray[0x89]['Data'][0]);
            // Sometimes the minolta thumbnail data is empty (or the offset is corrupt, which results in the same thing)
            // Check if the thumbnail data exists
            if ($OutputArray[0x88]['Data'] != "") {
                // Thumbnail exists
                // Minolta Thumbnails are missing their first 0xFF for some reason,
                // which is replaced with some weird character, so fix this
                $OutputArray[0x88]['Data'][0] = "ÿ";
            } else {
                // Thumbnail doesnt exist - make it obvious
                $OutputArray[0x88]['Data'] = FALSE;
            }
        }
    }
    // Return the array of IFD entries and the offset to the next IFD
    return array($OutputArray, $Next_Offset);
}