File Conversion

This function converts an image from any format to any format.
<?php

function image_convert($file, $targetType){
   // Obtain the file into memory
   $last3 = strtolower(substr($file, -3));
   $last4 = strtolower(substr($file, -4));
   $r = ($last3=='gif')?imagecreatefromgif($file):
          ($last3=='png')?imagecreatefrompng($file):
          ($last3=='jpg'||$last4=='jpeg')?imagecreatefromjpeg($file):
          NULL;
   if (!r) die('Invalid source file');
   
   // Output the file
   $filebase = ($last4=='jpeg')?substr($file,0,-4):substr($file,0,-3);
   $targetType = strtolower($targetType);
   if ($targetType=='gif') imagegif($r,$filebase."gif");
   else if ($targetType=='png') imagepng($r,$filebase."png");
   else if ($targetType=='jpg') imagejpeg($r,$filebase."jpg");
   else if ($targetType=='jpeg')imagejpeg($r,$filebase."jpeg");
   
   // Free the file from memory
   imagedestroy($r);
}

image_convert("testing.jpeg","png");

?>