It looks like you need to remove 'data:image/png;base64,' from the beginning of $fetch_photo before you pass it into decode_base64.
Update: Here's an updated script that improves upon how you were doing things. It passed cursory testing.
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use MIME::Base64;
my $arg = new CGI;
my $fetch_photo = $arg->param('imagedata');
my ($data, $base64) = split /,/, $fetch_photo;
my ($type) = $data =~ m!data:image/(\w+);base64!;
my $decoded = MIME::Base64::decode_base64($base64);
my $filename = int(rand(1000)) . '.' . $type;
open(my $file, '>', "img/$filename") or die 'err'; # 3 argument open
binmode $file;
print $file $decoded;
close($file);