How to deal with the final and final completion of an image file, such as. jpg,. jpg,. png or. gif?

Source: Internet
Author: User
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;

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.