php裁剪图片白边,php缩略图填充白边的示例代码
//源圖的路徑,可以是本地文件,也可以是遠程圖片
$src_path = '1.jpg';
//最終保存圖片的寬
$width = 160;
//最終保存圖片的高
$height = 120;
//源圖對象
$src_image = imagecreatefromstring(file_get_contents($src_path));
$src_width = imagesx($src_image);
$src_height = imagesy($src_image);
//生成等比例的縮略圖
$tmp_image_width = 0;
$tmp_image_height = 0;
if ($src_width / $src_height >= $width / $height) {
$tmp_image_width = $width;
$tmp_image_height = round($tmp_image_width * $src_height / $src_width);
} else {
$tmp_image_height = $height;
$tmp_image_width = round($tmp_image_height * $src_width / $src_height);
}
$tmpImage = imagecreatetruecolor($tmp_image_width, $tmp_image_height);
imagecopyresampled($tmpImage, $src_image, 0, 0, 0, 0, $tmp_image_width, $tmp_image_height, $src_width, $src_height);
//添加白邊
$final_image = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($final_image, 255, 255, 255);
imagefill($final_image, 0, 0, $color);
$x = round(($width - $tmp_image_width) / 2);
$y = round(($height - $tmp_image_height) / 2);
imagecopy($final_image, $tmpImage, $x, $y, 0, 0, $tmp_image_width, $tmp_image_height);
//輸出圖片
header('Content-Type: image/jpeg');
imagejpeg($final_image);
總結
以上是生活随笔為你收集整理的php裁剪图片白边,php缩略图填充白边的示例代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 复调制细化分析matlab,基于复调制的
- 下一篇: http协议 php,PHP中的http