Home Backend Development PHP Tutorial 判断图片文件结尾,补全.jpg, .jpeg, .png or .gif,该如何处理

判断图片文件结尾,补全.jpg, .jpeg, .png or .gif,该如何处理

Jun 13, 2016 pm 01:38 PM
image jpg

判断图片文件结尾,补全.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,如何正确判断?

PHP code
<!--

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
    }
}

Copy after login


------解决方案--------------------
PHP code
$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>
Copy after login
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; <div class="clear">
                 
              
              
        
            </div>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to convert PNG to JPG on Windows 11 How to convert PNG to JPG on Windows 11 May 12, 2023 pm 03:55 PM

How to Convert PNG to JPG on Windows 11 On Windows 10 and 11, you can use Microsoft's built-in Paint app to quickly convert image files. To convert a PNG image to JPG on Windows 11, use the following steps: Open File Explorer and navigate to the PNG image you want to convert. Right-click the image and select Open With > Draw from the menu. Your photo or image opens in the Paint app. Note the file size at the bottom of the screen. To convert a file from PNG to JPG, click File and select Save As > JPEG Image from the menu. When the file resource

Fix: Can't open JPG files in Windows 11 Fix: Can't open JPG files in Windows 11 Apr 17, 2023 pm 12:37 PM

JPG is one of the most popular image file formats along with PNG. This is a lossy compressed image format ideal for websites. Therefore, many users save or convert images to JPG format. However, some users have stated that they are having issues opening JPG files in Windows 11 using the platform’s default Photos app. Therefore, these users cannot open, view and edit JPG format images in Windows 11. This is what one user said in a Microsoft forum post: When double-clicking a JPG file to open in Microsoft Photo, I receive the error The specified program cannot be found. This was purchased new about a month ago

Let's talk about how to use php to easily convert WebP to JPG. Let's talk about how to use php to easily convert WebP to JPG. Mar 24, 2023 pm 02:57 PM

In recent years, with the continuous development of Web technology, WebP, a new image format, has gradually entered the field of vision of programmers and Web developers. The advantages of WebP cannot be ignored: small file size, high quality, high browser support, etc. But in some cases, we may need to convert WebP images into some other formats, such as JPG. So, how to use PHP to convert WebP to JPG?

What format is jpg? What format is jpg? Jun 21, 2023 am 11:36 AM

JPG is a JPEG format file. The JPEG format is the most commonly used image file format, with the suffix ".jpg" or ".jpeg". JPG format standards are divided into three types: JPEG, progressive JPEG, and JPEG2000. It is a standard generally used for continuous-tone static image compression. It is lossy compression, which compresses data images in a small storage space while causing loss of image data. Damage, if the user has higher requirements for image quality, it is recommended to abandon this image file format, especially for some high-definition pictures.

What does jpg mean? What does jpg mean? Nov 28, 2022 am 09:46 AM

JPG generally refers to the JPEG format, which is an image format saved using a lossy compression method. It is a standard for continuous tone static image compression. Its file suffix is ​​".jpg" or ".jpeg". JPG files mainly use a joint coding method of predictive coding (DPCM), discrete cosine transform (DCT) and entropy coding to remove redundant images and color data; it can compress images in a small storage space, but to a certain extent will cause damage to the image data.

How to use Bing Image Creator for free How to use Bing Image Creator for free Feb 27, 2024 am 11:04 AM

This article will introduce seven ways to get high-quality output using the free BingImageCreator. BingImageCreator (now known as ImageCreator for Microsoft Designer) is one of the great online artificial intelligence art generators. It generates highly realistic visual effects based on user prompts. The more specific, clear, and creative your prompts are, the better the results will be. BingImageCreator has made significant progress in creating high-quality images. It now uses Dall-E3 training mode, showing a higher level of detail and realism. However, its ability to consistently produce HD results depends on several factors, including fast

How to convert svg to jpg format How to convert svg to jpg format Nov 24, 2023 am 09:50 AM

svg can be converted to jpg format by using image processing software, using online conversion tools, and using the Python image processing library. Detailed introduction: 1. Image processing software includes Adobe Illustrator, Inkscape and GIMP; 2. Online conversion tools include CloudConvert, Zamzar, Online Convert, etc.; 3. Python image processing library, etc.

How to delete images from Xiaomi phones How to delete images from Xiaomi phones Mar 02, 2024 pm 05:34 PM

How to delete images on Xiaomi mobile phones? You can delete images on Xiaomi mobile phones, but most users don’t know how to delete images. Next is the tutorial on how to delete images on Xiaomi mobile phones brought by the editor. Interested users can come and join us. Let's see! How to delete images on Xiaomi mobile phone 1. First open the [Album] function in Xiaomi mobile phone; 2. Then check the unnecessary pictures and click the [Delete] button in the lower right corner; 3. Then click [Album] at the top to enter the special area , select [Recycle Bin]; 4. Then directly click [Empty Recycle Bin] as shown in the figure below; 5. Finally, directly click [Permanent Delete] to complete.

See all articles