目錄
一、出现的问题
二、问题分析以及解决方案
首頁 後端開發 php教程 二、问题分析以及解决方案

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

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

【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

熱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

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

華為GT3 Pro和GT4的差異是什麼? 華為GT3 Pro和GT4的差異是什麼? Dec 29, 2023 pm 02:27 PM

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

修復:截圖工具在 Windows 11 中不起作用 修復:截圖工具在 Windows 11 中不起作用 Aug 24, 2023 am 09:48 AM

為什麼截圖工具在Windows11上不起作用了解問題的根本原因有助於找到正確的解決方案。以下是截圖工具可能無法正常工作的主要原因:對焦助手已開啟:這可以防止截圖工具開啟。應用程式損壞:如果截圖工具在啟動時崩潰,則可能已損壞。過時的圖形驅動程式:不相容的驅動程式可能會幹擾截圖工具。來自其他應用程式的干擾:其他正在運行的應用程式可能與截圖工具衝突。憑證已過期:升級過程中的錯誤可能會導致此issu簡單的解決方案這些適合大多數用戶,不需要任何特殊的技術知識。 1.更新視窗與Microsoft應用程式商店應用程

Vue中如何對圖片進行壓縮和格式轉換? Vue中如何對圖片進行壓縮和格式轉換? Aug 25, 2023 pm 11:06 PM

Vue中如何對圖片進行壓縮和格式轉換?在前端開發中,經常會遇到需要對圖片進行壓縮和格式轉換的需求。特別是在行動端的開發中,為了提高頁面載入速度和節省使用者流量,對圖片進行壓縮和格式轉換是很關鍵的。而在Vue框架中,我們可以透過一些工具庫來實現圖片的壓縮和格式轉換。使用compressor.js庫進行壓縮compressor.js是一款用於壓縮圖片的JavaS

html的width是什麼意思 html的width是什麼意思 Jun 03, 2021 pm 02:15 PM

在html5中,width的意思是寬度,width屬性定義元素內容區的寬度,在內容區外面可以增加內邊距、邊框和外邊距,只需要給元素設定「元素{width:數值}」即可。

如何修復無法連線到iPhone上的App Store錯誤 如何修復無法連線到iPhone上的App Store錯誤 Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步驟檢查蘋果的系統狀態:在深入研究複雜的解決方案之前,讓我們先從基礎知識開始。問題可能不在於您的設備;蘋果的伺服器可能會關閉。造訪Apple的系統狀態頁面,查看AppStore是否正常運作。如果有問題,您所能做的就是等待Apple修復它。檢查您的網路連接:確保您擁有穩定的網路連接,因為「無法連接到AppStore」問題有時可歸因於連接不良。嘗試在Wi-Fi和行動數據之間切換或重置網路設定(「常規」>「重置」>「重置網路設定」>設定)。更新您的iOS版本:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

聊聊Vue2為什麼能透過this存取各種選項中屬性 聊聊Vue2為什麼能透過this存取各種選項中屬性 Dec 08, 2022 pm 08:22 PM

這篇文章帶大家解讀vue原始碼,來介紹一下Vue2中為什麼可以使用 this 存取各種選項中的屬性,希望對大家有幫助!

Python怎麼對圖片進行resize、裁切、旋轉、翻轉 Python怎麼對圖片進行resize、裁切、旋轉、翻轉 May 10, 2023 am 10:43 AM

對圖片進行resize、裁剪、旋轉、翻轉首先我們的原始圖片是10張網上下載尺寸不一的圖片,如下:操作1:resize將圖片resize到相同尺寸(320,240)fromPILimportImageimporttorchvision.transformsastransforms#使用PIL庫讀入圖片並進行resizedefResizeImage():ifnotos.path.exists(rdir):os.makedirs(rdir)foriinrange(10):im=Image.open(d

See all articles