PHP how to remove iptc from images without using GD

Discussion in 'Programming' started by EricBruggema, Oct 10, 2023.

  1. #1
    Is there anyway to remove the iptc tags from an image without recreating the image with GD?

    the examples on php.net doesn't work, and i cannot use external programs.

    Please help
     
    Solved! View solution.
    EricBruggema, Oct 10, 2023 IP
  2. hanoidesign

    hanoidesign Well-Known Member

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    108
    #2
    If your server has Imagick you can use it:

    https://stackoverflow.com/questions/3614925/remove-exif-data-from-jpg-using-php
     
    hanoidesign, Oct 11, 2023 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    EricBruggema, Oct 11, 2023 IP
  4. hanoidesign

    hanoidesign Well-Known Member

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    108
    #4
    Have you tried xtempore comment in that link? It uses no external library.

    /**
    * Remove EXIF from a JPEG file.
    * @param string $old Path to original jpeg file (input).
    * @param string $new Path to new jpeg file (output).
    */
    function removeExif($old, $new)
    {
        // Open the input file for binary reading
        $f1 = fopen($old, 'rb');
        // Open the output file for binary writing
        $f2 = fopen($new, 'wb');
    
        // Find EXIF marker
        while (($s = fread($f1, 2))) {
            $word = unpack('ni', $s)['i'];
            if ($word == 0xFFE1) {
                // Read length (includes the word used for the length)
                $s = fread($f1, 2);
                $len = unpack('ni', $s)['i'];
                // Skip the EXIF info
                fread($f1, $len - 2);
                break;
            } else {
                fwrite($f2, $s, 2);
            }
        }
    
        // Write the rest of the file
        while (($s = fread($f1, 4096))) {
            fwrite($f2, $s, strlen($s));
        }
    
        fclose($f1);
        fclose($f2);
    }
    PHP:
     
    hanoidesign, Oct 13, 2023 IP
    EricBruggema likes this.
  5. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #5
    Yes and that didn't work as it tries to remove EXIF information, not the IPTC data wich i use and want to remove.
     
    EricBruggema, Oct 14, 2023 IP
  6. #6
    As the answer in this:
    https://stackoverflow.com/questions/5833026/extract-iptc-information-from-jpeg-using-javascript

    0xFFED seems to be IPTC marker. Perhaps you could try that replacing EXIF marker (0xFFE1) in the above function to see if that works?
     
    hanoidesign, Oct 14, 2023 IP
    EricBruggema likes this.
  7. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #7
    Thank you so much, this worked like a charm! It removed 200 bytes from an image, and that part contained the IPTC data!
    Again, thanks, you made my day!!

    -BTW i couldn't set your answer as best answer as it contained two posts ;-)
     
    EricBruggema, Oct 14, 2023 IP
  8. hanoidesign

    hanoidesign Well-Known Member

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    108
    #8
    Awesome! Glad that I can help ;-)
     
    hanoidesign, Oct 15, 2023 IP