Disculpenme todos pero ya resolví. El error estaba aquí en estas líneas:
Código PHP:
$type = $_FILES['avatar']['type']; // aqui $type es igual a image/pjpeg
list($width,$height,$type,$atributes) = getimagesize($imagen); // aqui $type perdia su valor y tomaba 2
salu2