【问题标题】:PHP resize image proportionally to a bigger sizePHP将图像按比例调整为更大的尺寸
【发布时间】:2012-12-15 06:52:19
【问题描述】:

我目前正在编写一个在移动应用上显示网站缩略图的脚本,但我遇到了“视网膜显示屏”问题。如果原始图像足够大,我会以所需尺寸的两倍显示缩略图,如果不是,我会以所需尺寸显示。现在,我的函数检查它是否可以按比例调整大小,如果不能,我将其调整为“最小宽度”或“最小高度”并从中心裁剪。

问题是:如果它检测到图像无法以两倍大小显示,则“放大”裁剪后的大小,直到达到原始大小的限制,而我找不到方法正确缩放它。 (我的主要问题是我不太擅长数学:P)。

更简单的讲座:

  • 这是图片的原始尺寸:600 x 301
  • 这是所需/裁剪后的尺寸:320 x 180
  • 这是我想要的尺寸:535 x 301。我是从 Photoshop 中得到的,它是放大 320x180 直到找到原始大小的限制的结果。

PS:我知道GD,所以我只需要公式来计算大小。

【问题讨论】:

  • 使用 320/180 的目标纵横比。所以如果你知道你的高度是 301,那么你的宽度就是 (301 * (320/180))。
  • 谢谢,但我已经用过@RayPaseur 回答

标签: php image-processing resize gd scale


【解决方案1】:

(代表 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);
} 

【讨论】:

    【解决方案2】:

    缩小规模的相同算法也可以扩大规模。这是未经测试的代码,它仅设计用于缩减,但如果您删除“缩减”测试,它可能会为您解决问题。

    <?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/>';
    

    【讨论】:

    • 其实……它可能没有任何按比例缩小的测试。试一试,如果您有任何问题,请回复。
    • 谢谢,它有效,我根据您发布的功能创建了一个功能,我会将其添加到问题中。再次感谢!
    猜你喜欢
    • 2013-04-11
    • 2019-06-12
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    相关资源
    最近更新 更多