{"id":614,"date":"2021-09-22T08:27:00","date_gmt":"2021-09-22T00:27:00","guid":{"rendered":"https:\/\/log.tanxiaodian.com\/?p=614"},"modified":"2021-09-22T08:27:00","modified_gmt":"2021-09-22T00:27:00","slug":"php%e5%8e%8b%e7%bc%a9%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/log.tanxiaodian.com\/?p=614","title":{"rendered":"php\u538b\u7f29\u56fe\u7247"},"content":{"rendered":"\n<p>\u6ce8\u610falpha\u901a\u9053\u7684\u7684\u62f7\u8d1d \u4e0d\u7136png\u56fe\u7247\u4f1a\u6709\u95ee\u9898<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\n\n\nnamespace App\\Models\\pub;\n\n<em>\/**\n * \u56fe\u7247\u538b\u7f29\u7c7b\uff1a\u901a\u8fc7\u7f29\u653e\u6765\u538b\u7f29\u3002\n * \u5982\u679c\u8981\u4fdd\u6301\u6e90\u56fe\u6bd4\u4f8b\uff0c\u628a\u53c2\u6570$percent\u4fdd\u6301\u4e3a\u5373\u53ef\u3002\n * \u5373\u4f7f\u539f\u6bd4\u4f8b\u538b\u7f29\uff0c\u4e5f\u53ef\u5927\u5e45\u5ea6\u7f29\u5c0f\u3002\u6570\u7801\u76f8\u673aM\u56fe\u7247\u3002\u4e5f\u53ef\u4ee5\u7f29\u4e3aKB\u5de6\u53f3\u3002\u5982\u679c\u7f29\u5c0f\u6bd4\u4f8b\uff0c\u5219\u4f53\u79ef\u4f1a\u66f4\u5c0f\u3002\n *\n * \u7ed3\u679c\uff1a\u53ef\u4fdd\u5b58\u3001\u53ef\u76f4\u63a5\u663e\u793a\u3002\n *\/\n<\/em>class ImgCompress\n{\n    private $src;\n    private $image;\n    private $imageinfo;\n    private $percent;\n    const <em>PERCENT <\/em>= 1;\n   <em>\/**\n    * \u56fe\u7247\u538b\u7f29\n    * <\/em><strong><em>@param <\/em><\/strong><em>$src string \u6e90\u56fe\n    * <\/em><strong><em>@param <\/em><\/strong><em>float $percent \u538b\u7f29\u6bd4\u4f8b\n    *\/\n   <\/em>public function __construct($src, $percent)\n   {\n       $this->src = $src;\n       $this->percent = $percent;\n   }\n\n    <em>\/** \u9ad8\u6e05\u538b\u7f29\u56fe\u7247\n     * <\/em><strong><em>@param <\/em><\/strong><em>string $saveName \u63d0\u4f9b\u56fe\u7247\u540d\uff08\u53ef\u4e0d\u5e26\u6269\u5c55\u540d\uff0c\u7528\u6e90\u56fe\u6269\u5c55\u540d\uff09\u7528\u4e8e\u4fdd\u5b58\u3002\u6216\u4e0d\u63d0\u4f9b\u6587\u4ef6\u540d\u76f4\u63a5\u663e\u793a\n     *\/\n    <\/em>public function compressImg($saveName='')\n    {\n        $this->_openImage();\n        if(!empty($saveName)) $this->_saveImage($saveName); \/\/\u4fdd\u5b58\n        else $this->_showImage();\n    }\n    <em>\/**\n     * \u5185\u90e8\uff1a\u6253\u5f00\u56fe\u7247\n     *\/\n    <\/em>private function _openImage()\n    {\n        list($width, $height, $type, $attr) = getimagesize($this->src);\n        $this->imageinfo = array(\n            'width'=>$width,\n            'height'=>$height,\n            'type'=>image_type_to_extension($type,false),\n            'attr'=>$attr\n        );\n        $fun = \"imagecreatefrom\".$this->imageinfo['type'];\n        $this->image = $fun($this->src);\n        $this->_thumpImage();\n    }\n    <em>\/**\n     * \u5185\u90e8\uff1a\u64cd\u4f5c\u56fe\u7247\n     *\/\n    <\/em>private function _thumpImage()\n    {\n        $new_width = $this->imageinfo['width'] * $this->percent;\n        $new_height = $this->imageinfo['height'] * $this->percent;\n        $image_thump = imagecreatetruecolor($new_width,$new_height);\n      <strong>  $alpha = imagecolorallocatealpha($image_thump, 0, 0, 0, 127);\n        imagefill($image_thump, 0, 0, $alpha);<\/strong>\n        \/\/$color = imagecolorallocate($image_thump,255,255,255);\n        \/\/imagecolortransparent($image_thump,$color);\n        \/\/imagefill($image_thump,0,0,$color);\n        \/\/\u5c06\u539f\u56fe\u590d\u5236\u5e26\u56fe\u7247\u8f7d\u4f53\u4e0a\u9762\uff0c\u5e76\u4e14\u6309\u7167\u4e00\u5b9a\u6bd4\u4f8b\u538b\u7f29,\u6781\u5927\u7684\u4fdd\u6301\u4e86\u6e05\u6670\u5ea6\n        imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);\n        imagedestroy($this->image);\n        $this->image = $image_thump;\n    }\n    <em>\/**\n     * \u8f93\u51fa\u56fe\u7247:\u4fdd\u5b58\u56fe\u7247\u5219\u7528saveImage()\n     *\/\n    <\/em>private function _showImage()\n    {\n        header('Content-Type: image\/'.$this->imageinfo['type']);\n        $funcs = \"image\".$this->imageinfo['type'];\n        $funcs($this->image);\n    }\n    <em>\/**\n     * \u4fdd\u5b58\u56fe\u7247\u5230\u786c\u76d8\uff1a\n     * <\/em><strong><em>@param <\/em><\/strong><em>string $dstImgName \u3001\u53ef\u6307\u5b9a\u5b57\u7b26\u4e32\u4e0d\u5e26\u540e\u7f00\u7684\u540d\u79f0\uff0c\u4f7f\u7528\u6e90\u56fe\u6269\u5c55\u540d \u3002\u3001\u76f4\u63a5\u6307\u5b9a\u76ee\u6807\u56fe\u7247\u540d\u5e26\u6269\u5c55\u540d\u3002\n     *\/\n    <\/em>private function _saveImage($dstImgName)\n    {\n        if(empty($dstImgName)) return false;\n        $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];  \/\/\u5982\u679c\u76ee\u6807\u56fe\u7247\u540d\u6709\u540e\u7f00\u5c31\u7528\u76ee\u6807\u56fe\u7247\u6269\u5c55\u540d \u540e\u7f00\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u7528\u6e90\u56fe\u7684\u6269\u5c55\u540d\n        $dstExt = strrchr($dstImgName ,\".\");\n        $sourseExt = strrchr($this->src ,\".\");\n        if(!empty($dstExt)) $dstExt =strtolower($dstExt);\n        if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);\n        \/\/\u6709\u6307\u5b9a\u76ee\u6807\u540d\u6269\u5c55\u540d\n        if(!empty($dstExt) &amp;&amp; in_array($dstExt,$allowImgs)){\n            $dstName = $dstImgName;\n        }elseif(!empty($sourseExt) &amp;&amp; in_array($sourseExt,$allowImgs)){\n            $dstName = $dstImgName.$sourseExt;\n        }else{\n            $dstName = $dstImgName.$this->imageinfo['type'];\n        }\n        $funcs = \"image\".$this->imageinfo['type'];\n       <strong> imagesavealpha($this->image, true);<\/strong>\n        $funcs($this->image,$dstName);\n    }\n\n    public static function getImagetype($filename)\n    {\n        $file = fopen($filename, 'rb');\n        $bin  = fread($file, 2); \/\/\u53ea\u8bfb2\u5b57\u8282\n        fclose($file);\n        $strInfo  = @unpack('C2chars', $bin);\n        $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);\n        \/\/ dd($typeCode);\n        $fileType = '';\n        switch ($typeCode) {\n            case 255216:\n                $fileType = 'jpg';\n                break;\n            case 7173:\n                $fileType = 'gif';\n                break;\n            case 6677:\n                $fileType = 'bmp';\n                break;\n            case 13780:\n                $fileType = 'png';\n                break;\n            default:\n                $fileType = '\u53ea\u80fd\u4e0a\u4f20\u56fe\u7247\u7c7b\u578b\u683c\u5f0f';\n        }\n        \/\/ if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg';\n        \/\/ if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png';\n        if(!in_array($fileType, ['jpg', 'jpeg', 'gif', 'bmp', 'png'])) {\n            return false;\n        }\n        return true;\n    }\n\n    <em>\/**\n     * \u9500\u6bc1\u56fe\u7247\n     *\/\n    <\/em>public function __destruct(){\n        imagedestroy($this->image);\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\u610falpha\u901a\u9053\u7684\u7684\u62f7\u8d1d \u4e0d\u7136png\u56fe\u7247\u4f1a\u6709\u95ee\u9898&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/log.tanxiaodian.com\/?p=614\">Continue reading<span class=\"screen-reader-text\">php\u538b\u7f29\u56fe\u7247<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[101,100,99],"_links":{"self":[{"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/posts\/614"}],"collection":[{"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=614"}],"version-history":[{"count":1,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/posts\/614\/revisions"}],"predecessor-version":[{"id":615,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/posts\/614\/revisions\/615"}],"wp:attachment":[{"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}