判断图片文件结尾,补全.jpg, .jpeg, .png or .gif
我尝试从网络保存图片到本地,文件名保留原图片的结尾部分。
如:image.jpg, logo.png
但是有些图片文件结尾不是.jpg, .jpeg, .png or .gif的,需要补全.jpg, .jpeg, .png or .gif。
我用了stripos,但是遇到一些问题,比如像这样一个地址:
http://pcdn.500px.net/5953805/d0dd841969187f47e8ad9157713949b4b95b3bda/4.jpg?1333782904356
它原来的文件名结尾部分含有 .jpg,我想保存到本地,文件名为 4.jpg?1333782904356.jpg,如何正确判断?
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$webimage = 'http://pcdn.500px.net/5953805/d0dd841969187f47e8ad9157713949b4b95b3bda/4.jpg?1333782904356';$pieces = explode("/", $webimage); $pathend = end($pieces);$imageinfo = @getimagesize($webimage);$imagetype= $imageinfo['mime'];if($imagetype=='image/jpeg'){ if(stripos($pathend,'.jpg')==false){ $newpathend = $pathend.'.jpg'; // if image end is't '.jpg', add '.jpg' }else if(stripos($pathend,'.jpeg')==0){ $newpathend = $pathend.'.jpeg'; // if image end is't '.jpg', add '.jpeg' }else{ $newpathend = $pathend;// if image end is '.jpg' or '.jpeg', do not change }}if($imagetype=='image/png'){ if(stripos($pathend,'.png')==false){ $newpathend = $pathend.'.png'; // if image end is't '.png', add '.png' }else{ $newpathend = $pathend;// if image end is '.png', do not change }}if($imagetype=='image/gif'){ if(stripos($pathend,'.gif')==false){ $newpathend = $pathend.'.gif'; // if image end is't '.gif', add '.gif' }else{ $newpathend = $pathend;// if image end is '.gif', do not change }}
$url = "http://pcdn.500px.net/5953805/d0dd841969187f47e8ad9157713949b4b95b3bda/4.jpg?1333782904356";$url_arr = parse_url($url);echo basename($url_arr['path']);<br><font color="#e78608">------解决方案--------------------</font><br>
$webimage = 'http://pcdn.500px.net/5953805/d0dd841969187f47e8ad9157713949b4b95b3bda/4.jpg?1333782904356';$pieces = explode("/", $webimage);$fileName = end($pieces);$ar = explode('.', $fileName);preg_match('/^(jpg|jpeg|png|gif){1}\.*/i', $ar[1], $match);$newFileName = $ar[0].'.'.$match[1];echo $newFileName;<div class="clear"> </div>