我想将1200x1200
图像转换为1200x600
。
但是,我不想失去图像比例,这就是为什么我想在左右两侧添加一个白色边框,并将图像放在在中间。
最终的图像将像左右两侧的边框和中间的方形图像,然后我将把它保存到文件夹中,并在我的项目中使用它。
这可能使用GD库吗?
您可以编写自己的数学函数来在特定维度的画布中显示图像。这是我使用的一个数学函数,
function resize_image($img,$maxwidth,$maxheight) {
//This function will return the specified dimension(width,height)
//dimension[0] - width
//dimension[1] - height
$dimension = array();
$imginfo = getimagesize($img);
$imgwidth = $imginfo[0];
$imgheight = $imginfo[1];
if($imgwidth > $maxwidth){
$ratio = $maxwidth/$imgwidth;
$newwidth = round($imgwidth*$ratio);
$newheight = round($imgheight*$ratio);
if($newheight > $maxheight){
$ratio = $maxheight/$newheight;
$dimension[] = round($newwidth*$ratio);
$dimension[] = round($newheight*$ratio);
return $dimension;
}else{
$dimension[] = $newwidth;
$dimension[] = $newheight;
return $dimension;
}
}elseif($imgheight > $maxheight){
$ratio = $maxheight/$imgheight;
$newwidth = round($imgwidth*$ratio);
$newheight = round($imgheight*$ratio);
if($newwidth > $maxwidth){
$ratio = $maxwidth/$newwidth;
$dimension[] = round($newwidth*$ratio);
$dimension[] = round($newheight*$ratio);
return $dimension;
}else{
$dimension[] = $newwidth;
$dimension[] = $newheight;
return $dimension;
}
}else{
$dimension[] = $imgwidth;
$dimension[] = $imgheight;
return $dimension;
}
}
假设您的图像大小为1200x1200(高:1200,宽:1200),画布大小(要显示图像的位置)为1200x600(高1200,宽600),您可以像这样调用resize_image函数,
$dimension = resize_image("{$your_image_path}.jpg",600,1200);
$width = $dimension[0];
$height = $dimension[1];
在得到合适的尺寸后,像这样显示你的图像,
<img src="your_image_path.jpg" alt="Image" height="<?php echo $height; ?>" width="<?php echo $width; ?>" />