使用Php调整图像大小和裁剪

使用Php调整图像大小和裁剪,php,image,image-resizing,resize-image,Php,Image,Image Resizing,Resize Image,我使用php脚本上传图像并调整其大小,非常简单: if($_SERVER["REQUEST_METHOD"] == "POST") { $image = $_FILES["image_upload"]; $uploadedfile = $image['tmp_name']; if ($image) { $filename = stripslashes($_FILES['image_upload']['name']); $extension

我使用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);
        }
    }
我想更进一步,现在我只是调整图像的大小,不管比例如何,我想,我想调整它到一个固定的大小和高度,而不丢失原始比例,这当然是通过裁剪+调整原始图像的大小来实现的

我不知道如何使用实际的imagecreatetruecolor和imagecopyresampled函数来实现这一点,而查看php手册似乎并不容易

有一个非常简单的例子,我正试图将它集成到我的代码中,使用它就像mysite.com/lib/timtumb.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,我们说的是我们想在哪里裁剪图片。剩下的只是标准的调整大小,以适应全新的图像


我希望这有帮助

您可以尝试phpthumb库,这是一个成熟的项目,请检查此处的演示:请记住,服务器中的图像操作确实会产生问题,例如占用cpu,可能会造成严重瓶颈。有一个很好的教程,可以使用imagecreatetruecolor url调整大小:将帮助您调整大小,而不会丢失比例。