{"id":671,"date":"2025-06-11T12:32:32","date_gmt":"2025-06-11T04:32:32","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/xtywj\/671.html"},"modified":"2025-06-11T12:32:32","modified_gmt":"2025-06-11T04:32:32","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8php%e4%b8%ad%e8%b0%83%e6%95%b4%e5%9b%be%e5%83%8f%e5%a4%a7%e5%b0%8f","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/671.html","title":{"rendered":"\u5982\u4f55\u5728PHP\u4e2d\u8c03\u6574\u56fe\u50cf\u5927\u5c0f"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"Bug&#32534;&#35793;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">Bug&#32534;&#35793;&#29422;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312;PHP&#20013;&#35843;&#25972;&#22270;&#20687;&#22823;&#23567;&#21487;&#20197;&#36890;&#36807;&#22810;&#31181;&#26041;&#27861;&#23454;&#29616;&#65292;&#21253;&#25324;&#20351;&#29992;GD&#24211;&#21644;ImageMagick&#25193;&#23637;&#12290;&#20197;&#19979;&#26159;&#20004;&#31181;&#24120;&#35265;&#30340;&#26041;&#27861;&#65306;<\/p>\n<h3>&#26041;&#27861;&#19968;&#65306;&#20351;&#29992;GD&#24211;<\/h3>\n<ol>\n<li>\n<p><strong>&#23433;&#35013;GD&#24211;<\/strong>&#65306;\n&#22914;&#26524;&#20320;&#30340;&#26381;&#21153;&#22120;&#19978;&#27809;&#26377;&#23433;&#35013;GD&#24211;&#65292;&#21487;&#20197;&#20351;&#29992;&#20197;&#19979;&#21629;&#20196;&#36827;&#34892;&#23433;&#35013;&#65306;<\/p>\n<pre><code class=\"language-bash\">sudo apt-get install php-gd  # Debian\/Ubuntu\nsudo yum install php-gd       # CentOS\/RHEL<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>&#32534;&#20889;PHP&#33050;&#26412;<\/strong>&#65306;\n&#20351;&#29992;GD&#24211;&#26469;&#35835;&#21462;&#22270;&#20687;&#24182;&#35843;&#25972;&#20854;&#22823;&#23567;&#12290;<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\/\/ &#25351;&#23450;&#35201;&#22788;&#29702;&#30340;&#22270;&#20687;&#25991;&#20214;&#36335;&#24452;\n$inputImagePath = 'path\/to\/input\/image.jpg';\n$outputImagePath = 'path\/to\/output\/image_resized.jpg';\n\n\/\/ &#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#36164;&#28304;\n$image = imagecreatefromjpeg($inputImagePath);\n\n\/\/ &#33719;&#21462;&#21407;&#22270;&#30340;&#23485;&#24230;&#21644;&#39640;&#24230;\n$originalWidth = imagesx($image);\n$originalHeight = imagesy($image);\n\n\/\/ &#35774;&#32622;&#26032;&#30340;&#23610;&#23544;&#65288;&#20363;&#22914;&#65292;&#23558;&#22270;&#20687;&#32553;&#25918;&#21040;50%&#65289;\n$newWidth = $originalWidth * 0.5;\n$newHeight = $originalHeight * 0.5;\n\n\/\/ &#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#31354;&#30333;&#22270;&#20687;\n$resizedImage = imagecreatetruecolor($newWidth, $newHeight);\n\n\/\/ &#23558;&#21407;&#22270;&#30340;&#20687;&#32032;&#22797;&#21046;&#21040;&#26032;&#22270;&#20687;&#20013;\nimagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);\n\n\/\/ &#20445;&#23384;&#35843;&#25972;&#21518;&#30340;&#22270;&#20687;\nimagejpeg($resizedImage, $outputImagePath);\n\n\/\/ &#37322;&#25918;&#20869;&#23384;\nimagedestroy($image);\nimagedestroy($resizedImage);\n?&gt;<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>&#26041;&#27861;&#20108;&#65306;&#20351;&#29992;ImageMagick&#25193;&#23637;<\/h3>\n<ol>\n<li>\n<p><strong>&#23433;&#35013;ImageMagick&#25193;&#23637;<\/strong>&#65306;\n&#22914;&#26524;&#20320;&#30340;&#26381;&#21153;&#22120;&#19978;&#27809;&#26377;&#23433;&#35013;ImageMagick&#25193;&#23637;&#65292;&#21487;&#20197;&#20351;&#29992;&#20197;&#19979;&#21629;&#20196;&#36827;&#34892;&#23433;&#35013;&#65306;<\/p>\n<pre><code class=\"language-bash\">sudo apt-get install php-imagemagick  # Debian\/Ubuntu\nsudo yum install php-pecl-image Magick     # CentOS\/RHEL<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>&#32534;&#20889;PHP&#33050;&#26412;<\/strong>&#65306;\n&#20351;&#29992;ImageMagick&#25193;&#23637;&#26469;&#35835;&#21462;&#22270;&#20687;&#24182;&#35843;&#25972;&#20854;&#22823;&#23567;&#12290;<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\/\/ &#25351;&#23450;&#35201;&#22788;&#29702;&#30340;&#22270;&#20687;&#25991;&#20214;&#36335;&#24452;\n$inputImagePath = 'path\/to\/input\/image.jpg';\n$outputImagePath = 'path\/to\/output\/image_resized.jpg';\n\n\/\/ &#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#36164;&#28304;\n$image = new Imagick($inputImagePath);\n\n\/\/ &#35774;&#32622;&#26032;&#30340;&#23610;&#23544;&#65288;&#20363;&#22914;&#65292;&#23558;&#22270;&#20687;&#32553;&#25918;&#21040;50%&#65289;\n$image-&gt;resizeImage($image-&gt;getImageWidth() * 0.5, $image-&gt;getImageHeight() * 0.5, Imagick::FILTER_LANCZOS, 1);\n\n\/\/ &#20445;&#23384;&#35843;&#25972;&#21518;&#30340;&#22270;&#20687;\n$image-&gt;writeImage($outputImagePath);\n\n\/\/ &#37322;&#25918;&#20869;&#23384;\n$image-&gt;destroy();\n?&gt;<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>&#36825;&#20004;&#31181;&#26041;&#27861;&#37117;&#21487;&#20197;&#26377;&#25928;&#22320;&#35843;&#25972;&#22270;&#20687;&#22823;&#23567;&#65292;&#36873;&#25321;&#21738;&#31181;&#26041;&#27861;&#21462;&#20915;&#20110;&#20320;&#30340;&#20855;&#20307;&#38656;&#27714;&#21644;&#26381;&#21153;&#22120;&#29615;&#22659;&#12290;&#24076;&#26395;&#36825;&#20123;&#31034;&#20363;&#33021;&#24110;&#21161;&#20320;&#26356;&#22909;&#22320;&#29702;&#35299;&#21644;&#20351;&#29992;PHP&#20013;&#30340;&#22270;&#20687;&#22788;&#29702;&#21151;&#33021;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#40657;&#26495;Bug&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#40657;&#26495;Bug&#35762;&#24072;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<body><h2>&#29702;&#35299;PHP&#20013;&#30340;&#22270;&#20687;&#22788;&#29702;<\/h2><p>&#22270;&#29255;&#32553;&#25918;&#22312;&#32593;&#39029;&#24212;&#29992;&#20013;&#26159;&#19968;&#31181;&#24120;&#35265;&#30340;&#38656;&#27714;&#65292;&#26080;&#35770;&#26159;&#20026;&#20102;&#20943;&#23569;&#25991;&#20214;&#22823;&#23567;&#20197;&#21152;&#24555;&#21152;&#36733;&#36895;&#24230;&#65292;&#36824;&#26159;&#31616;&#21333;&#22320;&#36866;&#24212;&#29305;&#23450;&#30340;&#35774;&#35745;&#24067;&#23616;&#12290;&#22312;&#36825;&#31687;&#25351;&#21335;&#20013;&#65292;&#25105;&#20204;&#23558;&#23398;&#20064;&#22914;&#20309;&#22312;PHP&#20013;&#25191;&#34892;&#22270;&#20687;&#32553;&#25918;&#25805;&#20316;&#12290;<\/p><p>PHP &#25552;&#20379;&#20102;&#22810;&#31181;&#22270;&#20687;&#22788;&#29702;&#24211;&#65292;&#20363;&#22914; GD &#21644; Imagick&#12290;GD &#26159;&#19968;&#20010;&#26631;&#20934;&#30340;&#24211;&#65292;&#22312;&#22823;&#22810;&#25968; PHP &#23433;&#35013;&#20013;&#36890;&#24120;&#40664;&#35748;&#21551;&#29992;&#65292;&#32780; Imagick &#21017;&#26159; ImageMagick &#24211;&#30340;&#21253;&#35013;&#22120;&#65292;&#25552;&#20379;&#20102;&#26356;&#39640;&#32423;&#30340;&#22270;&#20687;&#22788;&#29702;&#33021;&#21147;&#12290;&#26412;&#25991;&#20027;&#35201;&#20171;&#32461;&#20351;&#29992; GD &#24211;&#65292;&#22240;&#20026;&#23427;&#26159;&#22823;&#22810;&#25968; PHP &#23433;&#35013;&#20013;&#40664;&#35748;&#21551;&#29992;&#30340;&#26631;&#20934;&#24211;&#65288;&#36824;&#26377;&#21478;&#19968;&#20010;&#39640;&#32423;&#31034;&#20363;&#20351;&#29992; Imagick&#65289;&#12290;<\/p><h2>&#35774;&#32622;&#24744;&#30340;&#29615;&#22659;<\/h2><p>&#22312;&#24320;&#22987;&#20043;&#21069;&#65292;&#35831;&#30830;&#20445;&#24744;&#30340;&#26381;&#21153;&#22120;&#19978;&#24050;&#23433;&#35013;&#24182;&#21551;&#29992;&#20102;GD&#24211;&#12290;&#24744;&#21487;&#20197;&#36890;&#36807;&#21019;&#24314;&#19968;&#20010;phpinfo.php&#25991;&#20214;&#26469;&#26816;&#26597;&#65306;&#35831;&#26597;&#30475;PHP&#20449;&#24687;&#39029;&#38754;&#20013;&#30340;GD&#37096;&#20998;&#12290;<\/p><pre><code>&lt;?php\nphpinfo();\n?&gt;\n<\/code><\/pre><p>&#22914;&#26524;GD&#26410;&#23433;&#35013;&#65292;&#24744;&#21487;&#33021;&#38656;&#35201;&#36890;&#36807;&#24744;&#30340;&#26381;&#21153;&#22120;&#36719;&#20214;&#31649;&#29702;&#24037;&#20855;&#25110;&#21521;&#24744;&#30340;&#25176;&#31649;&#25552;&#20379;&#21830;&#27714;&#21161;&#26469;&#23433;&#35013;&#25110;&#21551;&#29992;&#23427;&#12290;<\/p><h2>&#20351;&#29992;GD&#24211;&#22312;PHP&#20013;&#32553;&#25918;&#22270;&#20687;<\/h2><p>&#35201;&#20351;&#29992;GD&#24211;&#32553;&#25918;&#22270;&#20687;&#65292;&#35831;&#25353;&#29031;&#20197;&#19979;&#27493;&#39588;&#25805;&#20316;&#65306;<\/p><h3>&#27493;&#39588;1 &#8211; &#21152;&#36733;&#21407;&#22987;&#22270;&#20687;<\/h3><p>&#31532;&#19968;&#27493;&#26159;&#35201;&#21152;&#36733;&#20320;&#24819;&#32553;&#25918;&#30340;&#22270;&#20687;&#12290;&#20320;&#21487;&#20197;&#36890;&#36807;&#20197;&#19979;&#20960;&#31181;&#26041;&#24335;&#26469;&#23436;&#25104;&#36825;&#20010;&#25805;&#20316;&#65306;&#20351;&#29992;Photoshop&#12289;GIMP&#25110;&#32773;&#20219;&#20309;&#20854;&#20182;&#25903;&#25345;&#22270;&#29255;&#22788;&#29702;&#30340;&#24212;&#29992;&#31243;&#24207;&#12290;<code>imagecreatefromjpeg()<\/code>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>imagecreatefrompng()<\/code>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>imagecreatefromgif()<\/code>&#65292;&#25110;&#32773;&#26681;&#25454;&#22270;&#20687;&#31867;&#22411;&#20351;&#29992;&#31867;&#20284;&#30340;&#21151;&#33021;&#65306;<\/p><pre><code>$originalImage = imagecreatefromjpeg('path\/to\/your\/image.jpg');<\/code><\/pre><h3>&#27493;&#39588; 2 &#8211; &#21019;&#24314;&#26032;&#22270;&#20687;&#30011;&#24067;<\/h3><p>&#25509;&#19979;&#26469;&#65292;&#20351;&#29992;&#25152;&#38656;&#30340;&#23610;&#23544;&#21019;&#24314;&#19968;&#20010;&#30495;&#23454;&#33394;&#24425;&#30340;&#22270;&#20687;&#30011;&#24067;&#12290;<code>imagecreatetruecolor()<\/code>&#21151;&#33021;&#65306;<\/p><pre><code>$width = 200; \/\/ desired image width\n$height = 150; \/\/ desired image height\n$newImage = imagecreatetruecolor($width, $height);<\/code><\/pre><h3>&#27493;&#39588; 3 &#8211; &#22797;&#21046;&#24182;&#35843;&#25972;&#22270;&#20687;&#22823;&#23567;<\/h3><p>&#35831;&#20351;&#29992;&#12290;<code>imagecopyresampled()<\/code>&#20026;&#20102;&#22312;&#21407;&#22987;&#22270;&#20687;&#19978;&#22797;&#21046;&#24182;&#35843;&#25972;&#37096;&#20998;&#21306;&#22495;&#21040;&#30495;&#24425;&#33394;&#30011;&#24067;&#65306;<\/p><pre><code>imagecopyresampled($newImage, $originalImage, 0, 0, 0, 0, $width, $height, imagesx($originalImage), imagesy($originalImage));<\/code><\/pre><h3>&#27493;&#39588; 4 &#8211; &#20445;&#23384;&#32553;&#25918;&#21518;&#30340;&#22270;&#20687;<\/h3><p>&#20026;&#20102;&#20445;&#23384;&#32553;&#30053;&#22270;&#65292;&#21487;&#20197;&#20351;&#29992;&#22914; ResizeImage &#20989;&#25968;&#31561;&#31867;&#20284;&#30340;&#21151;&#33021;&#12290;<code>imagejpeg()<\/code>&#21734;&#12290;<code>imagepng()<\/code>&#26681;&#25454;&#22270;&#20687;&#30340;&#21407;&#22987;&#26684;&#24335;&#65306;<\/p><pre><code>imagejpeg($newImage, 'path\/to\/resized\/image.jpg', 90); \/\/ 90 is the quality setting<\/code><\/pre><h3>&#27493;&#39588;5 &#8211; &#28165;&#29702;&#24037;&#20316;&#21306;<\/h3><p>&#35760;&#24471;&#22312;&#20445;&#23384;&#22270;&#20687;&#21518;&#37322;&#25918;&#19982;&#20043;&#20851;&#32852;&#30340;&#25152;&#26377;&#20869;&#23384;&#65306;<\/p><pre><code>imagedestroy($originalImage);\nimagedestroy($newImage);<\/code><\/pre><h2>&#22788;&#29702;&#19981;&#21516;&#31867;&#22411;&#30340;&#22270;&#20687;&#21644;&#32437;&#27178;&#27604;&#12290;<\/h2><p>&#22270;&#20687;&#31867;&#22411;&#65306;&#30830;&#20445;&#26681;&#25454;&#25991;&#20214;&#31867;&#22411;&#27491;&#30830;&#21152;&#36733;&#21644;&#20445;&#23384;&#24744;&#30340;&#22270;&#29255;&#12290;&#20363;&#22914;&#65292;<code>imagecreatefrompng()<\/code>&#23545;&#20110;PNG&#25991;&#20214;&#65292;<code>imagepng()<\/code>&#20026;&#20102;&#25327;&#25937;&#20182;&#20204;&#12290;<\/p><p>&#32437;&#27178;&#27604;&#65306;&#20026;&#20102;&#20445;&#25345;&#32437;&#27178;&#27604;&#65292;&#24744;&#24212;&#35813;&#26681;&#25454;&#21407;&#22987;&#23610;&#23544;&#35745;&#31639;&#26032;&#30340;&#23485;&#24230;&#25110;&#39640;&#24230;&#27604;&#20363;&#65306;<\/p><pre><code>$originalWidth = imagesx($originalImage);\n$originalHeight = imagesy($originalImage);\n$newWidth = $width; \/\/ desired width\n$newHeight = ($originalHeight \/ $originalWidth) * $newWidth;<\/code><\/pre><h2>&#39640;&#32423;&#25216;&#26415;<\/h2><p>&#23545;&#20110;&#26356;&#39640;&#32423;&#30340;&#25511;&#21046;&#65292;&#25506;&#32034;&#65306;<\/p><p>&#32553;&#25918;&#31639;&#27861;&#65306;&#23454;&#39564;&#19981;&#21516;&#30340;&#32553;&#25918;&#31639;&#27861;&#12290;<code>imagecopyresampled()<\/code>&#20026;&#20102;&#20248;&#21270;&#36136;&#37327;&#12290;<\/p><p>ImageMagick&#65306;&#22914;&#26524;&#24744;&#38656;&#35201;&#26356;&#22810;&#21151;&#33021;&#65292;&#35831;&#32771;&#34385;&#20351;&#29992;ImageMagick&#65292;&#23427;&#25552;&#20379;&#20102;&#24378;&#22823;&#30340;&#22270;&#20687;&#22788;&#29702;&#33021;&#21147;&#12290;<\/p><p>&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<\/p><pre><code>&lt;?php\r\n\r\nfunction resizeImageWithImagick($imagePath, $newWidth, $newHeight) {\r\n    try {\r\n        \/\/ Create an Imagick object\r\n        $imagick = new Imagick($imagePath);\r\n\r\n        \/\/ Set the resize algorithm - Imagick::FILTER_LANCZOS is a good choice for quality\r\n        $filterType = Imagick::FILTER_LANCZOS;\r\n\r\n        \/\/ Resize the image\r\n        $imagick-&gt;resizeImage($newWidth, $newHeight, $filterType, 1);\r\n\r\n        \/\/ Set to use jpeg compression\r\n        $imagick-&gt;setImageFormat('jpeg');\r\n\r\n        \/\/ Send the headers and output the image\r\n        header('Content-Type: image\/jpeg');\r\n        echo $imagick;\r\n\r\n        \/\/ Clear Imagick resources\r\n        $imagick-&gt;clear();\r\n        $imagick-&gt;destroy();\r\n    } catch (Exception $e) {\r\n        echo 'Error: ' . $e-&gt;getMessage();\r\n    }\r\n}\r\n\r\n$imagePath = 'path\/to\/your\/image.jpg';\r\n$newWidth = 800;  \/\/ Set the new width\r\n$newHeight = 600; \/\/ Set the new height\r\n\r\nresizeImageWithImagick($imagePath, $newWidth, $newHeight);\r\n\r\n?&gt;\r<\/code><\/pre><p>&#35299;&#37322;&#65306;<\/p><p>Imagick&#30340;&#36164;&#28304;&#22312;&#20351;&#29992;&#21518;&#20250;&#34987;&#28165;&#38500;&#12290;<\/p><p>&#22270;&#20687;&#20250;&#30452;&#25509;&#36755;&#20986;&#21040;&#27983;&#35272;&#22120;&#12290;<\/p><p>&#22270;&#20687;&#26684;&#24335;&#24050;&#35774;&#32622;&#20026;JPEG&#65292;&#20294;&#21487;&#20197;&#26681;&#25454;&#24744;&#30340;&#38656;&#27714;&#36827;&#34892;&#26356;&#25913;&#12290;<\/p><p>&#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#22826;&#26126;&#30333;&#24744;&#30340;&#24847;&#24605;&#12290;<code>resizeImage<\/code>&#26041;&#27861;&#29992;&#20110;&#35843;&#25972;&#22270;&#20687;&#22823;&#23567;&#12290;<code>Imagick::FILTER_LANCZOS<\/code>&#28388;&#38236;&#26159;&#19968;&#20010;&#36890;&#29992;&#30340;&#36136;&#37327;&#32553;&#25918;&#22909;&#36873;&#25321;&#12290;<\/p><p>&#20174;&#22270;&#20687;&#25991;&#20214;&#21019;&#24314;&#30340;Imagick&#23545;&#35937;&#12290;<\/p><p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#28040;&#24687;&#19981;&#23436;&#25972;&#65292;&#25105;&#26080;&#27861;&#29702;&#35299;&#24744;&#35201;&#34920;&#36798;&#30340;&#20869;&#23481;&#12290;&#22914;&#26524;&#24744;&#33021;&#25552;&#20379;&#26356;&#22810;&#30340;&#20449;&#24687;&#25110;&#37325;&#26032;&#24320;&#22987;&#23545;&#35805;&#65292;&#35831;&#21578;&#35785;&#25105;&#12290;<code>resizeImageWithImagick<\/code>&#35813;&#20989;&#25968;&#25509;&#21463;&#19968;&#20010;&#22270;&#20687;&#30340;&#36335;&#24452;&#12289;&#19968;&#20010;&#26032;&#30340;&#23485;&#24230;&#20197;&#21450;&#19968;&#20010;&#26032;&#30340;&#39640;&#24230;&#20316;&#20026;&#21442;&#25968;&#12290;<\/p><p>&#35831;&#27880;&#24847;&#12290;<\/p><p>&#24322;&#24120;&#22788;&#29702;&#29992;&#20110;&#25429;&#33719;&#21644;&#26174;&#31034;&#22312;&#36807;&#31243;&#20013;&#21457;&#29983;&#30340;&#25152;&#26377;&#38169;&#35823;&#12290;<\/p><p>&#32553;&#25918;&#31639;&#27861;&#65288;&#65289;<code>$filterType<\/code>&#23427;&#21487;&#20197;&#26356;&#25913;&#20026;&#20854;&#20182;&#12290;<code>Imagick::FILTER_*<\/code>&#21462;&#20915;&#20110;&#24744;&#23545;&#36136;&#37327;&#21450;&#24615;&#33021;&#35201;&#27714;&#30340;&#24120;&#25968;&#12290;<\/p><p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#38382;&#39064;&#19981;&#23436;&#25972;&#65292;&#35831;&#25552;&#20379;&#26356;&#22810;&#30340;&#20449;&#24687;&#12290;<code>$newWidth<\/code>and &#26159;&ldquo;&#21644;&rdquo;&#30340;&#25340;&#38899;&#65292;&#34920;&#31034;&#24182;&#21015;&#20851;&#31995;&#30340;&#36830;&#35789;&#12290;<code>$newHeight<\/code>&#21442;&#25968;&#24212;&#26681;&#25454;&#24744;&#30340;&#36755;&#20986;&#22823;&#23567;&#36827;&#34892;&#35774;&#32622;&#12290;<\/p><p>&#30830;&#20445;&#22312;&#24744;&#30340;PHP&#35774;&#32622;&#20013;&#24050;&#23433;&#35013;&#24182;&#21551;&#29992;&#20102;Imagick&#25193;&#23637;&#12290;<\/p><h2>&#32467;&#35770;<\/h2><p>&#22312;PHP&#30340;GD&#24211;&#20013;&#36827;&#34892;&#22270;&#20687;&#32553;&#25918;&#38750;&#24120;&#31616;&#21333;&#12290;&#25353;&#29031;&#20197;&#19979;&#27493;&#39588;&#65292;&#24744;&#21487;&#20197;&#36731;&#26494;&#22320;&#23558;&#22270;&#20687;&#32553;&#25918;&#21151;&#33021;&#38598;&#25104;&#21040;&#24744;&#30340;PHP&#24212;&#29992;&#31243;&#24207;&#20013;&#12290;<\/p><\/body>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--3\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/xygcfznnzczhsdmwydzhsmzzzmddnq-400x300.jpg\" alt=\"&#38271;&#24037;&#30721;&#22900;1523\" class=\"wp-image-2906\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#38271;&#24037;&#30721;&#22900;1523<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312; PHP &#20013;&#35843;&#25972;&#22270;&#20687;&#22823;&#23567;&#36890;&#24120;&#28041;&#21450;&#21040;&#20004;&#20010;&#20027;&#35201;&#27493;&#39588;&#65306;&#39318;&#20808;&#65292;&#20320;&#38656;&#35201;&#21152;&#36733;&#22270;&#20687;&#25991;&#20214;&#65307;&#28982;&#21518;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992; PHP &#30340; GD &#22871;&#20214;&#26469;&#35843;&#25972;&#22270;&#20687;&#30340;&#23610;&#23544;&#12290;<\/p>\n<p>&#20197;&#19979;&#26159;&#22914;&#20309;&#20351;&#29992; PHP &#21152;&#36733;&#21644;&#35843;&#25972;&#22270;&#20687;&#22823;&#23567;&#30340;&#19968;&#20010;&#22522;&#26412;&#31034;&#20363;&#65306;<\/p>\n<ol>\n<li>&#39318;&#20808;&#65292;&#30830;&#20445;&#20320;&#30340;&#31995;&#32479;&#24050;&#32463;&#23433;&#35013;&#20102; PHP &#21644; GD &#22871;&#20214;&#12290;<\/li>\n<li>&#32534;&#20889; PHP &#20195;&#30721;&#26469;&#35835;&#21462;&#22270;&#20687;&#25991;&#20214;&#24182;&#23558;&#20854;&#36716;&#25442;&#20026;&#20108;&#36827;&#21046;&#25968;&#25454;&#12290;<\/li>\n<li>&#20351;&#29992; GD &#22871;&#20214;&#20013;&#30340; resize &#20989;&#25968;&#23558;&#22270;&#20687;&#32553;&#25918;&#33267;&#25351;&#23450;&#30340;&#23610;&#23544;&#12290;<\/li>\n<\/ol>\n<pre><code class=\"language-php\">&lt;?php\n\n\/\/ &#23450;&#20041;&#35201;&#22788;&#29702;&#30340;&#22270;&#20687;&#25991;&#20214;&#36335;&#24452;\n$imagePath = 'path\/to\/your\/image.jpg';\n\n\/\/ &#20351;&#29992; gd_info() &#33719;&#21462;&#22270;&#20687;&#20449;&#24687;\n$info = getimagesize($imagePath);\n\n\/\/ &#30830;&#20445;&#22270;&#20687;&#26159; JPEG &#25110; PNG &#31867;&#22411;\nif ($info['mime'] !== 'image\/jpeg' &amp;&amp; $info['mime'] !== 'image\/png') {\n    echo \"&#19981;&#25903;&#25345;&#30340;&#22270;&#20687;&#26684;&#24335;\";\n}\n\n\/\/ &#22914;&#26524;&#22270;&#20687;&#31867;&#22411;&#25903;&#25345;&#65292;&#32487;&#32493;&#36827;&#34892;&#35843;&#25972;\nelse {\n\n    \/\/ &#21152;&#36733;&#22270;&#20687;&#21040;&#20869;&#23384;\n    $image = imagecreatefromjpeg($imagePath);\n\n    \/\/ &#33719;&#21462;&#22270;&#20687;&#23485;&#24230;&#21644;&#39640;&#24230;\n    $width = imagesx($image);\n    $height = imagesy($image);\n\n    \/\/ &#35745;&#31639;&#26032;&#30340;&#23610;&#23544;&#65288;&#20363;&#22914;&#65292;500 x 500&#65289;\n    $newWidth = 500;\n    $newHeight = 500;\n\n    \/\/ &#35843;&#25972;&#22270;&#20687;&#23610;&#23544;\n    $newImage = imagecreatetruecolor($newWidth, $newHeight);\n\n    \/\/ &#23558;&#21407;&#22270;&#22797;&#21046;&#21040;&#26032;&#22270;&#19978;\n    imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);\n\n    \/\/ &#20445;&#23384;&#35843;&#25972;&#21518;&#30340;&#22270;&#29255;\n    imagejpeg($newImage, 'new_image.jpg');\n\n}\n?&gt;<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#25105;&#20204;&#39318;&#20808;&#26816;&#26597;&#22270;&#20687;&#26159;&#21542;&#25903;&#25345; JPEG &#25110; PNG &#26684;&#24335;&#12290;&#22914;&#26524;&#25903;&#25345;&#65292;&#25105;&#20204;&#23601;&#21152;&#36733;&#22270;&#20687;&#24182;&#23558;&#23427;&#36716;&#25442;&#20026;&#20869;&#23384;&#20013;&#30340;&#20108;&#36827;&#21046;&#25968;&#25454;&#12290;&#28982;&#21518;&#65292;&#25105;&#20204;&#35745;&#31639;&#20986;&#26032;&#30340;&#23610;&#23544;&#65292;&#24182;&#20351;&#29992; <code>imagecreatetruecolor()<\/code> &#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#22270;&#20687;&#23545;&#35937;&#12290;&#26368;&#21518;&#65292;&#25105;&#20204;&#23558;&#21407;&#22987;&#22270;&#20687;&#22797;&#21046;&#21040;&#26032;&#22270;&#20687;&#19978;&#65292;&#24182;&#20351;&#29992; <code>imagejpeg()<\/code> &#20989;&#25968;&#20445;&#23384;&#35843;&#25972;&#21518;&#30340;&#22270;&#20687;&#12290;<\/p>\n<p>&#27880;&#24847;&#65292;&#36825;&#20010;&#20363;&#23376;&#20551;&#35774;&#20320;&#30340;&#22270;&#20687;&#25991;&#20214;&#26159; <code>.jpg<\/code> &#25110; <code>.png<\/code> &#31867;&#22411;&#30340;&#12290;&#22914;&#26524;&#20320;&#30340;&#22270;&#20687;&#25991;&#20214;&#26159;&#20854;&#20182;&#26684;&#24335;&#65292;&#35831;&#30456;&#24212;&#22320;&#20462;&#25913;&#20195;&#30721;&#12290;&#27492;&#22806;&#65292;&#36825;&#20010;&#20363;&#23376;&#27809;&#26377;&#22788;&#29702;&#22270;&#20687;&#36136;&#37327;&#30340;&#38382;&#39064;&#65292;&#23454;&#38469;&#24212;&#29992;&#20013;&#21487;&#33021;&#38656;&#35201;&#32771;&#34385;&#22686;&#21152;&#25110;&#20943;&#23569;&#20687;&#32032;&#25968;&#37327;&#20197;&#20445;&#25345;&#22270;&#20687;&#36136;&#37327;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#22312;PHP&#20013;&#35843;&#25972;&#22270;&#20687;&#22823;&#23567;&#21487;&#20197;&#36890;&#36807;&#22810;&#31181;&#26041;&#27861;&#038;..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[42],"tuisongtax":[],"class_list":["post-671","my1js","type-my1js","status-publish","hentry","my1js2nav-xtywj"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=671"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=671"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=671"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}