{"id":174,"date":"2023-02-19T19:08:37","date_gmt":"2023-02-19T11:08:37","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=174"},"modified":"2023-02-19T19:09:18","modified_gmt":"2023-02-19T11:09:18","slug":"php-realize-image-scale-thumbnail","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/19\/php-realize-image-scale-thumbnail\/","title":{"rendered":"PHP\u5b9e\u73b0\u56fe\u7247\u7b49\u6bd4\u4f8b\u7f29\u7565\u56fe"},"content":{"rendered":"<p>PHP\u5b9e\u73b0\u56fe\u7247\u7b49\u6bd4\u4f8b\u7f29\u7565\u56fe\uff0c\u751f\u6210\u6b63\u65b9\u5f62\u7f29\u7565\u56fe\u3002<\/p>\n<p>\u6b65\u9aa4\uff1a\u5f85\u538b\u7f29\u6b63\u65b9\u5f62\u56fe\u7247\u7684\u8fb9\u957f\u4e3a\u539f\u59cb\u56fe\u7247\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u7684\u50cf\u7d20\u6700\u5c0f\u503c\uff0c\u56fe\u7247\u5bbd\u9ad8\u8f83\u957f\u7684\u4e00\u8fb9\u88c1\u526a\u4e2d\u95f4\u90e8\u5206\uff0c\u518d\u5b9e\u73b0\u50cf\u7d20\u538b\u7f29\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-php\">&lt;?php\n\n$imgpath = &#039;test.jpg&#039;;\n\nif (!empty($imgpath)) {\n    $info = getImageInfo($imgpath);\n    if ($info) {\n        $srcWidth  = $info[&#039;width&#039;];\n        $srcHeight = $info[&#039;height&#039;];\n        if ($srcWidth &lt; $srcHeight) {\n            $srcSize = $srcWidth;\n            $srcX = 0;\n            $srcY = ($srcHeight - $srcWidth) \/ 2;\n        } else {\n            $srcSize = $srcHeight;\n            $srcX = ($srcWidth - $srcHeight) \/ 2;\n            $srcY = 0;\n        }\n        $type = strtolower(empty($type) ? $info[&#039;type&#039;] : $type);\n        unset($info);\n\n        \/\/\u6784\u9020\u51fd\u6570\n        $loadImage = &#039;imagecreatefrom&#039;.($type==&#039;jpg&#039;?&#039;jpeg&#039;:$type);\n        $createImage = &#039;image&#039;.($type==&#039;jpg&#039;?&#039;jpeg&#039;:$type);\n\n        \/\/\u53c2\u6570\n        $quality = 60;\n        $thumb_size = 200;\n\n        \/\/\u8f7d\u5165\u539f\u56fe\n        $src_image    = $loadImage($imgpath);\n\n        \/\/\u521b\u5efa\u76ee\u6807\u56fe\n        $thumb_image = imagecreatetruecolor($thumb_size, $thumb_size);\n        $color = imagecolorallocatealpha($thumb_image, 255, 255, 255, 100);\n        imagefill($thumb_image, 0, 0, $color);\n\n        \/\/\u5904\u7406\n        \/\/bool imagecopyresampled(resource $dst_image, resource $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $dst_w, int $dst_h, int $src_w, int $src_h)\n        $result = imagecopyresampled($thumb_image, $src_image, 0, 0, $srcX, $srcY, $thumb_size, $thumb_size, $srcSize, $srcSize);\n\n        \/\/\u751f\u6210\u56fe\u7247\n        $index = strpos($imgpath, &#039;.&#039;, 0);\n        $thumb_imgpath = substr($imgpath, 0, $index) . &quot;-thumb&quot; . substr($imgpath, $index);\n        $createImage($thumb_image, $thumb_imgpath, 100);\n\n        \/\/\u91ca\u653e\u8d44\u6e90\n        imagedestroy($src_image);\n        imagedestroy($thumb_image);\n    }\n}\n\nfunction getImageInfo($imgpath) {\n    $info = array();\n    $imageInfo = getimagesize($imgpath);\n    if($imageInfo !== false) {\n        $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));\n        $imageSize = filesize($imgpath);\n        $info[&#039;width&#039;] = $imageInfo[0];\n        $info[&#039;height&#039;] = $imageInfo[1];\n        $info[&#039;type&#039;] = $imageType;\n        $info[&#039;size&#039;] = $imageSize;\n        $info[&#039;mime&#039;] = $imageInfo[&#039;mime&#039;];\n    }\n    return $info;\n}\n\n?&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u5b9e\u73b0\u56fe\u7247\u7b49\u6bd4\u4f8b\u7f29\u7565\u56fe\uff0c\u751f\u6210\u6b63\u65b9\u5f62\u7f29\u7565\u56fe\u3002 \u6b65\u9aa4\uff1a\u5f85\u538b\u7f29\u6b63\u65b9\u5f62\u56fe\u7247\u7684\u8fb9\u957f\u4e3a\u539f\u59cb\u56fe\u7247\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u7684\u50cf\u7d20\u6700\u5c0f\u503c\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-php-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}