Linux环境—JPEG/JPG/PNG图片转换WEBP格式(二)
生活随笔
收集整理的這篇文章主要介紹了
Linux环境—JPEG/JPG/PNG图片转换WEBP格式(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
PHP源碼編寫
<?php
/**
?*?Use :?將JPEG/JPG/PNG?的圖片轉(zhuǎn)換為?WEBP?格式
?*?User:?yKan_SF
?*?Date: 2018-2-27
?*?Time:?下午8:22
?*/
ini_set('display_errors',1);
class?imagick_convert
{
????//原始圖片絕對(duì)路徑
????private?$str_old_file?=?'';
????//轉(zhuǎn)換webp默認(rèn)擴(kuò)展名
????private?$str_default_ext?=?'webp';
????//支持轉(zhuǎn)換的圖片格式
????private?$arr_image_ext?=?null;
????/** ?????*?默認(rèn)構(gòu)造函數(shù) ?????*/ ????public?function?__construct($str_image_path) ????{ ????????$this->str_old_file?=?$str_image_path; ????}
????/** ?????*?Linux下ImageMagick?+?libwebp?+?php擴(kuò)展imagick?轉(zhuǎn)化?jpeg/jpg/png?圖片文件 ?????*?@param?string?$webp_dir?webp圖片的存儲(chǔ)絕對(duì)路徑 ?????*?@return?string??返回處理之后的圖片絕對(duì)路徑 ?????*/ ????public?function?convert_transform_images($webp_dir?=?'') ????{ ????????//文件是否存在 ????????if(!is_file($this->str_old_file)) ????????{ ? ? ? ? ? ? ???//WEBP格式轉(zhuǎn)換:待轉(zhuǎn)換的圖片路徑不正確 ????????????return?$this->str_old_file; ????????} ????????//圖片基本信息 ????????$ext?=?pathinfo($this->str_old_file,PATHINFO_EXTENSION); ????????$ext?=?empty($ext)???''?:?strtolower($ext); ????????//圖片質(zhì)量 ????????$this->arr_image_ext?=?array( ????????????'jpeg'?=>?Imagick::COMPRESSION_JPEG, ????????????'jpg'??=>?Imagick::COMPRESSION_JPEG, ????????????'png'??=>?Imagick::COMPRESSION_UNDEFINED, ????????); ????????//根據(jù)后綴名把jpg或者png轉(zhuǎn)成webp ????????if(!in_array($ext,?array_keys($this->arr_image_ext))) ????????{ ? ? ? ? ? ? ???//WEBP格式轉(zhuǎn)換:暫不支持該格式轉(zhuǎn)換,目前僅支持轉(zhuǎn)換?jpeg,jpg和png?格式 ????????????return?$this->str_old_file; ????????} ????????//默認(rèn)webp圖片新路徑 ????????if(empty($webp_dir)) ????????{ ????????????$webp_dir?=?rtrim($this->str_old_file,$ext)?.?$this->str_default_ext; ????????} ????????//開始轉(zhuǎn)換 ????????try ????????{ ????????????//原圖的質(zhì)量 ????????????$new_q?=?$this->get_img_quality($ext); ????????????//轉(zhuǎn)換成webp格式 ????????????$this->do_jpg_transform_webp($webp_dir,?$new_q); ????????} ????????catch(Exception?$e) ????????{ ? ? ? ? ? ? ???//WEBP格式轉(zhuǎn)換:工具轉(zhuǎn)換異常,切換至GD庫轉(zhuǎn)換 ????????????$webp_dir?=?$this->php_gd_image_webp($ext,$webp_dir); ????????} ????????//睡眠0.2秒 ????????usleep(20000); ????????if(is_file($webp_dir)) ????????{ ? ? ? ? ? ? ???//刪除原始圖片 + 設(shè)置WEBP圖片訪問權(quán)限 ????????????chmod($webp_dir,?0777); ????????????unlink($this->str_old_file); ? ? ? ? ? ? ???//WEBP格式轉(zhuǎn)換:轉(zhuǎn)換成功 ????????????return?$webp_dir; ????????} ? ? ? ? ??//WEBP格式轉(zhuǎn)換:轉(zhuǎn)換失敗,返回原始圖片路徑 ????????return?$this->str_old_file; ????}
????/** ?????*?獲取圖片的質(zhì)量 ?????*?@param?string?$str_ext?圖片擴(kuò)展名 ?????*?@return?int?圖片的質(zhì)量 ?????*/ ????private?function?get_img_quality($str_ext) ????{ ????????$resource?=?new?Imagick($this->str_old_file); ????????//png特殊處理 ????????if($str_ext?==?'png') ????????{ ????????????$resource->setImageFormat('PNG'); ????????} ????????$resource->setImageCompression($this->arr_image_ext[$str_ext]); ????????$current?=?$resource->getImageCompressionQuality(); ????????$resource->clear(); ????????$resource->destroy(); ????????if(!isset($current)?||?empty($current)) ????????{ ????????????$current?=?80; ????????} ????????$resource->clear(); ????????$resource->destroy(); ????????return?$current; ????}
????/** ?????*?jpg/jpeg/png格式轉(zhuǎn)換成webp格式 ?????*?@param?string?$webp_img_path?webp圖片的真實(shí)路徑 ?????*?@param?int?$int_q?圖片的壓縮質(zhì)量 ?????*/ ????private?function?do_jpg_transform_webp($webp_img_path,?$int_q?=?80) ????{ ????????exec("cwebp?-q?{$int_q}?{$this->str_old_file}?-o?{$webp_img_path}"); ????}
????/** ?????*?應(yīng)用PHP自身的GD庫重新生成一張?webp?格式的圖片,但是有概率生成空白圖(有風(fēng)險(xiǎn)) ?????*?@param?string?$ext??文件擴(kuò)展名 ?????*?@param?string?$webp_img_path??生成webp文件的絕對(duì)路徑 ?????*?@return?string?返回處理之后的圖片絕對(duì)路徑 ?????*/ ????private?function?php_gd_image_webp($ext,$webp_img_path) ????{ ????????//jpg處理使用jpeg ????????$ext?=?$ext?==?'jpg'???'jpeg'?:?$ext; ????????//拼接函數(shù)名?imagecreatefromjpeg?還是?imagecreatefrompng ????????$funName?=?'imagecreatefrom'?.?$ext; ????????//開始轉(zhuǎn)換 ????????$obj_img?=?null; ????????try ????????{ ????????????//打開這個(gè)圖片資源 ????????????$obj_img?=?$funName($this->str_old_file); ????????????//用這個(gè)圖片資源創(chuàng)建一個(gè)webp圖片,?存在路徑$tdir ????????????imagewebp($obj_img,$webp_img_path); ????????} ????????catch(Exception?$e) ????????{
????????} ????????//銷毀畫布資源 ????????if($obj_img?!=?null) ????????{ ????????????imagedestroy($obj_img); ????????} ????????//睡眠0.3秒 ????????return?$webp_img_path; ????} }?
????/** ?????*?默認(rèn)構(gòu)造函數(shù) ?????*/ ????public?function?__construct($str_image_path) ????{ ????????$this->str_old_file?=?$str_image_path; ????}
????/** ?????*?Linux下ImageMagick?+?libwebp?+?php擴(kuò)展imagick?轉(zhuǎn)化?jpeg/jpg/png?圖片文件 ?????*?@param?string?$webp_dir?webp圖片的存儲(chǔ)絕對(duì)路徑 ?????*?@return?string??返回處理之后的圖片絕對(duì)路徑 ?????*/ ????public?function?convert_transform_images($webp_dir?=?'') ????{ ????????//文件是否存在 ????????if(!is_file($this->str_old_file)) ????????{ ? ? ? ? ? ? ???//WEBP格式轉(zhuǎn)換:待轉(zhuǎn)換的圖片路徑不正確 ????????????return?$this->str_old_file; ????????} ????????//圖片基本信息 ????????$ext?=?pathinfo($this->str_old_file,PATHINFO_EXTENSION); ????????$ext?=?empty($ext)???''?:?strtolower($ext); ????????//圖片質(zhì)量 ????????$this->arr_image_ext?=?array( ????????????'jpeg'?=>?Imagick::COMPRESSION_JPEG, ????????????'jpg'??=>?Imagick::COMPRESSION_JPEG, ????????????'png'??=>?Imagick::COMPRESSION_UNDEFINED, ????????); ????????//根據(jù)后綴名把jpg或者png轉(zhuǎn)成webp ????????if(!in_array($ext,?array_keys($this->arr_image_ext))) ????????{ ? ? ? ? ? ? ???//WEBP格式轉(zhuǎn)換:暫不支持該格式轉(zhuǎn)換,目前僅支持轉(zhuǎn)換?jpeg,jpg和png?格式 ????????????return?$this->str_old_file; ????????} ????????//默認(rèn)webp圖片新路徑 ????????if(empty($webp_dir)) ????????{ ????????????$webp_dir?=?rtrim($this->str_old_file,$ext)?.?$this->str_default_ext; ????????} ????????//開始轉(zhuǎn)換 ????????try ????????{ ????????????//原圖的質(zhì)量 ????????????$new_q?=?$this->get_img_quality($ext); ????????????//轉(zhuǎn)換成webp格式 ????????????$this->do_jpg_transform_webp($webp_dir,?$new_q); ????????} ????????catch(Exception?$e) ????????{ ? ? ? ? ? ? ???//WEBP格式轉(zhuǎn)換:工具轉(zhuǎn)換異常,切換至GD庫轉(zhuǎn)換 ????????????$webp_dir?=?$this->php_gd_image_webp($ext,$webp_dir); ????????} ????????//睡眠0.2秒 ????????usleep(20000); ????????if(is_file($webp_dir)) ????????{ ? ? ? ? ? ? ???//刪除原始圖片 + 設(shè)置WEBP圖片訪問權(quán)限 ????????????chmod($webp_dir,?0777); ????????????unlink($this->str_old_file); ? ? ? ? ? ? ???//WEBP格式轉(zhuǎn)換:轉(zhuǎn)換成功 ????????????return?$webp_dir; ????????} ? ? ? ? ??//WEBP格式轉(zhuǎn)換:轉(zhuǎn)換失敗,返回原始圖片路徑 ????????return?$this->str_old_file; ????}
????/** ?????*?獲取圖片的質(zhì)量 ?????*?@param?string?$str_ext?圖片擴(kuò)展名 ?????*?@return?int?圖片的質(zhì)量 ?????*/ ????private?function?get_img_quality($str_ext) ????{ ????????$resource?=?new?Imagick($this->str_old_file); ????????//png特殊處理 ????????if($str_ext?==?'png') ????????{ ????????????$resource->setImageFormat('PNG'); ????????} ????????$resource->setImageCompression($this->arr_image_ext[$str_ext]); ????????$current?=?$resource->getImageCompressionQuality(); ????????$resource->clear(); ????????$resource->destroy(); ????????if(!isset($current)?||?empty($current)) ????????{ ????????????$current?=?80; ????????} ????????$resource->clear(); ????????$resource->destroy(); ????????return?$current; ????}
????/** ?????*?jpg/jpeg/png格式轉(zhuǎn)換成webp格式 ?????*?@param?string?$webp_img_path?webp圖片的真實(shí)路徑 ?????*?@param?int?$int_q?圖片的壓縮質(zhì)量 ?????*/ ????private?function?do_jpg_transform_webp($webp_img_path,?$int_q?=?80) ????{ ????????exec("cwebp?-q?{$int_q}?{$this->str_old_file}?-o?{$webp_img_path}"); ????}
????/** ?????*?應(yīng)用PHP自身的GD庫重新生成一張?webp?格式的圖片,但是有概率生成空白圖(有風(fēng)險(xiǎn)) ?????*?@param?string?$ext??文件擴(kuò)展名 ?????*?@param?string?$webp_img_path??生成webp文件的絕對(duì)路徑 ?????*?@return?string?返回處理之后的圖片絕對(duì)路徑 ?????*/ ????private?function?php_gd_image_webp($ext,$webp_img_path) ????{ ????????//jpg處理使用jpeg ????????$ext?=?$ext?==?'jpg'???'jpeg'?:?$ext; ????????//拼接函數(shù)名?imagecreatefromjpeg?還是?imagecreatefrompng ????????$funName?=?'imagecreatefrom'?.?$ext; ????????//開始轉(zhuǎn)換 ????????$obj_img?=?null; ????????try ????????{ ????????????//打開這個(gè)圖片資源 ????????????$obj_img?=?$funName($this->str_old_file); ????????????//用這個(gè)圖片資源創(chuàng)建一個(gè)webp圖片,?存在路徑$tdir ????????????imagewebp($obj_img,$webp_img_path); ????????} ????????catch(Exception?$e) ????????{
????????} ????????//銷毀畫布資源 ????????if($obj_img?!=?null) ????????{ ????????????imagedestroy($obj_img); ????????} ????????//睡眠0.3秒 ????????return?$webp_img_path; ????} }?
總結(jié)
以上是生活随笔為你收集整理的Linux环境—JPEG/JPG/PNG图片转换WEBP格式(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 交通杀马特︱道路瘦身全纪录:用金钱和生命
- 下一篇: 基于SSM+JSP+MYSQL+H-UI