Jasbir Rana
Jasbir Rana

Reputation: 277

How can i get image name and image extension from image url

I have a image url with query strings. I just want to get image name and its extension in php.

https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale

Upvotes: 4

Views: 1692

Answers (3)

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

use php pathinfo() function to get the details http://www.php.net/manual/en/function.pathinfo.php

$fileParts = pathinfo("https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale");
echo"<pre>";    
print_r($fileParts);
$extension=explode("?",$fileParts ['extension'])[0];

Output

Array
(
    [dirname] => https://static01.nyt.com/images/2018/08/28/us/28vote_print
    [basename] => 28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale
    [extension] => jpg?quality=75&auto=webp&disable=upscale
    [filename] => 28vote_xp-articleLarge
)

Upvotes: 1

AymDev
AymDev

Reputation: 7554

You can use pathinfo() and parse_url():

$url = 'https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale';

// Getting the name
$name = pathinfo(parse_url($url)['path'], PATHINFO_FILENAME);

// Getting the extension
$ext = pathinfo(parse_url($url)['path'], PATHINFO_EXTENSION);

// Output:
var_dump($name);    // 28vote_xp-articleLarge
var_dump($ext);     // jpg

Upvotes: 3

Ketan Yekale
Ketan Yekale

Reputation: 2223

Refer:

http://php.net/manual/en/function.parse-url.php

http://php.net/manual/en/function.pathinfo.php

<?php

$parsedURL = parse_url('https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale');

$pathinfo = pathinfo($parsedURL['path']);

$filename = $pathinfo['filename'];
$extension = $pathinfo['extension'];

var_dump($filename, $extension);

Check the output: https://3v4l.org/vejBA

Upvotes: 0

Related Questions