Open In App

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 similar 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:

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: 




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

Output:

.png
png

Program 2: 




<?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);
 
?>

Output:

Creates an image with name test.png

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

Article Tags :