What am I doing wrong?
while (my $zeichen = getc($handle1))
This will fail if the character you are reading is "0" or some other "false" value.
Try:
while (defined ( my $zeichen = getc($handle1) ) )
Also, although 'IEND' does signify the end marker of the PNG file it does so as part of a "chunk", and a chunk always ends with a CRC - from the
PNG specs:
CRC
A 4-byte CRC (Cyclic Redundancy Check) calculated on the preceding bytes in the chunk, including the chunk type code and chunk data fields, but not including the length field. The CRC is always present, even for chunks containing no data.
So you will need to read/write a further 4 bytes after 'IEND'. I'm no expert on PNG files and can't say this is the correct way to do it but adding these two lines worked for me:
if ($buffer =~ /IEND/) {
print $handle2 $buffer;
read($handle1, $buffer, $bytes); ### 1
print $handle2 $buffer; ### 2
exit;
}