如何在PHP中生成300X200维的图像缩略图?

如何在PHP中生成300X200维的图像缩略图?,php,thumbnails,phpthumb,Php,Thumbnails,Phpthumb,我使用下面的代码在PHP中生成图像缩略图。它生成与图像高度和宽度尺寸成比例的缩略图 make_thumb('images/image.jpg', 'images-generated-thumbs/7.jpg', 300, 200); function make_thumb($src, $dest, $desired_width, $desired_height) { /* read the source image */ $source_image = imagecreate

我使用下面的代码在PHP中生成图像缩略图。它生成与图像高度和宽度尺寸成比例的缩略图

make_thumb('images/image.jpg', 'images-generated-thumbs/7.jpg', 300, 200);

function make_thumb($src, $dest, $desired_width, $desired_height) {

    /* read the source image */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);

    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height*($desired_width/$width));
    $desired_width  = floor($width*($desired_height/$height));

    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image, $dest);
}
对于上面的示例,它生成大小为299x187的
7.jpg
缩略图。所以,我的问题是如何用白色填充其余的像素((300-299)x(300-187))。
如果我们删除上述代码中的
$desired_height
变量,它将准确地生成一个宽度为300的缩略图,因此只需用白色填充高度的其余部分

在修改宽度/高度之前,请存储它们:

$actual_width = $desired_width;
$actual_height = $desired_height;
$desired_height = floor($height*($desired_width/$width));
$desired_width  = floor($width*($desired_height/$height));
在绘制画布时:

/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($actual_width, $actual_height);
此时的虚拟图像为黑色,请将其填充为白色:

$white = imagecolorallocate($virtual_image, 255, 255, 255);
imagefill($virtual_image, 0, 0, $white );

为什么你需要那些尺寸为300*200的缩略图?