0

以下のコードを使用して、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);
}

上記の例では7.jpg、サイズが 299x187 のサムネイルが生成されます。したがって、私の質問は、残りのピクセル ((300-299)x(300-187)) を白色で塗りつぶす方法です。上記のコードで変数を削除する$desired_heightと、正確に幅 300 のサムネイルが生成されるため、残りの高さを白色で埋めるだけで済みます。

4

1 に答える 1

2

幅/高さを変更する前に、それらを保存します。

$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 );
于 2014-03-13T18:36:51.017 に答える