PHP从图像扩展名中删除参数 [英] PHP Remove parameters from image extension
问题描述
我从一个函数获取图像URL.我需要找到该图像的扩展名.有时,图片网址带有类似" http://slimages.macys.com/is/image/MCY/products/4/optimized/1776484_fpx.tif ?$ filterlrg $& wid = 370'.因此,文件扩展名类似于"tif?$ filterlrg $& wid = 370".如何获得确切的扩展名.
I am getting the image url from one function. I need to find the extension for the image. Sometimes the image url comes with parameters like 'http://slimages.macys.com/is/image/MCY/products/4/optimized/1776484_fpx.tif?$filterlrg$&wid=370'. So, the file extension comes like 'tif?$filterlrg$&wid=370'. How can I get the exact extension.
下面是我的代码
<?php
$srcimg = 'http://slimages.macys.com/is/image/MCY/products/4/optimized/1776484_fpx.tif?$filterlrg$&wid=370';
$fullpath = basename($srcimg);
$userImageDetails = pathinfo($fullpath);
$extension = strtolower($userImageDetails['extension']);
echo $extension;
?>
推荐答案
您可以使用此答案
$link = 'http://slimages.macys.com/is/image/MCY/products/4/optimized/1776484_fpx.tif?$filterlrg$&wid=370';
if ($url = parse_url($link)) {
echo pathinfo($url['path'], PATHINFO_EXTENSION);
}
输出
tif
没有第二个参数,parse_url
返回一个关联数组,但是如果只需要路径,则可以传递第二个参数parse_url($link, PHP_URL_PATH)
,它将返回一个单独的变量.
with no second argument, parse_url
returns an associative array but if you only want the path, you can pass a second argument parse_url($link, PHP_URL_PATH)
and it will return a single variable instead.
if ($path = parse_url($link, PHP_URL_PATH)) {
echo pathinfo($path, PATHINFO_EXTENSION);
}
这篇关于PHP从图像扩展名中删除参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!