When the image file ends, complete the file. jpg,. png,. gif. I tried to save the image from the network to the local device, and the file name kept the end of the original image. For example, when or. when the image file ends, make up. jpg,. jpeg,. png or. gif.
I tried to save the image from the network to the local, and the file name kept the end of the original image.
For example, image.jpg and logo.png
However, some images are suffixed with. jpg,. jpeg,. png, or. gif. jpg,. jpeg,. png, or. gif.
I used stripos, but encountered some problems, such as an address like this:
Http://pcdn.500px.net/5953805/d0dd841969187f47e8ad9157713949b4b95b3bda/4.jpg? 1333782904356
It contains. jpg at the end of the original file name. I want to save it locally. the file name is 4.jpg? 1333782904356. jpg. how can I make a correct decision?
PHP code
$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 }}
------ Solution --------------------
PHP code
$ Url = "http://pcdn.500px.net/5953805/d0dd841969187f47e8ad9157713949b4b95b3bda/4.jpg? 1333782904356 "; $ url_arr = parse_url ($ url); echo basename ($ url_arr ['path']);
------ Solution --------------------
PHP code
$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;