在不使用第三方库的情况下在 PHP 中调整图像大小? [英] Resize images in PHP without using third-party libraries?

查看:16
本文介绍了在不使用第三方库的情况下在 PHP 中调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个应用程序中,我使用下面的代码片段将上传的图像复制到一个目录中.它工作正常,但复制大图像(> 2MB)比理想情况需要更多的时间,我真的不需要这么大的图像,所以,我正在寻找一种调整图像大小的方法.如何使用 PHP 实现这一点?

解决方案

最后,我找到了一种适合我需求的方法.下面的代码片段会将图像调整为指定的宽度,并自动计算高度以保持比例.

$image = $_FILES["image"]["tmp_name"];$resizedDestination = $uploadDirectory.md5($randomNumber.$filename)."_RESIZED.jpg";复制($_FILES,$resizedDestination);$imageSize = getImageSize($image);$imageWidth = $imageSize[0];$imageHeight = $imageSize[1];$DESIRED_WIDTH = 100;$proportionalHeight = round(($DESIRED_WIDTH * $imageHeight)/$imageWidth);$originalImage = imageCreateFromJPEG($image);$resizedImage = imageCreateTrueColor($DESIRED_WIDTH, $proportionalHeight);imageCopyResampled($images_fin, $originalImage, 0, 0, 0, 0, $DESIRED_WIDTH+1, $proportionalHeight+1, $imageWidth, $imageHeight);imageJPEG($resizedImage, $resizedDestination);imageDestroy($originalImage);imageDestroy($resizedImage);

对于寻求完整示例的其他人,请创建两个文件:

<头><title>简单文件上传</title><身体><中心><div style="margin-top:50px; padding:20px; border:1px solid #CECECE;">选择图像.<br/><br/><form action="receive.php" enctype="multipart/form-data" method="post"><input type="file" name="image" size="40"><输入类型=提交"值=发送"></表单>

</中心>

In one of my applications, I'm using the code snippet below to copy uploaded images to a directory. It works fine but copying large images (> 2MB) takes more time than ideal and I really don't need images this big, so, I'm looking for a way to resize the images. How to achieve this using PHP?

<?php

$uploadDirectory = 'images/0001/';
$randomNumber = rand(0, 99999); 
$filename = basename($_FILES['userfile']['name']);
$filePath = $uploadDirectory.md5($randomNumber.$filename);

// Check if the file was sent through HTTP POST.

if (is_uploaded_file($_FILES['userfile']['tmp_name']) == true) {

    // Validate the file size, accept files under 5 MB (~5e+6 bytes).

    if ($_FILES['userfile']['size'] <= 5000000) {

        // Move the file to the path specified.

        if (move_uploaded_file($_FILES['userfile']['tmp_name'], $filePath) == true) {

            // ...

        }

    }

}

?>

解决方案

Finally, I've discovered a way that fit my needs. The following snippet will resize an image to the specified width, automatically calculating the height in order to keep the proportion.

$image = $_FILES["image"]["tmp_name"];
$resizedDestination = $uploadDirectory.md5($randomNumber.$filename)."_RESIZED.jpg";

copy($_FILES, $resizedDestination);

$imageSize = getImageSize($image);
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];

$DESIRED_WIDTH = 100;
$proportionalHeight = round(($DESIRED_WIDTH * $imageHeight) / $imageWidth);

$originalImage = imageCreateFromJPEG($image);

$resizedImage = imageCreateTrueColor($DESIRED_WIDTH, $proportionalHeight);

imageCopyResampled($images_fin, $originalImage, 0, 0, 0, 0, $DESIRED_WIDTH+1, $proportionalHeight+1, $imageWidth, $imageHeight);
imageJPEG($resizedImage, $resizedDestination);

imageDestroy($originalImage);
imageDestroy($resizedImage);

To anyone else seeking a complete example, create two files:

<!-- send.html -->

<html>

<head>

    <title>Simple File Upload</title>

</head>

<body>

    <center>

        <div style="margin-top:50px; padding:20px; border:1px solid #CECECE;">

            Select an image.

            <br/>
            <br/>

            <form action="receive.php" enctype="multipart/form-data" method="post">
                <input type="file" name="image" size="40">
                <input type="submit" value="Send">
            </form>

        </div>

    </center>

</body>

<?php

// receive.php

$randomNumber = rand(0, 99999);
$uploadDirectory = "images/";
$filename = basename($_FILES['file_contents']['name']);
$destination = $uploadDirectory.md5($randomNumber.$filename).".jpg";

echo "File path:".$filePath."<br/>";

if (is_uploaded_file($_FILES["image"]["tmp_name"]) == true) {

    echo "File successfully received through HTTP POST.<br/>";

    // Validate the file size, accept files under 5 MB (~5e+6 bytes).

    if ($_FILES['image']['size'] <= 5000000) {

        echo "File size: ".$_FILES["image"]["size"]." bytes.<br/>";

        // Resize and save the image.

        $image = $_FILES["image"]["tmp_name"];
        $resizedDestination = $uploadDirectory.md5($randomNumber.$filename)."_RESIZED.jpg";

        copy($_FILES, $resizedDestination);

        $imageSize = getImageSize($image);
        $imageWidth = $imageSize[0];
        $imageHeight = $imageSize[1];

        $DESIRED_WIDTH = 100;
        $proportionalHeight = round(($DESIRED_WIDTH * $imageHeight) / $imageWidth);

        $originalImage = imageCreateFromJPEG($image);

        $resizedImage = imageCreateTrueColor($DESIRED_WIDTH, $proportionalHeight);

        imageCopyResampled($images_fin, $originalImage, 0, 0, 0, 0, $DESIRED_WIDTH+1, $proportionalHeight+1, $imageWidth, $imageHeight);
        imageJPEG($resizedImage, $resizedDestination);

        imageDestroy($originalImage);
        imageDestroy($resizedImage);

        // Save the original image.

        if (move_uploaded_file($_FILES['image']['tmp_name'], $destination) == true) {

            echo "Copied the original file to the specified destination.<br/>";

        }

    }

}

?>

这篇关于在不使用第三方库的情况下在 PHP 中调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆