35 lines
778 B
PHP
35 lines
778 B
PHP
<?
|
|
/*
|
|
$imgPath 源图片路径
|
|
$maxWidth 缩小后最大宽度
|
|
$maxHeight 缩小后最大高度
|
|
$destPath 目标路径,留空则直接输出
|
|
*/
|
|
function resizeImg($imgPath, $maxWidth, $maxHeight, $destPath = '') {
|
|
$img = imagecreatefromjpeg($imgPath);
|
|
$w = $w_o = imagesx($img);
|
|
$h = $h_o = imagesy($img);
|
|
|
|
if($w > $maxWidth) {
|
|
$h = intval($h*$maxWidth/$w);
|
|
$w = $maxWidth;
|
|
}
|
|
if($h > $maxHeight) {
|
|
$w = intval($w*$maxHeight/$h);
|
|
$h = $maxHeight;
|
|
}
|
|
//echo $w.",".$h;exit;
|
|
$img2 = imagecreatetruecolor($w, $h);
|
|
imagecopyresampled($img2, $img, 0, 0, 0, 0, $w, $h, $w_o, $h_o);
|
|
if($destPath) {
|
|
imagejpeg($img2, $destPath, 80);
|
|
} else {
|
|
header("Content-Type:image/jpeg");
|
|
imagejpeg($img2, '', 80);
|
|
}
|
|
|
|
imagedestroy($img2);
|
|
imagedestroy($img);
|
|
}
|
|
|
|
//resizeImg("1.jpg", 300, 300);
|