950x335 縮圖成 610x228 (取較大的一邊)
![](/sysdata/modules/mod_step/media/357/fa20e1f7abe9a3189fcabf5a9f5d4c29_l.jpg)
// 目的地尺寸及檔案 $size = "326x228"; $output_file = "thumb.jpg"; list($resizeW, $resizeH) = split("x", $size); // 先抓出圖片的=長寬資訊 @list($width, $height, $type, $attr) = getimagesize( $source_img ); // 確認是否有抓到正確的檔案 if($resizeW!=0 && $resizeH!=0) { // 找出正確的輸出比例 $aspect_ratio = $resizeW / $resizeH; $factor = (($width/$height) > $aspect_ratio)?($width/$resizeW):($height/$resizeH); $newWidth = round($width / $factor); $newHeight = round($height / $factor); $offset_w = round(abs( $newWidth - $resizeW ) / 2); $offset_h = round(abs( $newHeight - $resizeH ) / 2); $offset_w = 0; $offset_h = 0; $cmd = 'convert '.$source_img.' -thumbnail '.$size.'^ -crop '.$size.'+'.$offset_w.'+'.$offset_h.' '.$output_file; pclose(popen("$cmd ", 'r')); }