{"id":24881,"date":"2024-11-21T14:02:41","date_gmt":"2024-11-21T06:02:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24881\/"},"modified":"2024-11-21T14:02:41","modified_gmt":"2024-11-21T06:02:41","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8laravel%e4%b8%ad%e4%bd%bf%e7%94%a8%e4%b8%ad%e9%97%b4%e4%bb%b6%e8%bf%9b%e8%a1%8c%e5%9b%be%e7%89%87%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24881\/","title":{"rendered":"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u56fe\u7247\u5904\u7406"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169898735593065.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u56fe\u7247\u5904\u7406\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u56fe\u7247\u5904\u7406\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u56fe\u7247\u5904\u7406<\/p>\n<p>\u5728\u73b0\u4ee3\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u56fe\u7247\u5904\u7406\u662f\u4e00\u9879\u975e\u5e38\u5e38\u89c1\u7684\u4efb\u52a1\u3002Laravel \u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684 PHP \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u7247\u5904\u7406\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528\u4e2d\u95f4\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u56fe\u7247\uff0c\u800c\u4e0d\u9700\u8981\u4f9d\u8d56\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<p>\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u4ee5\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\u6765\u8bf4\u660e\u5982\u4f55\u5728 Laravel \u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u56fe\u7247\u5904\u7406\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u7c7b\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u4e2d\u95f4\u4ef6\u7c7b\uff1a<\/p>\n<pre>php artisan make:middleware ImageProcessingMiddleware<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5728 app\/Http\/Middleware \u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a ImageProcessingMiddleware.php \u7684\u6587\u4ef6\u3002\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\uff0c\u5e76\u5728 handle \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>public function handle($request, Closure $next)\n{\n    $response = $next($request);\n\n    \/\/ \u68c0\u67e5\u662f\u5426\u4e3a\u56fe\u7247\u7c7b\u578b\n    if ($response instanceof IlluminateHttpResponse &amp;amp;&amp;amp; in_array($response-&amp;gt;headers-&amp;gt;get('Content-Type'), ['image\/jpeg', 'image\/png', 'image\/gif'])) {\n        \/\/ \u83b7\u53d6\u539f\u59cb\u56fe\u50cf\u8def\u5f84\n        $path = $response-&amp;gt;original;\n\n        \/\/ \u6267\u884c\u56fe\u7247\u5904\u7406\u903b\u8f91\n        $image = Image::make($path);\n        $image-&amp;gt;resize(300, null, function ($constraint) {\n            $constraint-&amp;gt;aspectRatio();\n        });\n        $image-&amp;gt;save($path);\n    }\n\n    return $response;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u505a\u4e86\u4ee5\u4e0b\u4e8b\u60c5\uff1a<\/p>\n<ol>\n<li>\u68c0\u67e5\u8fd4\u56de\u7684\u54cd\u5e94\u662f\u5426\u4e3a\u56fe\u7247\u7c7b\u578b\u3002<\/li>\n<li>\u5982\u679c\u662f\u56fe\u7247\u7c7b\u578b\uff0c\u5219\u4f7f\u7528 Intervention Image \u5e93\u6253\u5f00\u539f\u59cb\u56fe\u7247\u3002<\/li>\n<li>\u6267\u884c\u56fe\u7247\u5904\u7406\u903b\u8f91\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a\u5c06\u56fe\u7247\u7f29\u653e\u4e3a\u5bbd\u5ea6\u4e3a 300 \u50cf\u7d20\uff0c\u9ad8\u5ea6\u6309\u6bd4\u4f8b\u8c03\u6574\u3002<\/li>\n<li>\u5c06\u5904\u7406\u540e\u7684\u56fe\u7247\u4fdd\u5b58\u56de\u539f\u59cb\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4e2d\u95f4\u4ef6\u7c7b\u6ce8\u518c\u5230 Laravel \u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u6253\u5f00 app\/Http\/Kernel.php \u6587\u4ef6\uff0c\u5e76\u5728 $middleware \u5c5e\u6027\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>protected $middleware = [\n    \/\/ ...\n    AppHttpMiddlewareImageProcessingMiddleware::class,\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e2d\u95f4\u4ef6\u7684\u51c6\u5907\u5de5\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u6d4b\u8bd5\u7684\u8def\u7531\u3002\u6253\u5f00 routes\/web.php \u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>Route::get('\/image', function () {\n    $path = public_path('images\/test.jpg');\n    return response()-&amp;gt;file($path);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a \/image \u8def\u7531\uff0c\u5b83\u4f1a\u8fd4\u56de\u4f4d\u4e8e public\/images\/test.jpg \u7684\u6d4b\u8bd5\u56fe\u7247\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u542f\u52a8 Laravel \u5f00\u53d1\u670d\u52a1\u5668\u6765\u6d4b\u8bd5\u6211\u4eec\u7684\u4e2d\u95f4\u4ef6\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan serve<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u5e76\u8bbf\u95ee http:\/\/localhost:8000\/image\u3002\u4f60\u5e94\u8be5\u53ef\u4ee5\u770b\u5230\u4f4d\u4e8e public\/images\/test.jpg \u7684\u539f\u59cb\u56fe\u7247\u5df2\u7ecf\u88ab\u4e2d\u95f4\u4ef6\u5904\u7406\u8fc7\u4e86\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<br \/>\u901a\u8fc7\u4f7f\u7528\u4e2d\u95f4\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 Laravel \u4e2d\u975e\u5e38\u65b9\u4fbf\u5730\u8fdb\u884c\u56fe\u7247\u5904\u7406\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Intervention Image \u5e93\u5bf9\u56fe\u7247\u8fdb\u884c\u7f29\u653e\u5904\u7406\u3002\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u8be5\u4e2d\u95f4\u4ef6\uff0c\u4ee5\u6ee1\u8db3\u4f60\u7684\u5177\u4f53\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4e8e\u5b66\u4e60 Laravel \u4e2d\u95f4\u4ef6\u7684\u56fe\u7247\u5904\u7406\u529f\u80fd\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u56fe\u7247\u5904\u7406\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u56fe\u7247\u5904\u7406 \u5728\u73b0\u4ee3\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u56fe\u7247\u5904\u7406\u662f\u4e00\u9879\u975e\u5e38\u5e38\u89c1\u7684\u4efb\u52a1\u3002Laravel \u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684 PHP \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u7247\u5904\u7406\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528\u4e2d\u95f4\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u56fe\u7247\uff0c\u800c\u4e0d\u9700\u8981\u4f9d\u8d56\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\u3002 \u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u4ee5\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\u6765\u8bf4\u660e\u5982\u4f55\u5728 Laravel \u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u56fe\u7247\u5904\u7406\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u7c7b\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u4e2d\u95f4\u4ef6\u7c7b\uff1a php artisan make:middleware ImageProcessingMiddleware \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u5728 app\/Http\/Middleware \u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a ImageProcessingMiddleware.php \u7684\u6587\u4ef6\u3002\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\uff0c\u5e76\u5728 handle \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a public function handle($request, Closure $next) { $response = $next($request); \/\/ \u68c0\u67e5\u662f\u5426\u4e3a\u56fe\u7247\u7c7b\u578b if ($response instanceof IlluminateHttpResponse &amp;amp;&amp;amp; in_array($response-&amp;gt;headers-&amp;gt;get(&#8216;Content-Type&#8217;), [&#8216;image\/jpeg&#8217;, &#8216;image\/png&#8217;, &#8216;image\/gif&#8217;])) { \/\/ \u83b7\u53d6\u539f\u59cb\u56fe\u50cf\u8def\u5f84 $path = $response-&amp;gt;original; \/\/ \u6267\u884c\u56fe\u7247\u5904\u7406\u903b\u8f91 $image = Image::make($path); $image-&amp;gt;resize(300, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24881","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24881"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24881\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}