<?php /** * 获取等比的缩略图 * @param $filename string 原始文件路径 * @param $dstWidth 缩略图最大宽度 * @param $dstHight 缩略图最大高度 * @return string 缩略图路径 */ function thumb_pic($filename, $dstWidth, $dstHeight) { $srcImg = imagecreatefromjpeg($filename); $srcInfo = getimagesize($filename); //得到宽高比 $srcScale = $srcInfo[0]/$srcInfo[1]; $dstScale = $dstWidth/$dstHeight; if ($srcScale > $dstScale) { $sampleWidth = $dstWidth; $sampleHeight = $dstHeight/$srcScale; } else { $sampleWidth = $dstHeight; $sampleHeight = $dstWidth*$srcScale; } $dstImg = imagecreatetruecolor($sampleWidth, $sampleHeight); // 制作缩略图 imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $sampleWidth, $sampleHeight, $srcInfo[0], $srcInfo[1]); imagejpeg($dstImg,'thumb_1.jpg'); } thumb_pic('1.jpg', 100, 50);