国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
一、出現(xiàn)的問題
二、問題分析以及解決方案
Home Backend Development PHP Tutorial 二、問題分析以及解決方案

二、問題分析以及解決方案

Jun 13, 2016 pm 12:02 PM
gt height resize this width

【PHP縮略圖類】手機照片不能生成縮略圖問題以及解決方案

本文原創(chuàng),謝絕轉(zhuǎn)載

一、出現(xiàn)的問題

這幾天做了手機上傳照片并裁出縮略圖的接口的測試,發(fā)現(xiàn)不管怎么,生成的縮略圖都是一片漆黑。:-(

然后就把這個縮略圖類單拿出來進行測試,發(fā)現(xiàn)只要是手機拍出來的照片都不能進行縮略圖的處理。。。。


二、問題分析以及解決方案


經(jīng)過群里的請教,發(fā)現(xiàn)問題可能是出現(xiàn)在文件的類型的判斷上,因為png圖片自帶一個透明的圖層,導(dǎo)致不能直接轉(zhuǎn)換成jpg的文件,而手機排出的照片擴展名是jpg.

所以,得出的結(jié)論是手機拍出的是jpg擴展名的png圖片。


由于擴展名是可以隨意修改的,不是很能保證文件的信息的準確性,所以我們采用了 getimagesize 函數(shù)進行文件類型的獲取。


//獲取真實的圖片類型 list($width, $height, $type, $attr) = getimagesize($this->sur_file);    switch($type) {          case 1 :              $img_type = 'gif';              break;          case 2 :              $img_type = 'jpeg';              break;          case 3 :              $img_type = 'png';              break;          case 15 :              $img_type = 'wbmp';              break;          default :              return false;      }  


三、生成縮略圖類


下面把修改后的生成縮略圖的類貼出來,供大家指正~


/** * php生成縮略圖類 * 修改者 點點細雨  * 文章出處 : http://blog.csdn.net/diandianxiyu_geek * 2014-07-23 解決了圖片類型不能正常識別的問題 */class thumb {    public $sur_file; //讀取的原圖片    public $des_file; //生成目標圖片    public $tem_file; //臨時圖片    public $tag;  //縮略標簽  0,默認,按固定的高寬生成  1,按比列或固定最大長度生成  -1,按某個寬度或某個高度縮小    public $resize_width;  //$tag為0時,目標文件寬    public $resize_height;  //$tag為0時,目標文件高    public $sca_max; //$tag為1時,1時為最大長度(高或?qū)捴械淖畲笾?    public $type;  //圖片類型    public $width;  //原圖片寬    public $height;  //原圖片高    public $size;     //原圖大小    //構(gòu)造函數(shù)    public function __construct($surpic, $reswid, $reshei, $despic, $mark, $scamax) {        $this->sur_file = $surpic;        $this->resize_width = $reswid;        $this->resize_height = $reshei;        $this->tag = $mark;        $this->sca_max = $scamax;        list($width, $height, $type, $attr) = getimagesize($this->sur_file);        switch ($type) {            case 1 :                $img_type = 'gif';                break;            case 2 :                $img_type = 'jpeg';                break;            case 3 :                $img_type = 'png';                break;            case 15 :                $img_type = 'wbmp';                break;            default :                return false;        }        $this->type = $img_type; //獲取圖片類型        $this->init_img(); //初始化圖片        $this->des_file = $despic; //目標圖片地址        $this->width = $width;        $this->height = $height;        $this->size = filesize($surpic);        $this->new_img();        imagedestroy($this->tem_file);    }    //圖片初始化函數(shù)    private function init_img() {        if ($this->type == 'jpeg') {            $this->tem_file = imagecreatefromjpeg($this->sur_file);        } elseif ($this->type == 'jpg') {            $this->tem_file = imagecreatefromjpeg($this->sur_file);        } elseif ($this->type == 'gif') {            $this->tem_file = imagecreatefromgif($this->sur_file);        } elseif ($this->type == 'png') {            $this->tem_file = imagecreatefrompng($this->sur_file);        } elseif ($this->type == 'bmp') {            $this->tem_file = imagecreatefrombmp($this->sur_file); //bmp.php中包含        }    }    //圖片生成函數(shù)    private function new_img() {        $ratio = ($this->width) / ($this->height); //原圖比例        $resize_ratio = ($this->resize_width) / ($this->resize_height); //縮略后比例        $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); //生成新圖片        imagealphablending($newimg, false); //這里很重要,意思是不合并顏色,直接用$img圖像顏色替換,包括透明色;        imagesavealpha($newimg, true);        if ($this->tag == 0) { //按固定高寬截取縮略圖            $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); //生成新圖片            if ($ratio >= $resize_ratio) {//即等比例下,縮略圖的高比原圖長,因此高不變                imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, $this->resize_height, (($this->height) * $resize_ratio), $this->height);            } elseif ($ratio tem_file, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width) / $resize_ratio));            }        } elseif ($this->tag == 1) { //按固定比例或最大長度縮小            if ($this->sca_max width) * ($this->sca_max)), (($this->height) * ($this->sca_max))); //生成新圖片                imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, (($this->width) * ($this->sca_max)), (($this->height) * ($this->sca_max)), $this->width, $this->height);            } elseif ($this->sca_max > 1) { //按某個最大長度縮小                if ($ratio >= 1) { //寬比高長                    $newimg = imagecreatetruecolor($this->sca_max, ($this->sca_max / $ratio)); //生成新圖片                    imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->sca_max, ($this->sca_max / $ratio), $this->width, $this->height);                } else {                    $newimg = imagecreatetruecolor(($this->sca_max * $ratio), $this->sca_max); //生成新圖片                    imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->sca_max * $ratio), $this->sca_max, $this->width, $this->height);                }            }        } elseif ($this->tag == -1) { //按某個寬度或某個高度縮小            if ($resize_ratio >= 1) {//新高小于新寬,則圖片按新寬度縮小                $newimg = imagecreatetruecolor($this->resize_width, ($this->resize_width / $ratio)); //生成新圖片                imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, ($this->resize_width / $ratio), $this->width, $this->height);            } elseif ($resize_ratio resize_height * $ratio), $this->resize_height); //生成新圖片                imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->resize_height * $ratio), $this->resize_height, $this->width, $this->height);            }        }        //輸出新圖片        if ($this->type == 'jpeg' || $this->type == 'jpg') {            imagejpeg($newimg, $this->des_file);        } elseif ($this->type == 'gif') {            imagegif($newimg, $this->des_file);        } elseif ($this->type == 'png') {            imagepng($newimg, $this->des_file);        } elseif ($this->type == 'bmp') {            imagebmp($newimg, $this->des_file); //bmp.php中包含        }    }#function new_img() end}




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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to compress and format images in Vue? How to compress and format images in Vue? Aug 25, 2023 pm 11:06 PM

How to compress and format images in Vue? In front-end development, we often encounter the need to compress and format images. Especially in mobile development, in order to improve page loading speed and save user traffic, it is critical to compress and format images. In the Vue framework, we can use some tool libraries to compress and format images. Compression using the compressor.js library compressor.js is a JavaS for compressing images

What does the width of html mean? What does the width of html mean? Jun 03, 2021 pm 02:15 PM

In HTML5, width means width. The width attribute defines the width of the element's content area. You can add inner margins, borders, and outer margins outside the content area. You only need to set "element {width: value}" to the element.

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表單通過后,彈出的對話框怎樣在當前頁彈出,該如何解決 php提交表單通過后,彈出的對話框怎樣在當前頁彈出,該如何解決 Jun 13, 2016 am 10:23 AM

php提交表單通過后,彈出的對話框怎樣在當前頁彈出php提交表單通過后,彈出的對話框怎樣在當前頁彈出而不是在空白頁彈出?想實現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗證用PHP在后端,那么就用Ajax;僅供參考:HTML code

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

See all articles