2

私は imagecopyresampled でいくつかの問題を抱えています。主に、画像が正しくスケーリングされていないこと、画像の位置が間違っていること、およびエッジの周りに黒い境界線があることです。

次の変数を設定しました

    $tW = $width; // Original width of image
    $tH = $height; // Orignal height of image

    $w = postvar; // New width
    $h = postvar; // New height
    $x = postvar; // New X pos
    $y = postvar; // New Y pos

そして、次を実行します

    $tn = imagecreatetruecolor($w, $h);
    $image = imagecreatefromjpeg('filepathhere.jpeg');
    imagecopyresampled($tn, $image, 0, 0, $x, $y, $w, $h, $tW, $tH);

誰かが手がかりを持っているなら、それは大きな助けになるでしょう! ありがとう

4

1 に答える 1

3

ここにはいくつかの問題があります。まず、指定した新しい高さと幅で新しい画像を作成するのではなく、元の画像の比率に基づいてそれらがどうあるべきかを計算する必要があります。そうしないと、拡大縮小された画像が歪んでしまいます。たとえば、次のコードは、次の長方形に収まる適切なサイズの画像を作成します$w x $h

$tW = $width;    //original width
$tH = $height;   //original height

$w = postvar;
$h = postvar;

if($w == 0 || $h == 0) {
    //error...
    exit;
}

if($tW / $tH > $w / $h) {
    // specified height is too big for the specified width
    $h = $w * $tH / $tW;
}
elseif($tW / $tH < $w / $h) {
    // specified width is too big for the specified height
    $w = $h * $tW / $tH;
}

$tn = imagecreatetruecolor($w, $h);  //this will create it with black background
imagefill($tn, 0, 0, imagecolorallocate($tn, 255, 255, 255));    //fill it with white;

//now you can copy the original image:
$image = imagecreatefromjpeg('filepathhere.jpeg');
//next line will just create a scaled-down image
imagecopyresampled($tn, $image, 0, 0, 0, 0, $w, $h, $tW, $tH);

ここで、元の画像の特定の部分のみをコピーする場合、たとえば座標($x, $y)から右隅にコピーする場合は、それを計算に含める必要があります。

$tW = $width - $x;    //original width
$tH = $height - $y;   //original height

$w = postvar;
$h = postvar;

if($w == 0 || $h == 0) {
    //error...
    exit;
}

if($tW / $tH > $w / $h) {
    // specified height is too big for the specified width
    $h = $w * $tH / $tW;
}
elseif($tW / $tH < $w / h) {
    // specified width is too big for the specified height
    $w = $h * $tW / $tH;
}

$tn = imagecreatetruecolor($w, $h);  //this will create it with black background
imagefill($tn, 0, 0, imagecolorallocate($tn, 255, 255, 255));    //fill it with white;

//now you can copy the original image:
$image = imagecreatefromjpeg('filepathhere.jpeg');
//next line will create a scaled-down portion of the original image from coordinates ($x, $y) to the lower-right corner
imagecopyresampled($tn, $image, 0, 0, $x, $y, $w, $h, $tW, $tH);

あなたが達成しようとしていることについてもっと詳しく教えていただければ、私はさらに手助けできるかもしれません。

于 2012-07-04T15:01:38.887 に答える