2

現在、モバイルアプリでウェブサイトのサムネイルを表示するスクリプトを作成していますが、「Retinaディスプレイ」に問題があります。元の画像が十分に大きい場合は、必要なサイズの2倍のサムネイルを表示し、そうでない場合は、必要なサイズで表示します。これで、関数は比例してサイズ変更できるかどうかをチェックし、サイズ変更できない場合は、「最小幅」または「最小高さ」にサイズ変更して、中央からトリミングします。

問題は次のとおりです。画像を2倍のサイズで表示できないことが検出された場合、元のサイズの制限に達するまでトリミングされたサイズを「拡大」し、拡大縮小する方法を見つけることができません。右。(私の主な問題は、私が数学があまり得意ではないということです:P)。

より簡単な講義の場合:

  • これは画像の元のサイズです:600 x 301
  • これは必要な/トリミングされたサイズです:320 x 180
  • これは私が取得したいサイズです:535x301。Photoshopから入手しました。これは、元のサイズの制限が見つかるまで320x180を拡大した結果です。

PS:GDを知っているので、サイズを計算するための式だけが必要です。

4

2 に答える 2

3

スケールダウンするのと同じアルゴリズムでスケールアップすることもできます。これはテストされていないコードであり、スケールダウンするように設計されていますが、「スケールダウン」テストを削除すると、うまくいく可能性があります。

<?php // RAY_image_resize_and_crop.php
error_reporting(E_ALL);


// RESIZE AN IMAGE PROPORTIONALLY AND CROP TO THE CENTER


function resize_and_crop($original_image_url, $thumb_image_url, $thumb_w, $thumb_h, $quality=75)
{
    // ACQUIRE THE ORIGINAL IMAGE: http://php.net/manual/en/function.imagecreatefromjpeg.php
    $original = imagecreatefromjpeg($original_image_url);
    if (!$original) return FALSE;

    // GET ORIGINAL IMAGE DIMENSIONS
    list($original_w, $original_h) = getimagesize($original_image_url);

    // RESIZE IMAGE AND PRESERVE PROPORTIONS
    $thumb_w_resize = $thumb_w;
    $thumb_h_resize = $thumb_h;
    if ($original_w > $original_h)
    {
        $thumb_h_ratio  = $thumb_h / $original_h;
        $thumb_w_resize = (int)round($original_w * $thumb_h_ratio);
    }
    else
    {
        $thumb_w_ratio  = $thumb_w / $original_w;
        $thumb_h_resize = (int)round($original_h * $thumb_w_ratio);
    }
    if ($thumb_w_resize < $thumb_w)
    {
        $thumb_h_ratio  = $thumb_w / $thumb_w_resize;
        $thumb_h_resize = (int)round($thumb_h * $thumb_h_ratio);
        $thumb_w_resize = $thumb_w;
    }

    // CREATE THE PROPORTIONAL IMAGE RESOURCE
    $thumb = imagecreatetruecolor($thumb_w_resize, $thumb_h_resize);
    if (!imagecopyresampled($thumb, $original, 0,0,0,0, $thumb_w_resize, $thumb_h_resize, $original_w, $original_h)) return FALSE;

    // ACTIVATE THIS TO STORE THE INTERMEDIATE IMAGE
    // imagejpeg($thumb, 'RAY_temp_' . $thumb_w_resize . 'x' . $thumb_h_resize . '.jpg', 100);

    // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
    $final = imagecreatetruecolor($thumb_w, $thumb_h);

    $thumb_w_offset = 0;
    $thumb_h_offset = 0;
    if ($thumb_w < $thumb_w_resize)
    {
        $thumb_w_offset = (int)round(($thumb_w_resize - $thumb_w) / 2);
    }
    else
    {
        $thumb_h_offset = (int)round(($thumb_h_resize - $thumb_h) / 2);
    }

    if (!imagecopy($final, $thumb, 0,0, $thumb_w_offset, $thumb_h_offset, $thumb_w_resize, $thumb_h_resize)) return FALSE;

    // STORE THE FINAL IMAGE - WILL OVERWRITE $thumb_image_url
    if (!imagejpeg($final, $thumb_image_url, $quality)) return FALSE;
    return TRUE;
}


// USE CASE
echo '<a target="_blank" href="RAY_orig_600x374.jpg">Original 600x374</a><br/>';

resize_and_crop('RAY_orig_600x374.jpg', 'RAY_temp_100x100.jpg', 100, 100);
echo '<a target="_blank" href="RAY_temp_100x100.jpg">100x100</a><br/>';

resize_and_crop('RAY_orig_600x374.jpg', 'RAY_temp_200x100.jpg', 200, 100);
echo '<a target="_blank" href="RAY_temp_200x100.jpg">200x100</a><br/>';

resize_and_crop('RAY_orig_600x374.jpg', 'RAY_temp_200x300.jpg', 200, 300);
echo '<a target="_blank" href="RAY_temp_200x300.jpg">200x300</a><br/>';
于 2012-12-31T00:16:28.050 に答える
2

(OPに代わって投稿):

Ray Paseurのおかげで、私はこの関数を手に入れました:

function getAppropriateDimensionsForRetina($originalWidth,$originalHeight,$width,$height){
    $newWidth = $originalWidth;
    $newHeigth = $originalHeight;
    if($originalWidth > $originalHeight){
        $heightRatio = $originalHeight / $height;
        $newWidth = (int)floor($width * $heightRatio);
    }else{
        $widthRatio = $originalWidth / $width;
        $newHeigth = (int)floor($height * $widthRatio);
    }
    return array('width' => $newWidth,'height' => $newHeigth);
} 
于 2012-12-31T11:06:26.503 に答える