Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调整图像大小并保持纵横比以适合iPhone的算法_Php_Algorithm_Image_Resize_Aspect Ratio - Fatal编程技术网

Php 调整图像大小并保持纵横比以适合iPhone的算法

Php 调整图像大小并保持纵横比以适合iPhone的算法,php,algorithm,image,resize,aspect-ratio,Php,Algorithm,Image,Resize,Aspect Ratio,我正在为iPhone应用程序创建一个与之交互的web服务 当我的客户端在服务器端上传图像时,我希望我的php脚本调整图像的大小,同时保持纵横比,以便它能够适应iPhone屏幕。(也就是说,最长的部分是我认为下面应该告诉你这个想法。它不是用任何特定的语言编写的,而是一个类似C的伪代码 shortSideMax = 640; longSideMax = 960; function Resize(image) { if (image.width >= image.height)

我正在为iPhone应用程序创建一个与之交互的web服务


当我的客户端在服务器端上传图像时,我希望我的php脚本调整图像的大小,同时保持纵横比,以便它能够适应iPhone屏幕。(也就是说,最长的部分是我认为下面应该告诉你这个想法。它不是用任何特定的语言编写的,而是一个类似C的伪代码

shortSideMax = 640;
longSideMax = 960;
function Resize(image)
{
    if (image.width >= image.height)
    {
        if (image.width <= longSideMax && image.height <= shortSideMax)
            return image;  // no resizing required
        wRatio = longSideMax / image.width;
        hRatio = shortSideMax / image.height;
    }
    else
    {
        if (image.height <= longSideMax && image.width <= shortSideMax)
            return image; // no resizing required
        wRatio = shortSideMax / image.width;
        hRatio = longSideMax / image.height;
    }

    // hRatio and wRatio now have the scaling factors for height and width.
    // You want the smallest of the two to ensure that the resulting image
    // fits in the desired frame and maintains the aspect ratio.
    resizeRatio = Min(wRatio, hRatio);

    newHeight = image.Height * resizeRatio;
    newWidth = image.Width * resizeRatio;

    // Now call function to resize original image to [newWidth, newHeight]
    // and return the result.
}
shortSideMax=640;
longSideMax=960;
函数调整大小(图像)
{
如果(image.width>=image.height)
{

如果(image.width,可能稍微短一点的例行程序是:

// Calculate resize ratios for resizing 
float ratioW = targetWidth / oldWidth; 
float ratioH = targetHeight / oldHeight;

// smaller ratio will ensure that the image fits in the view
float ratio = ratioW < ratioH?ratioW:ratioH;

newWidth = oldWidth*ratio;
newHeight = oldHeight*ratio;
//计算调整大小的大小比率
浮动比率=目标宽度/旧宽度;
浮动比率OH=目标高度/旧高度;
//较小的比例将确保图像适合视图
浮动比率=比率<比率?比率:比率;
新宽度=旧宽度*比率;
新高度=旧高度*比率;

很明显,如果比例大于1,那么它会增大,如果小于1,那么它会缩小。

下面是我知道的保持比例的最简单方法。希望能有所帮助

Javascript

function resize(width, height, maxWidth, maxHeight) {
    var ratio = Math.min(maxWidth / width, maxHeight / height);
    var newWidth = ratio * width;
    var newHeight = ratio * height;

    console.log(newWidth + ' ' + newHeight); // Test

    // Process resizing...
}

resize(1280, 1024, 600, 300);
PHP

function resize($width, $height, $maxWidth, $maxHeight) {
    $ratio = min(array($maxWidth / $width, $maxHeight / $height));
    $newWidth = $ratio * $width;
    $newHeight = $ratio * $height;

    echo $newWidth . ' ' . $newHeight; // Test

    // Process resizing...
}

resize(1600, 1280, 150, 150);

任何人从Google进入本页面,寻找方面填充而不是方面匹配,只需切换比率选择代码即可,即:

吉姆的回答是:

resizeRatio = Min(wRatio, hRatio); //Aspect Fit
float ratio = ratioW < ratioH?ratioW:ratioH; //Aspect Fit
变成

resizeRatio = Max(wRatio, hRatio); //Aspect Fill
float ratio = ratioW > ratioH?ratioW:ratioH; //Aspect Fill
德夫罗德的回答是:

resizeRatio = Min(wRatio, hRatio); //Aspect Fit
float ratio = ratioW < ratioH?ratioW:ratioH; //Aspect Fit

与DevProd的答案类似,但由于命名约定,我自己也很难遵循它。希望这更清楚一点:

如何将一些媒体(照片)放入容器中?


唯一的问题是在检查w>960后立即采取行动,并假设新的w=h*(新的w/w)我有几乎完全相同的算法,但很高兴看到其他人对这个问题的解决方案——值得称赞的是,实际上对图像执行大小调整操作将使算法的效率变得毫无意义。当图像的高度为800,宽度为600,而shortsidemax为275(垂直)时,这个算法对我不起作用longsidemax为480(水平)@ManishJain:那么,你应该发布一个问题,展示你的尝试和你得到的结果。一定要将这个答案联系起来,并解释它不起作用。我只想指出,在方面匹配和方面填充之间进行更改非常简单。对于方面填充,只需填写符号:
float ratio=ratioW>ratioH?ratioW:ratioH;
这比其他答案更简单。使用
Math.min()
进行“拟合”,使用
Math.max()
进行“填充”。