Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

Get the Extension of the Image from URL

Hi E's, I need to get the extension of the image from a URL address (gif, png, jpg etc).
In practice if I have this address: http://example.com/image/image.gif I want the output $extension = gif.

How I get the image extension from the URL?

Regards, JC
Avatar of kaufmed
kaufmed
Flag of United States of America image

$ext = preg_match('/(?<=\.)[a-z]+/i', $input)
Correction:

$ext = preg_match('/(?<=\.)[a-z]+$/i', $input)
ASKER CERTIFIED SOLUTION
Avatar of shanikawm
shanikawm
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>  A flawless mode:

What about  ".jpeg"?
What about:

$x = explode(".",$path);
$ext = array_pop($x);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Right
substr($path, (strlen($path)-3), strlen($path));

It´s flawless for most common extensions, jpeg ext is very rare in web apps
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>  It´s flawless for most common extensions

It won't cover Google's new WEBP extension either  ;)
Tthe proposed code by kumaranmca will only work if your file name has only one "." in it.  you need to grab the end of the array to be sure you get the extension. A filename like:

Jim.Smith.jpg

would not give the desired results by taking $file_extension[1].
Avatar of Pedro Chagas

ASKER

Hi to all, first thanks for participation on this question.

@kaufmed, your solution in post 33827856, the return is allways "1":
======================================================
$input = "http://portugal.montranet.com/portugal/palacioestoi/8.jpg";
$ext = preg_match('/(?<=\.)[a-z]+$/i', $input);
echo $ext;
======================================================

@shanikawm, your both codes working very well.
@betopa, your code in post 33828117 work well too
@smadeira, your both codes work well too.
@kumaranmca, your solution work too.

Please don't post more solutions. @kaufmed you can give me a new solution (I appreciate the fact you send solution by mobile phone)!

Regards, JC
The bug for close questions in mod multiple solutions persists:
I want close the question and assign the points:
33828045 100 points
33828069 75 "
33828117 50 "
33828189 50 "
33829752 25 "
Total = 300 points
My pattern was correct; my use of preg_match was not. Forgive me, I'm not a php programmer :\

The correct usage is:
$input = "http://portugal.montranet.com/portugal/palacioestoi/8.jpg";
preg_match('/(?<=\.)[a-z]+$/i', $input, $ext);
echo $ext[0];

Open in new window

Hi @kaufmed, your solution working too. Unfortunately I have been close the question before you post your correct solution, so I cant assign points to you.
Thanks.
JC
>>  Unfortunately I have been close the question before you post your correct solution, so I cant assign points to you.

It's cool. I knew that before I posted. I added it for the benefit of future EE readers  :)