Php 添加黑条以创建16x9图像

Php 添加黑条以创建16x9图像,php,css,image,image-processing,Php,Css,Image,Image Processing,我的服务器上有一个jpg。我用 imagecreatefromjpeg($imgPath); 打开它。我想通过在顶部+底部或左侧+右侧添加黑色条,使其成为16x9图像。(想想背景大小:contain;背景位置:center;)这就是我的全部: $img_info = getimagesize($imgPath); 我知道我需要使用 Image CeCueTraceCoor 来制作空白图像, Image EngopyReSimule创建图像,以及 IGEJPEGG/以保存它。但我不知道如何把它

我的服务器上有一个jpg。我用

imagecreatefromjpeg($imgPath);
打开它。我想通过在顶部+底部或左侧+右侧添加黑色条,使其成为16x9图像。(想想
背景大小:contain;背景位置:center;
)这就是我的全部:

$img_info = getimagesize($imgPath);

我知道我需要使用<代码> Image CeCueTraceCoor 来制作空白图像,<代码> Image EngopyReSimule创建图像,以及<代码> IGEJPEGG/<代码>以保存它。但我不知道如何把它们组合在一起。谢谢

这将实现以下目的:

$im=imagecreatefromjpeg ($imgPath);
$width=ImageSX($im); $height=ImageSY($im); $ratio=16/9;
$width_out=$width; $height_out=$height;
if ($height_out*$ratio<$width_out) {$height_out=floor($width_out/$ratio);} else {$width_out=floor($height_out*$ratio);}
$left=round(($width_out-$width)/2);
$top=round(($height_out-$height)/2);
$image_out = imagecreatetruecolor($width_out,$height_out);
$bg_color = ImageColorAllocate ($image_out, 0, 0, 0);
imagefill($image_out,0,0,$bg_color);
imagecopy($image_out, $im, $left, $top, 0, 0, $width,$height);
imagejpeg($image_out);
$im=imagecreatefromjpeg($imgPath);
$width=ImageSX($im)$高度=图像系统($im)$比率=16/9;
$width\u out=$width$高度_out=$height;
如果($height\u out*$比率