簡體   English   中英

在PHP中調整任意圖像大小

[英]Arbitrary image resizing in PHP

調整圖像大小的最佳方法是什么,可以是任何尺寸,固定尺寸,或至少適合固定尺寸?

圖像來自不受我控制的隨機網址,我必須確保圖像不會超出大約250px x 300px或20%x 50%的區域。

我認為我會首先確定大小,如果它超出范圍,請調整大小,但我不確定如果圖像大小可能是任何東西,如何計算調整大小的邏輯。

編輯:我沒有本地訪問圖像,圖像網址是一個變量與img src = ...輸出,我需要一種方法來指定寬度和高度標簽的值。

使用ImageMagick很容易。 要么通過exec使用轉換命令行工具,要么使用http://www.francodacosta.com/phmagick/resizing-images
如果您使用'convert',您甚至可以告訴ImageMagick只調整較大的圖像並且不轉換較小的圖像: http//www.imagemagick.org/Usage/resize/

如果您沒有本地訪問權限,則無法使用ImageMagick:

<?php
$maxWidth  = 250;
$maxHeight = 500;

$size = getimagesize($url);
if ($size) {
    $imageWidth  = $size[0];
    $imageHeight = $size[1];
    $wRatio = $imageWidth / $maxWidth;
    $hRatio = $imageHeight / $maxHeight;
    $maxRatio = max($wRatio, $hRatio);
    if ($maxRatio > 1) {
        $outputWidth = $imageWidth / $maxRatio;
        $outputHeight = $imageHeight / $maxRatio;
    } else {
        $outputWidth = $imageWidth;
        $outputHeight = $imageHeight;
    }
}
?>

你看過GD庫文檔,特別是imagecopyresized方法嗎?

如果必須保持圖像的縱橫比不變,則應將其縮放一個因子,以使圖像的較長邊(高度或寬度)符合該邊的限制。

您可以使用以下內容:

$maxWidth  = 250;
$maxHeight = 500;

$validSize = true;
$imageinfo = getimagesize($filename);
if ($imageinfo) {
    $validSize &= $imageinfo[0] <= $maxWidth;
    $validSize &= $imageinfo[1] <= $maxHeight;
}

&=是的組合操作者按位與運算符&賦值運算符= 因此$foo &= *expr*$foo = $foo & *expr*是等價的。

我不確定你答案的確切解決方案,但我知道用PHP編寫的項目有解決方案。 去看看為Drupal CMS構建的ImageCache,它是用PHP編寫的。

它基本上讓你定義一些動作來拍攝任意圖像,並產生你想要的任何縮放/裁剪圖像。

您必須學習一些Drupal API以便能夠理解這個示例,但它很廣泛,如果您了解它們的算法,您將能夠解決其他更復雜的問題。

我創建了以下功能來根據比率智能調整圖像大小,甚至還有一個參數來放大較小的圖像(如果你的HTML布局在縮略圖尺寸很大的情況下搞砸了,這一點至關重要)。

function ImageIntelligentResize( $imagePath, $maxWidth, $maxHeight, $alwaysUpscale )
{
    // garbage in, garbage out
    if ( IsNullOrEmpty($imagePath) || !is_file($imagePath) || IsNullOrEmpty($maxWidth) || IsNullOrEmpty($maxHeight) )
    {
        return array("width"=>"", "height"=>"");
    }

    // if our thumbnail size is too big, adjust it via HTML
    $size = getimagesize($imagePath);
    $origWidth = $size[0];
    $origHeight = $size[1];

    // Check if the image we're grabbing is larger than the max width or height or if we always want it resized
    if ( $alwaysUpscale || $origWidth > $maxWidth || $origHeight > $maxHeight )
    {   
        // it is so let's resize the image intelligently
        // check if our image is landscape or portrait
        if ( $origWidth > $origHeight )
        {
            // target image is landscape/wide (ex: 4x3)
            $newWidth = $maxWidth;
            $ratio = $maxWidth / $origWidth;
            $newHeight = floor($origHeight * $ratio);
            // make sure the image wasn't heigher than expected
            if ($newHeight > $maxHeight)
            {
                // it is so limit by the height
                $newHeight = $maxHeight;
                $ratio = $maxHeight / $origHeight;
                $newWidth = floor($origWidth * $ratio);
            }
        }
        else
        {
            // target image is portrait/tall (ex: 3x4)
            $newHeight = $maxHeight;
            $ratio = $maxHeight / $origHeight;
            $newWidth = floor($origWidth * $ratio);
            // make sure the image wasn't wider than expected
            if ($newWidth > $maxWidth)
            {
                // it is so limit by the width
                $newWidth = $maxWidth;
                $ratio = $maxWidth / $origWidth;
                $newHeight = floor($origHeight * $ratio);
            }
        }
    }
    // it's not, so just use the current height and width
    else
    {
        $newWidth = $origWidth;
        $newHeight = $origHeight;
    }   

    return array("width"=>$newWidth, "height"=>$newHeight);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM