【问题标题】:php resize image after upload and crop it to the centerphp在上传后调整图像大小并将其裁剪到中心
【发布时间】:2011-11-11 05:36:48
【问题描述】:

我在 php 中有一个用户配置文件,我想让用户选择更改他们的配置文件图片。但是当他们通过 $_POST 提交新图片时,我希望将图片调整为:

高度:110px |宽度:与高度有关(如果宽度大于高度)

宽度:110px |高度:与宽度有关(如果高度大于宽度)

调整大小完成后,我想裁剪图片,使其变为 110px x 110px,但我希望它居中。

例如,如果用户上传的图片宽度为 110px,高度为 200px(调整大小后的尺寸),则裁剪后的新图像将从右侧裁剪 110x110 90px。我想要的是从左侧裁剪 45px,从右侧裁剪另外 45px,以便居中

该函数将接受 .png.gif.jpg 图像,并且无论初始格式是什么,都只会以 jpg 格式保存新图像。

我搜索了很多来创建这样一个函数,我找到了答案,但任何时候我试图改变一些小细节,一切都停止正常工作。

到目前为止我的代码:

<?php

$userfile_name = $_FILES["sgnIMG"]["name"];
$userfile_tmp = $_FILES["sgnIMG"]["tmp_name"];
$userfile_size = $_FILES["sgnIMG"]["size"];
$filename = basename($_FILES["sgnIMG"]["name"]);
$file_ext = substr($filename, strrpos($filename, ".") + 1);
$large_image_location = $target_path . $filename;
$ext = '';

if ($file_ext == 'jpg') {
    $ext = 1;
} else if ($file_ext == 'gif') {
    $ext = 2;
} else if ($file_ext == 'png') {
    $ext = 3;
} else {
    $ext = 0;
}

$target = $target_path . basename($_FILES["sgnIMG"]["name"]);

if (move_uploaded_file($userfile_tmp, $target)) {
    $newImg = resize110($target, $ext);
    if (isset($_POST['imupd']) && ($_POST['imupd'] == 'up')) {
        $sql = "UPDATE users SET avatar='" . str_replace('im/users/', '', $newImg) . "' WHERE id=" . $_SESSION['sesID'] . "";
        $result = mysql_query($sql);
        if ($result) {
            echo '<img src="' . $newImg . '" width="110" title="' . $file_ext . '"/>';
        } else {
            echo '<img src="im/avatars/px.png" width="110" title="' . $file_ext . '"/>';
        }
    }
} else {
    
}

function getHeight($image)
{
    $sizes = getimagesize($image);
    $height = $sizes[1];
    return $height;
}

function getWidth($image)
{
    $sizes = getimagesize($image);
    $width = $sizes[0];
    return $width;
}

function resize110($image, $ext)
{
    chmod($image, 0777);
    $oldHeight = getHeight($image);
    $oldWidth = getWidth($image);
    if ($oldHeight < $oldWidth) {
        $newImageHeight = 110;
        $newImageWidth = ceil((110 * $oldWidth) / $oldHeight);
        imagecopyresampled($newImage, $source, -ceil(($newImageWidth - 110) / 2), 0, 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight);
    } else {
        $newImageHeight = ceil((110 * $oldHeight) / $oldWidth);
        $newImageWidth = 110;
        imagecopyresampled($newImage, $source, 0, -ceil(($newImageHeight - 110) / 2), 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight);
    }
    $newImage = imagecreatetruecolor(110, 110);
    chmod($image, 0777);
    return $image;
    switch ($ext) {
        case 1;
            $source = imagecreatefromjpeg($image);
            break;
        case 2;
            $source = imagecreatefromgif($image);
            break;
        case 3;
            $source = imagecreatefrompng($image);
            break;
    }

    imagejpeg($newImage, $image, 90);
    return $image;
}

【问题讨论】:

    标签: php image-processing file-upload resize crop


    【解决方案1】:

    我环顾四周,并结合了我发现的不同部分的代码。因此,此脚本将获取 png 图像的 jpg、gif 图像,如果宽度大于 110px 高度,则将其调整为 110px 宽度(如果高度更大)。纵横比将保持不变,因此剩余的像素将除以 2 将用于居中图像。

    对于不同的尺寸,只需在任何地方更改 110。

    ================================================ ====================================

    <?php
    
    // pfpic  ->  the name of the <input type="file" name="pfpic"/> where user chooses file
    
    $target_path = "im/users/";                                // the directory to store the uploaded and then resampled image
    $userfile_name = $_FILES["pfpic"]["name"];                  // the name that the image file will have once uploaded
    $userfile_tmp = $_FILES["pfpic"]["tmp_name"];                   // the temporary name the server uses to store the file
    $userfile_size = $_FILES["pfpic"]["size"];                  // the size of the file that we want to upload
    $filename = basename($_FILES["pfpic"]["name"]);             // the full name of the file
    $file_ext = substr($filename, strrpos($filename, ".") + 1);  // the file extension
    $large_image_location = $target_path.$filename;                  // the full path to the file
    $ext='';
    
    
    if($file_ext=='jpg')
    {
        $ext=1;
    }
    else if ($file_ext=='gif')
    {
        $ext=2;
    }
    else if ($file_ext=='png')
    {
        $ext=3;
    }
    else
    {
        $ext=0;
    }
    
        $target = $target_path.basename(sha1($_SESSION['sesID']).'.'.'jpg');
        if($ext!=0)
        {
            if(move_uploaded_file($userfile_tmp,$target))
            {
                $newImg=resize110($target,$ext);
                echo '<img src="'.$newImg.'"/>';
            }
            else
            {
                echo 'the file could not be uploaded, please try again';
            }
        }
        else
        {
            echo 'this file extension is not accepted, please use "jpg", "gif" or "png" file formats';
        }
    
        function getHeight($image) 
        {
            $sizes = getimagesize($image);
            $height = $sizes[1];
            return $height;
        }
    
        function getWidth($image) 
        {
            $sizes = getimagesize($image);
            $width = $sizes[0];
            return $width;
        }
    
    
        function resize110($image,$ext) 
        {
            chmod($image, 0777);
            $oldHeight=getHeight($image);
            $oldWidth=getWidth($image);
            switch ($ext)
            {
                case 1;
                    $source = imagecreatefromjpeg($image);
                break;
    
                case 2;
                    $source = imagecreatefromgif($image);
                break;
    
                case 3;
                    $source = imagecreatefrompng($image);
                break;
            }
            $newImage = imagecreatetruecolor(110,110);
            $bgcolor = imagecolorallocate($newImage, 255, 255, 255);
            imagefill($newImage, 0, 0, $bgcolor);       // use this if you want to have a white background instead of black
    
    
            // we check tha width and height and then we crop the image to the center
            if($oldHeight<$oldWidth)
            {
                $newImageHeight = 110;
                $newImageWidth = ceil((110*$oldWidth)/$oldHeight);
                imagecopyresampled($newImage,$source,-ceil(($newImageWidth-110)/2),0,0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight);
            }
            else
            {
                $newImageHeight = ceil((110*$oldHeight)/$oldWidth);
                $newImageWidth = 110; 
                imagecopyresampled($newImage,$source,0,-ceil(($newImageHeight-110)/2),0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight);
            }
    
            //we save the image as jpg resized to 110x110 px and cropped to the center. the old image will be replaced
            imagejpeg($newImage,$image,90);
    
            return $image;
    
        }
    

    ?>

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 2015-02-22
      • 2012-08-16
      • 1970-01-01
      • 2013-03-01
      • 2011-05-01
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多