搜尋
首頁後端開發php教程二、问题分析以及解决方案

【PHP缩略图类】手机照片不能生成缩略图问题以及解决方案

本文原创,谢绝转载

一、出现的问题

这几天做了手机上传照片并裁出缩略图的接口的测试,发现不管怎么,生成的缩略图都是一片漆黑。:-(

然后就把这个缩略图类单拿出来进行测试,发现只要是手机拍出来的照片都不能进行缩略图的处理。。。。


二、问题分析以及解决方案


经过群里的请教,发现问题可能是出现在文件的类型的判断上,因为png图片自带一个透明的图层,导致不能直接转换成jpg的文件,而手机排出的照片扩展名是jpg.

所以,得出的结论是手机拍出的是jpg扩展名的png图片


由于扩展名是可以随意修改的,不是很能保证文件的信息的准确性,所以我们采用了 getimagesize 函数进行文件类型的获取。


//获取真实的图片类型 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时为最大长度(高或宽之中的最大值)    public $type;  //图片类型    public $width;  //原图片宽    public $height;  //原图片高    public $size;     //原图大小    //构造函数    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);    }    //图片初始化函数    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中包含        }    }    //图片生成函数    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}




陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP中的依賴注入:避免常見的陷阱PHP中的依賴注入:避免常見的陷阱May 16, 2025 am 12:17 AM

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

如何加快PHP網站:性能調整如何加快PHP網站:性能調整May 16, 2025 am 12:12 AM

到Improveyourphpwebsite的實力,UsEthestertate:1)emplastOpCodeCachingWithOpcachetCachetOspeedUpScriptInterpretation.2)優化的atabasequesquesquesquelies berselectingOnlynlynnellynnessaryfields.3)usecachingsystemssslikeremememememcachedisemcachedtoredtoredtoredsatabaseloadch.4)

通過PHP發送大規模電子郵件:有可能嗎?通過PHP發送大規模電子郵件:有可能嗎?May 16, 2025 am 12:10 AM

是的,ItispossibletosendMassemailswithp.1)uselibrarieslikeLikePhpMailerorSwiftMailerForeffitedEmailsending.2)enasledeLaysBetenemailstoavoidSpamflagssspamflags.3))

PHP中依賴注入的目的是什麼?PHP中依賴注入的目的是什麼?May 16, 2025 am 12:10 AM

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

如何使用PHP發送電子郵件?如何使用PHP發送電子郵件?May 16, 2025 am 12:03 AM

使用PHP發送電子郵件的最佳方法包括:1.使用PHP的mail()函數進行基本發送;2.使用PHPMailer庫發送更複雜的HTML郵件;3.使用SendGrid等事務性郵件服務提高可靠性和分析能力。通過這些方法,可以確保郵件不僅到達收件箱,還能吸引收件人。

如何計算PHP多維數組的元素總數?如何計算PHP多維數組的元素總數?May 15, 2025 pm 09:00 PM

計算PHP多維數組的元素總數可以使用遞歸或迭代方法。 1.遞歸方法通過遍歷數組並遞歸處理嵌套數組來計數。 2.迭代方法使用棧來模擬遞歸,避免深度問題。 3.array_walk_recursive函數也能實現,但需手動計數。

PHP中do-while循環有什麼特點?PHP中do-while循環有什麼特點?May 15, 2025 pm 08:57 PM

在PHP中,do-while循環的特點是保證循環體至少執行一次,然後再根據條件決定是否繼續循環。 1)它在條件檢查之前執行循環體,適合需要確保操作至少執行一次的場景,如用戶輸入驗證和菜單系統。 2)然而,do-while循環的語法可能導致新手困惑,且可能增加不必要的性能開銷。

PHP中如何哈希字符串?PHP中如何哈希字符串?May 15, 2025 pm 08:54 PM

在PHP中高效地哈希字符串可以使用以下方法:1.使用md5函數進行快速哈希,但不適合密碼存儲。 2.使用sha256函數提高安全性。 3.使用password_hash函數處理密碼,提供最高安全性和便捷性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

北端:融合系統,解釋
1 個月前By尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
4 週前By尊渡假赌尊渡假赌尊渡假赌
<🎜>掩蓋:探險33-如何獲得完美的色度催化劑
2 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具