缩略图

1.简单缩略图

$src_img = imagecreatefromjpeg('./src.jpg');
$dst_img = imagecreatetruecolor(100, 100);

//2
//采样-拷贝-修改大小
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, 100, 100, 500, 367);

//3
header('Content-Type:image/jpeg');
imagejpeg($dst_img);

//4
imagedestroy($src_img);
imagedestroy($dst_img);

2等比缩略图

600 x 100   缩略图  500 x 500
6/5 > 1/5 以宽为准 500 : 500x (1/6)


$path = $_SERVER['DOCUMENT_ROOT']; $src_file = $path.'/1111111111111/333/aaas.jpg';//原始文件路径 $max_w = 1500;//缩略图最大宽 $max_h = 1500;//缩略图最大高 //得到原始的宽高 $src_info = getimagesize($src_file); $src_w = $src_info[0];//元素0,为宽 $src_h = $src_info[1];//元素1,为高 //计算 宽之比 和 高之比 $scale_w = $src_w/$max_w; $scale_h = $src_h/$max_h; //比较 宽之比 和 高之比 if($scale_w > $scale_h) { $dst_w = $max_w; $dst_h = $dst_w * ($src_h/$src_w); } else { $dst_h = $max_h; $dst_w = $dst_h * ($src_w/$src_h); } //1 //创建原始和目标(缩略图) $src_img = imagecreatefromjpeg($src_file); $dst_img = imagecreatetruecolor($dst_w, $dst_h); //2 //采样-拷贝-修改大小 imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h); //3 header('Content-Type:image/jpeg'); imagejpeg($dst_img);


保存图片
imagejpeg($dst_img,'./aa.jpg');
imagegif($dst_img,'./aa.gif');

//4 imagedestroy($src_img); imagedestroy($dst_img);

3补白缩略图

//1
//创建原始和目标(缩略图)
$src_img = imagecreatefromjpeg($src_file);
$dst_img = imagecreatetruecolor($max_w,    $max_h);

//2
//将图片背景设置为白色
$bg_color = imagecolorallocate($dst_img, 0xff, 0xff, 0xff);
imagefill($dst_img, 0, 0, $bg_color);
//采样-拷贝-修改大小
imagecopyresampled($dst_img, $src_img, ($max_w-$dst_w)/2, ($max_h-$dst_h)/2, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
原文地址:https://www.cnblogs.com/suxiaolong/p/5949193.html