Novis\Image\Image::extension
Novis\Image\Image::extension(Format $format): stringNovis\Image\Image::extension( Format $format, ): string
Returns the usual file extension of an image format, without the dot, for example jpg.
The argument is a Format. The result is a lower-case string you can put at the end of a file name. Format::Jpeg gives jpg. Every other case gives its own name in lower case: png, webp, gif, avif, jxl, svg and pdf.
To name a file that a user uploaded, read its format with Image::info and pass $info->format to extension. The extension then matches the bytes of the file. The name the user gave can be wrong, so it is not used.
extension reads no file and never throws an error.
Parameters
$formatFormat
Return value
Returns string.
Examples
Name a stored file
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to build the name of a stored image from its real format.use Novis\Image\Image;
// The user named this file "avatar.png", but the bytes are a JPEG file.tainted bytes $upload = Image::create(48, 48, Color::hex('#336699'))->withFormat(Format::Jpeg)->encode();
// The extension comes from the bytes. This prints avatar-42.jpg.string $name = 'avatar-42.' . Image::extension(Image::info($upload)->format);echo $name, "\n";avatar-42.jpgEvery format
<?nvsuse Novis\Image\Format;// Shows the file extension of every image format.use Novis\Image\Image;
array<Format> $formats = [ Format::Jpeg, Format::Png, Format::Webp, Format::Gif, Format::Avif, Format::Jxl, Format::Svg, Format::Pdf,];
// This prints one extension on each line. `Format::Jpeg` gives jpg.foreach ($formats as Format $format) { echo Image::extension($format), "\n";}jpgpngwebpgifavifjxlsvgpdfOne name per variant
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to name the files when one photo is saved in two formats.use Novis\Image\Image;
Image $photo = Image::create(64, 32, Color::hex('#669933'));array<Format> $formats = [Format::Webp, Format::Png];
// Each file gets the extension of its own format.// This prints product-7.webp and product-7.png.foreach ($formats as Format $format) { tainted bytes $file = $photo->withFormat($format)->encode(); echo 'product-7.', Image::extension(Image::info($file)->format), "\n";}product-7.webpproduct-7.png