一个根据宽高补白的缩略图

<?php

//获取原图的目录
$file='./src.jpg';

//有一个原图创建一个新的图像
$file_img=imagecreatefromjpeg($file);
//缩略图的大小
$temp_w=100;
$temp_h=100;
//创建缩略图的画布
$temp_img=imagecreatetruecolor($temp_w, $temp_h);


//填充颜色
$bg_color=imagecolorallocate($temp_img,0xff, 0xff, 0xff);
imagefill($temp_img,0,0,$bg_color);//填充的区域


//采集区域
$src_area_x=0;
$src_area_y=0;
$src_area_w = $src_w = imageSx($file_img);
$src_area_h = $src_h = imagesy($file_img);

//判断
//  宽之比与高之比的关系
if ($src_w/$temp_w > $src_h/$temp_h) {
 // 宽缩放大,宽标准
 $temp_area_w = $temp_w;
 $temp_area_h = $src_h/$src_w * $temp_area_w;//
} else {
 // 高缩放大,高标准
 $temp_area_h = $temp_h;
 $temp_area_w = $src_w/$src_h * $temp_area_h;
}
$temp_area_x = ($temp_w-$temp_area_w) / 2;
$temp_area_y = ($temp_h-$temp_area_h) / 2;

//输出
//imagecopyresampled($temp_img, $file_img, $temp_area_x, $temp_area_y, $src_area_x, $src_area_y, $temp_w, $temp_h, $src_w, $src_h);

imagecopyresampled($temp_img, $file_img, $temp_area_x, $temp_area_y, $src_area_x, $src_area_y, $temp_area_w, $temp_area_h, $src_area_w, $src_area_h);
//定义输出的文件
header('content-Type:image/jpeg');

imagejpeg($temp_img);

imagedestroy($file_img);
imagedestroy($temp_img);

原文地址:https://www.cnblogs.com/love1226/p/4502218.html