在 PHP 中调整图像大小 [英] Resize image in PHP

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

问题描述

我想编写一些 PHP 代码,它会自动将通过表单上传的任何图像的大小调整为 147x147 像素,但我不知道该怎么做(我是相对的 PHP 新手).

到目前为止,我已经成功上传了图像,识别了文件类型并清理了名称,但我想将调整大小功能添加到代码中.例如,我有一个 2.3MB 大小的测试图像,尺寸为 1331x1331,我希望代码可以缩小它的大小,我猜这也会显着压缩图像的文件大小.

到目前为止,我有以下几点:

if ($_FILES) {//将文件属性放入变量中$file_name = $_FILES['profile-image']['name'];$file_size = $_FILES['profile-image']['size'];$file_tmp_name = $_FILES['profile-image']['tmp_name'];//确定文件类型开关 ($_FILES['profile-image']['type']) {case 'image/jpeg': $ext = "jpg";休息;case 'image/png': $ext = "png";休息;默认值:$ext = '';休息;}如果($ext){//检查文件大小如果 ($file_size <500000) {//处理文件 - 清理文件名并移动到安全位置$n = "$file_name";$n = ereg_replace("[^A-Za-z0-9.]", "", $n);$n = strtolower($n);$n = "头像/$n";move_uploaded_file($file_tmp_name, $n);} 别的 {$bad_message = "请确保您选择的文件小于 5MB.";}} 别的 {$bad_message = "请确保您的图片文件类型为 .jpg 或 .png.";}}$query = "INSERT INTO users (image) VALUES ('$n')";mysql_query($query) 或 die("插入失败." . mysql_error() . "<br/>" . $query);

解决方案

您需要使用 PHP 的 ImageMagickGD 函数来处理图像.

以 GD 为例,它就像……一样简单.

function resize_image($file, $w, $h, $crop=FALSE) {列表($width, $height) = getimagesize($file);$r = $width/$height;如果($crop){如果 ($width > $height) {$width = ceil($width-($width*abs($r-$w/$h)));} 别的 {$height = ceil($height-($height*abs($r-$w/$h)));}$newwidth = $w;$newheight = $h;} 别的 {如果 ($w/$h > $r) {$newwidth = $h*$r;$newheight = $h;} 别的 {$newheight = $w/$r;$newwidth = $w;}}$src = imagecreatefromjpeg($file);$dst = imagecreatetruecolor($newwidth, $newheight);imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);返回 $dst;}

你可以调用这个函数,就像这样......

$img = resize_image(‘/path/to/some/image.jpg’, 200, 200);

根据个人经验,GD 的图像重采样也确实显着减小了文件大小,尤其是在重采样原始数码相机图像时.

I'm wanting to write some PHP code which automatically resizes any image uploaded via a form to 147x147px, but I have no idea how to go about it (I'm a relative PHP novice).

So far, I've got images uploading successfully, filetypes being recognised and names cleaned up, but I'd like to add the resize functionality into the code. For example, I've got a test image which is 2.3MB, and 1331x1331 in dimension, and I'd like the code to size it down, which I'm guessing will dramatically compress the filesize of the image, too.

So far, I've got the following:

if ($_FILES) {
                //Put file properties into variables
                $file_name = $_FILES['profile-image']['name'];
                $file_size = $_FILES['profile-image']['size'];
                $file_tmp_name = $_FILES['profile-image']['tmp_name'];

                //Determine filetype
                switch ($_FILES['profile-image']['type']) {
                    case 'image/jpeg': $ext = "jpg"; break;
                    case 'image/png': $ext = "png"; break;
                    default: $ext = ''; break;
                }

                if ($ext) {
                    //Check filesize
                    if ($file_size < 500000) {
                        //Process file - clean up filename and move to safe location
                        $n = "$file_name";
                        $n = ereg_replace("[^A-Za-z0-9.]", "", $n);
                        $n = strtolower($n);
                        $n = "avatars/$n";
                        move_uploaded_file($file_tmp_name, $n);
                    } else {
                        $bad_message = "Please ensure your chosen file is less than 5MB.";
                    }
                } else {
                    $bad_message = "Please ensure your image is of filetype .jpg or.png.";
                }
            }
$query = "INSERT INTO users (image) VALUES ('$n')";
mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);

解决方案

You need to use either PHP's ImageMagick or GD functions to work with images.

With GD, for example, it's as simple as...

function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
}

And you could call this function, like so...

$img = resize_image(‘/path/to/some/image.jpg’, 200, 200);

From personal experience, GD's image resampling does dramatically reduce file size too, especially when resampling raw digital camera images.

这篇关于在 PHP 中调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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