{"id":421,"date":"2021-06-17T17:13:56","date_gmt":"2021-06-17T09:13:56","guid":{"rendered":"https:\/\/wp.dj47.top\/?p=421"},"modified":"2021-10-20T16:06:57","modified_gmt":"2021-10-20T08:06:57","slug":"php%e5%9b%be%e7%89%87%e5%8e%8b%e7%bc%a9%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=421","title":{"rendered":"PHP\u56fe\u7247\u538b\u7f29\u7c7b"},"content":{"rendered":"<pre class=\"language-php\"><code>&lt;?php\r\n\/**\r\n * \u56fe\u7247\u538b\u7f29\u7c7b\uff1a\u901a\u8fc7\u7f29\u653e\u6765\u538b\u7f29\u3002\u5982\u679c\u8981\u4fdd\u6301\u6e90\u56fe\u6bd4\u4f8b\uff0c\u628a\u53c2\u6570$percent\u4fdd\u6301\u4e3a1\u5373\u53ef\u3002\r\n * \u5373\u4f7f\u539f\u6bd4\u4f8b\u538b\u7f29\uff0c\u4e5f\u53ef\u5927\u5e45\u5ea6\u7f29\u5c0f\u3002\u6570\u7801\u76f8\u673a4M\u56fe\u7247\u3002\u4e5f\u53ef\u4ee5\u7f29\u4e3a700KB\u5de6\u53f3\u3002\u5982\u679c\u7f29\u5c0f\u6bd4\u4f8b\uff0c\u5219\u4f53\u79ef\u4f1a\u66f4\u5c0f\u3002\r\n * \u7ed3\u679c\uff1a\u53ef\u4fdd\u5b58\u3001\u53ef\u76f4\u63a5\u663e\u793a\u3002\r\n *\/\r\nclass imgcompress{\r\n    private $src;\r\n    private $image;\r\n    private $imageinfo;\r\n    private $percent = 0.5;\r\n  \/**\r\n   * \u56fe\u7247\u538b\u7f29\r\n   * @param string $src \u6e90\u56fe\r\n   * @param float $percent \u538b\u7f29\u6bd4\u4f8b\r\n   *\/\r\n  public function __construct($src, $percent = 1)\r\n  {\r\n      $this-&gt;src = $src;\r\n      $this-&gt;percent = $percent;\r\n  }\r\n  \/** \u9ad8\u6e05\u538b\u7f29\u56fe\u7247\r\n   * @param 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\r\n   *\/\r\n  public function compressImg($saveName='')\r\n  {\r\n      $this-&gt;_openImage();\r\n      if(!empty($saveName)) $this-&gt;_saveImage($saveName); \/\/\u4fdd\u5b58\r\n      else $this-&gt;_showImage();\r\n  }\r\n  \/**\r\n   * \u5185\u90e8\uff1a\u6253\u5f00\u56fe\u7247\r\n   *\/\r\n  private function _openImage()\r\n  {\r\n      list($width, $height, $type, $attr) = getimagesize($this-&gt;src);\r\n      $this-&gt;imageinfo = array(\r\n          'width'=&gt;$width,\r\n          'height'=&gt;$height,\r\n          'type'=&gt;image_type_to_extension($type,false),\r\n          'attr'=&gt;$attr\r\n      );\r\n      $fun = \"imagecreatefrom\".$this-&gt;imageinfo['type'];\r\n      $this-&gt;image = $fun($this-&gt;src);\r\n      $this-&gt;_thumpImage();\r\n  }\r\n  \/**\r\n   * \u5185\u90e8\uff1a\u64cd\u4f5c\u56fe\u7247\r\n   *\/\r\n  private function _thumpImage()\r\n  {\r\n      $new_width = $this-&gt;imageinfo['width'] * $this-&gt;percent;\r\n      $new_height = $this-&gt;imageinfo['height'] * $this-&gt;percent;\r\n      $image_thump = imagecreatetruecolor($new_width,$new_height);\r\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\r\n      imagecopyresampled($image_thump,$this-&gt;image,0,0,0,0,$new_width,$new_height,$this-&gt;imageinfo['width'],$this-&gt;imageinfo['height']);\r\n      imagedestroy($this-&gt;image);\r\n      $this-&gt;image = $image_thump;\r\n  }\r\n  \/**\r\n   * \u8f93\u51fa\u56fe\u7247:\u4fdd\u5b58\u56fe\u7247\u5219\u7528saveImage()\r\n   *\/\r\n  private function _showImage()\r\n  {\r\n      header('Content-Type: image\/'.$this-&gt;imageinfo['type']);\r\n      $funcs = \"image\".$this-&gt;imageinfo['type'];\r\n      $funcs($this-&gt;image);\r\n  }\r\n  \/**\r\n   * \u4fdd\u5b58\u56fe\u7247\u5230\u786c\u76d8\uff1a\r\n   * @param 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\r\n   *\/\r\n  private function _saveImage($dstImgName)\r\n  {\r\n      if(empty($dstImgName)) return false;\r\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\r\n      $dstExt = strrchr($dstImgName ,\".\");\r\n      $sourseExt = strrchr($this-&gt;src ,\".\");\r\n      if(!empty($dstExt)) $dstExt =strtolower($dstExt);\r\n      if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);\r\n      \/\/\u53d6\u51fa\u8def\u5f84\u3010\u53bb\u9664\u6587\u4ef6\u540d\u3011\uff0c\u65b9\u4fbf\u751f\u6210\u6587\u4ef6\u5939\r\n      $dir = str_replace(basename($dstImgName),'',$dstImgName);\r\n      if(!file_exists($dir))\r\n      {\r\n          \/\/\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\u5219\u751f\u6210\r\n          mkdir($dir, 0777, true);\r\n      }\r\n     \r\n      \/\/\u6709\u6307\u5b9a\u76ee\u6807\u540d\u6269\u5c55\u540d\r\n      if(!empty($dstExt) &amp;&amp; in_array($dstExt,$allowImgs)){\r\n          $dstName = $dstImgName;\r\n      }elseif(!empty($sourseExt) &amp;&amp; in_array($sourseExt,$allowImgs)){\r\n          \/\/\u65e0\u6269\u5c55\u540d\u65f6\uff0c\u5219\u4f7f\u7528\u539f\u56fe\u6269\u5c55\u540d\r\n          $dstName = $dstImgName.$sourseExt;\r\n      }else{\r\n          \/\/\u5426\u5219\u6839\u636e\u6587\u4ef6\u7c7b\u578b\uff0c\u4f7f\u7528\u5bf9\u5e94\u6269\u5c55\u540d\r\n          $dstName = $dstImgName.$this-&gt;imageinfo['type'];\r\n      }\r\n      $funcs = \"image\".$this-&gt;imageinfo['type'];\r\n      $funcs($this-&gt;image,$dstName);\r\n  }\r\n  \/**\r\n   * \u9500\u6bc1\u56fe\u7247\r\n   *\/\r\n  public function __destruct(){\r\n      imagedestroy($this-&gt;image);\r\n  }\r\n}\r\n \r\n$source = '1.png';  \/\/\u539f\u56fe\u6587\u4ef6\u540d\r\n$dst_img = '..\/copy1.png'; \/\/\u4fdd\u5b58\u56fe\u7247\u7684\u6587\u4ef6\u540d\r\n$percent = 1;  #\u539f\u56fe\u538b\u7f29\uff0c\u4e0d\u7f29\u653e\r\n$image = (new imgcompress($source,$percent))-&gt;compressImg($dst_img);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/** * \u56fe\u7247\u538b\u7f29\u7c7b\uff1a\u901a\u8fc7\u7f29\u653e\u6765\u538b\u7f29\u3002\u5982\u679c\u8981\u4fdd\u6301\u6e90\u56fe\u6bd4\u4f8b\uff0c\u628a\u53c2\u6570$percent\u4fdd\u6301\u4e3a1\u5373 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"class_list":["post-421","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=421"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/421\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}