PHP | image_type_to_extension() Function

The image_type_to_extension() function is an inbuilt function in PHP which is used to get the file extension for an image type. This function can be found in any PHP version silimar or greater than 5.2.0.

Syntax:

string image_type_to_extension( int $imagetype, bool $include_dot )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $imagetype: It takes an integer value as first parameter that is one of the IMAGETYPE_XXX constant. For example: IMAGETYPE_GIF, IMAGETYPE_JPEG etc.
  • $include_dot: The second parameter takes a boolean value to decide whether to prepend a dot to the extension or not. The default value is set to TRUE in the function.

Return Value: This function returns a string value associated with the extension corresponding to the given image type.

Below programs illustrate the image_type_to_extension() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Extension with dot
echo image_type_to_extension(IMAGETYPE_PNG, TRUE) . "\n";
  
// Extension without dot
echo image_type_to_extension(IMAGETYPE_PNG, FALSE);
  
?>

chevron_right


Output:

.png
png

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create image instance
$image = imagecreatetruecolor(100, 100);
   
// Creates an image with .png extension
imagepng($image, './test'
        image_type_to_extension(IMAGETYPE_PNG));
  
// Free any memory associated with image
imagedestroy($image);
  
?>

chevron_right


Output:

Creates an image with name test.png

Reference: https://www.php.net/manual/en/function.image-type-to-extension.php

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.