0

次のコードがあります。

$biggest = ($width > $height) ? $width : $height;
    $newWidth = 0;
    $newHeight = 0;

    if($biggest > $divSize){
        echo "BIGGEST<br />";
        $scale = $divSize/$biggest;
        $newWidth = floor($width * $scale);
        $newHeight = floor($height * $scale);
    } else if($biggest < $divSize){
        echo "DIVSIZE<br />";
        $scale = $biggest/$divSize;
        $newWidth = floor($width * $scale);
        $newHeight = floor($height * $scale);
    } 

    echo "SCALE: ".$scale."<br />";
    echo "BIGGEST: ".$biggest."<br />";
    echo "WIDTH: ".$width."<br />";
    echo "HEIGHT: ".$height."<br />";
    echo "NEWWIDTH: ".$newWidth."<br />";
    echo "NEWHEIGHT: ".$newHeight."<br />";

    $sourceImage = imagecreatefromstring(file_get_contents($fileName)); 
    $thumb = imagecreatetruecolor($newWidth, $newHeight);           
    imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    imagedestroy($sourceImage);

このコードは一部の画像では正常に機能しますが、すべての画像では機能しません。

64 x 64 のサイズの div があります。

一部の画像では完全にうまくスケーリングされますが、一部の画像では出力画像の高さも 64px であり、たとえば 32px である必要があります。

この問題の原因がわかりません。

さらに詳しい情報が必要な場合は、お問い合わせください。

4

2 に答える 2

3

あなたの機能は優れていますが、画像のサイズを静的にする必要がある場合があります (これにより、一部の Web ページのデザインが壊れることがなくなります)。

そんな時にこの機能が使えます。定義された幅/高さに収まるように画像のサイズを変更し、画像が必要なサムネイルと同じ比率でない場合、未使用の空き領域は透明に設定されます。

function resizePreservingAspectRatio($img, $targetWidth, $targetHeight)
{
    $srcWidth = imagesx($img);
    $srcHeight = imagesy($img);

    // Determine new width / height preserving aspect ratio
    $srcRatio = $srcWidth / $srcHeight;
    $targetRatio = $targetWidth / $targetHeight;
    if (($srcWidth <= $targetWidth) && ($srcHeight <= $targetHeight))
    {
        $imgTargetWidth = $srcWidth;
        $imgTargetHeight = $srcHeight;
    }
    else if ($targetRatio > $srcRatio)
    {
        $imgTargetWidth = (int) ($targetHeight * $srcRatio);
        $imgTargetHeight = $targetHeight;
    }
    else
    {
        $imgTargetWidth = $targetWidth;
        $imgTargetHeight = (int) ($targetWidth / $srcRatio);
    }

    // Creating new image with desired size
    $targetImg = imagecreatetruecolor($targetWidth, $targetHeight);

    // Add transparency if your reduced image does not fit with the new size
    $targetTransparent = imagecolorallocate($targetImg, 255, 0, 255);
    imagefill($targetImg, 0, 0, $targetTransparent);
    imagecolortransparent($targetImg, $targetTransparent);

    // Copies image, centered to the new one (if it does not fit to it)
    imagecopyresampled(
       $targetImg, $img, ($targetWidth - $imgTargetWidth) / 2, // centered
       ($targetHeight - $imgTargetHeight) / 2, // centered
       0, 0, $imgTargetWidth, $imgTargetHeight, $srcWidth, $srcHeight
    );

    return $targetImg;
}

使用例:

$gd = imagecreatefromjpeg("images/image5.jpg");
$resized = resizePreservingAspectRatio($gd, 100, 100);
header("Content-type: image/png");
imagepng($resized);

この画像 :

ここに画像の説明を入力

になります:

ここに画像の説明を入力

于 2013-04-12T22:03:31.497 に答える