簡體   English   中英

使用Php調整圖像大小和裁剪

[英]Image resizing and cropping with Php

我正在使用php腳本上傳和調整圖像大小,非常簡單:

if($_SERVER["REQUEST_METHOD"] == "POST") {
    $image = $_FILES["image_upload"];
    $uploadedfile = $image['tmp_name'];

    if ($image) {
        $filename = stripslashes($_FILES['image_upload']['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
        if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
            $error_txt = 'Immagine incoretta';
            $errors=1;
        } else {
            $size=filesize($uploadedfile);
            if ($size > MAX_SIZE*1024) {
                $error_txt = "Immagine troppo grande";
                $errors=1;
            }
            if($extension=="jpg" || $extension=="jpeg" ) {
                $uploadedfile = $uploadedfile;
                $src = imagecreatefromjpeg($uploadedfile);
            } else if($extension=="png") {
                $uploadedfile = $uploadedfile;
                $src = imagecreatefrompng($uploadedfile);
            } else {
                $src = imagecreatefromgif($uploadedfile);
            }

            list($width,$height)=getimagesize($uploadedfile);

            $newwidth=500;
            $newheight=375;
            $tmp=imagecreatetruecolor($newwidth,$newheight);



            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);


            $filename = "images/". generateRandomString(5) . $image['name'];

            imagejpeg($tmp,$filename,100);

            imagedestroy($src);
            imagedestroy($tmp);
        }
    }

我想進一步,現在我只是調整圖像大小,無論比例,思考是,我想調整到一個固定的和高度,而不會失去原有的比例,這當然是通過裁剪實現的+調整原始圖像的大小。

我不知道如何使用我的實際imagecreatetruecolorimagecopyresampled函數來做到這一點,並期待PHP手冊似乎不是很容易。

有一個非常好的庫我試圖集成到我的代碼,使用它像mysite.com/lib/timthumb.php?src=castle1.jpg&h=180&w=120一樣簡單,但我不知道如何將其與我的實際集成碼。

所以你有什么建議?

如果以下代碼中有任何拼寫錯誤或任何內容,請原諒我。 我沒有測試過。 我在這里做的是計算高度或寬度是否是太長的比例。 然后調整源尺寸以匹配最終圖像尺寸。 此外,調整我們縮小的邊的中心,使裁剪的圖像居中。

    $newwidth  = 500;
    $newheight = 375;
    $tmp       = imagecreatetruecolor($newwidth, $newheight);

    $widthProportion  = $width / $newwidth;
    $heightProportion = $height / $newheight;

    if ($widthProportion > $heightProportion) {
        // width proportion is greater than height proportion
        // figure out adjustment we need to make to width
        $widthAdjustment = ($width * ($widthProportion - $heightProportion));

        // Shrink width to proper proportion
        $width = $width - $widthAdjustment;

        $x = 0; // No adjusting height position
        $y = $y + ($widthAdjustment / 2); // Center the adjustment
    } else {
        // height proportion is greater than width proportion
        // figure out adjustment we need to make to width
        $heightAdjustment = ($height * ($heightProportion - $widthProportion));

        // Shrink height to proper proportion
        $height = $height - $heightAdjustment;

        $x = $x + ($heightAdjustment / 2); // Center the ajustment
        $y = 0; // No adjusting width position
    }

    imagecopyresampled($tmp, $src, 0, 0, $x, $y, $newwidth, $newheight, $width, $height);

因此,基本上使用$ width和$ height變量,您可以指定所需的圖片數量(裁剪)。 和$ x,$ y我們說的是我們想要裁剪圖片的地方。 其余的只是標准尺寸調整以適應全新的圖像。

我希望有所幫助!

暫無
暫無

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

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