PHP按比例调整图像大小 [英] PHP resize image proportionally to a bigger size

查看:124
本文介绍了PHP按比例调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个脚本,该脚本显示移动应用中网站的缩略图,我遇到视网膜显示问题。如果原始图像足够大,我会显示所需大小的两倍的缩略图,如果不是,我会按所需大小显示。现在,我的函数检查它是否可以按比例调整大小,如果不能,我将其调整为min-width或min-height并从中心裁剪。

I'm currently working on a script that shows thumbnails from a website on a mobile app, and I'm having problems with the "retina display". If the original image is big enough, I show a thumbnail with the double of the required size, and if it is not, I show it on the required size. Now, my function checks if it can be resized proportionally and if it can't, I resize it to the "min-width" or "min-height" and crop it from the center.

问题在于:如果它检测到图像无法以尺寸的两倍显示,则向上扩展裁剪尺寸,直到达到原始尺寸的极限,我不能找到一种正确的方法。 (我的主要问题是我不擅长数学:P)。

Here is the problem: if it detects that the image can't be shown on the double of size, "scale up" the cropped size until it reaches the limits of the original size, and I can't find a way to scale it right. (My main problem is that I'm not really good at maths :P).

更简单的讲座:


  • 这是图片的原始尺寸: 600 x 301

  • 这是必需/裁剪尺寸: 320 x 180

  • 这是我想要的尺寸: 535 x 301 。我是从Photoshop获得的,它是缩放320x180直到找到原始大小限制的结果。

  • This is the original size of the image: 600 x 301
  • This is the required/cropped size: 320 x 180
  • This is the size I want to get: 535 x 301. I got it from Photoshop, and is the result of scaling 320x180 up until it finds the limit of the original size.

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

PS: I know GD, so I only need the formula to calculate the size.

推荐答案

缩小相同的算法也可以扩展。这是未经测试的代码,它只是为了缩小,但如果你删除缩小测试,它可能会为你做到这一点。

The same algorithm that scales down can also scale up. This is untested code, and it is only designed to scale down, but if you remove the "scale down" tests it might do the trick for you.

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

这篇关于PHP按比例调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆