Determine the end of the picture file, complete the. jpg,. jpeg,. png, or. gif, how to handle

Source: Internet
Author: User
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   
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.