1
我目前使用GD php库来缩放一些图像的信箱风格,并用黑色填充产生的空白空间。然而,我需要使用另一个图像文件中的模式来填充空白区域。任何想法如何做到这一点?Letterbox图像并用另一个图像填充空白空间
这是我使用的缩放图像的代码:
function resize_image($source_image, $destination_width, $destination_height, $type = 0) {
// $type (1=crop to fit, 2=letterbox)
$source_width = imagesx($source_image);
$source_height = imagesy($source_image);
$source_ratio = $source_width/$source_height;
$destination_ratio = $destination_width/$destination_height;
if ($type == 1) {
// crop to fit
if ($source_ratio > $destination_ratio) {
// source has a wider ratio
$temp_width = (int)($source_height * $destination_ratio);
$temp_height = $source_height;
$source_x = (int)(($source_width - $temp_width)/2);
$source_y = 0;
} else {
// source has a taller ratio
$temp_width = $source_width;
$temp_height = (int)($source_width/$destination_ratio);
$source_x = 0;
$source_y = (int)(($source_height - $temp_height)/2);
}
$destination_x = 0;
$destination_y = 0;
$source_width = $temp_width;
$source_height = $temp_height;
$new_destination_width = $destination_width;
$new_destination_height = $destination_height;
} else {
// letterbox
if ($source_ratio < $destination_ratio) {
// source has a taller ratio
$temp_width = (int)($destination_height * $source_ratio);
$temp_height = $destination_height;
$destination_x = (int)(($destination_width - $temp_width)/2);
$destination_y = 0;
} else {
// source has a wider ratio
$temp_width = $destination_width;
$temp_height = (int)($destination_width/$source_ratio);
$destination_x = 0;
$destination_y = (int)(($destination_height - $temp_height)/2);
}
$source_x = 0;
$source_y = 0;
$new_destination_width = $temp_width;
$new_destination_height = $temp_height;
}
$destination_image = imagecreatetruecolor($destination_width, $destination_height);
if ($type > 1) {
imagefill($destination_image, 0, 0, imagecolorallocate($destination_image, 0, 0, 0));
}
imagecopyresampled($destination_image, $source_image, $destination_x, $destination_y, $source_x, $source_y, $new_destination_width, $new_destination_height, $source_width, $source_height);
return $destination_image;
}
谢谢
编辑------------------
我现在用这个代码工作完美,使用方法如下建议:
$destination_image = imagecreatetruecolor($destination_width, $destination_height);
if ($type > 1) {
if ($pattern != NULL) {
$pattern = imagecreatefrompng($pattern);
imagesettile($destination_image, $pattern);
imagefill($destination_image, 0, 0, IMG_COLOR_TILED);
} else {
imagefill($destination_image, 0, 0, imagecolorallocate($destination_image, 0, 0, 0));
}
}
imagecopyresampled($destination_image, $source_image, $destination_x, $destination_y, $source_x, $source_y, $new_destination_width, $new_destination_height, $source_width, $source_height);
return $destination_image;
$模式是文件路径的背景我法师填写信箱!
我从环顾四周猜测,我想使用像imagecopyresampled的东西,但我不知道我会怎么做。 – Nath5