I have written this PHP function which will flip an image (horizontally, like looking in a mirror). It reads the file, flips it, and writes it to a new filed called filename_flipped.ext. I would like to refactor this to the smallest possible size, but maintaining human readability.  Here is the function:  function flip_image($filepath){    if(file_exists($filepath)):        //Array ( [dirname] => images         //        [basename] => pig.png         //        [extension] => png         //        [filename] => pig         //        [filesize] => 72 )        $file = $this->get_file_info($filepath); // Returns above array        // Is it allowed in our extensions array        if(in_array($file['extension'], $this->allowed_exts)):            // Reference our Image for the correct extension            switch ($file['extension']) {                case 'png':                    $img = imagecreatefrompng($filepath);                    break;                case 'gif':                    $img = imagecreatefromgif($filepath);                    break;                default: // Has to be JPG, or JPEG                    $img = imagecreatefromjpeg($filepath);                    break;            }            // Now we have our image...            // Image Width & Height            $size_x = imagesx($img);            $size_y = imagesy($img);            // Create Temp Image            $temp = imagecreatetruecolor($size_x, $size_y);            // Define a colour as transparant, allocate the colour, resample it            imagecolortransparent($temp, imagecolorallocate($temp, 0, 0, 0));            imagealphablending($temp, false);            imagesavealpha($temp, true);            $x = imagecopyresampled($temp, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y);            if ($x) {                $img = $temp;            }            else {                die('Unable to flip image');            }            $writepath = $file['dirname'] . / . $file['filename'] . _flipped . .;            // Write our file            switch ($file['extension']) {                case 'png':                    imagepng($img, $writepath.$file['extension']);                    break;                case 'gif':                    imagegif($img, $writepath.$file['extension']);                    break;                default: // Has to be PNG                    imagejpeg($img, $writepath.$file['extension']);                    break;            }            $flipped_path = $writepath.$file['extension'];            imagedestroy($img);            return $flipped_path;        else:            echo 'Incorrect File Type, Not an Image, .png .gif, jpeg. jpeg Only.';            return false;        endif;    else:        echo 'No Such file found, Check the file path';        return false;    endif;}
Help to Refactor PHP Flip Image Code to smallest size as possible
php
You could use call_user_func to check the extension only once. Something like this (I haven't got the time even to read the code now) function flip_image($filepath){if(file_exists($filepath)):    //Array ( [dirname] => images     //        [basename] => pig.png     //        [extension] => png     //        [filename] => pig     //        [filesize] => 72 )    $file = $this->get_file_info($filepath); // Returns above array    // Is it allowed in our extensions array    if(in_array($file['extension'], $this->allowed_exts)):         // Reference our Image for the correct extension        $outfile = $file['dirname'] . / . $file['filename'] . _flipped . . . $file['extension'];        switch ($file['extension']) {            case 'png':                return flip_image_in($filepath, $outfile, 'imagecreatefrompng', 'imagepng');            case 'gif':                return flip_image_in($filepath, $outfile, 'imagecreatefromgif', 'imagegif');            default: // Has to be JPG, or JPEG                return flip_image_in($filepath, $outfile, 'imagecreatefromjpeg', 'imagejpeg');        }    else:        echo 'Incorrect File Type, Not an Image, .png .gif, jpeg. jpeg Only.';        return false;    endif;else:    echo 'No Such file found, Check the file path';    return false;endif;}function flip_image_in($inpath, $outfile, $create_function, $write_function){        $img = call_user_func($create_function, $inpath);        // Now we have our image...        // Image Width & Height        $size_x = imagesx($img);        $size_y = imagesy($img);        // Create Temp Image        $temp = imagecreatetruecolor($size_x, $size_y);        // Define a colour as transparant, allocate the colour, resample it        imagecolortransparent($temp, imagecolorallocate($temp, 0, 0, 0));        imagealphablending($temp, false);        imagesavealpha($temp, true);        $x = imagecopyresampled($temp, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y);        if ($x) {            $img = $temp;        }        else {            die('Unable to flip image');        }        call_user_func($write_function, $img, $outfile);        imagedestroy($img);        return $outfile;}