Determine the end of a picture file, complete a. jpg,. jpeg,. png, or. gif
I try to save the picture locally from the network, and the file name retains the end of the original picture.
such as: Image.jpg, logo.png
But some picture files end up not. jpg,. jpeg,. png, or. gif, need to complement. 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
Its original file name at the end of the section contains. jpg, I want to save to the local, the file name is 4.jpg?1333782904356.jpg, how to correctly judge?
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 ', don't 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 ', does 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);p reg_match ('/^ (jpg| jpeg|png|gif) {1}\.*/i ', $ar [1], $match); $newFileName = $ar [0]. '. '. $match [1];echo $newFileName;