PHP在保存到mysql数据库之前调整图像大小?

发布于 2024-10-05 12:41:00 字数 3032 浏览 0 评论 0原文

收到帖子后,如何调整为较小的宽度(最大宽度为 80,最小宽度也是 80)以及为了安全目的我应该检查什么?

我当前的代码:

if(!empty($_FILES)) {
# Resize Image function
$return=true;
function resizeImage($originalImage,$toWidth,$toHeight){
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;

    // Recalculate new size with default ratio
    if ($yscale>$xscale){
        $new_width = round($width * (1/$yscale));
        $new_height = round($height * (1/$yscale));
    }
    else {
        $new_width = round($width * (1/$xscale));
        $new_height = round($height * (1/$xscale));
    }

    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    return $imageResized;
}

// Get the file information
$userfile_name = $_FILES['profile_picture']['name'];
$userfile_tmp  = $_FILES['profile_picture']['tmp_name'];
$userfile_size = $_FILES['profile_picture']['size'];
$userfile_type = $_FILES['profile_picture']['type'];
$filename = basename($_FILES['profile_picture']['name']);
$file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));

//Only process if the file is a JPG and below the allowed limit
if((!empty($_FILES["profile_picture"])) && ($_FILES['profile_picture']['error'] == 0)) {
    $allowed_image_types = array('image/pjpeg'=>"jpg",'image/jpeg'=>"jpg",'image/jpg'=>"jpg",'image/png'=>"png",'image/x-png'=>"png",'image/gif'=>"gif");
    $allowed_image_ext = array_unique($allowed_image_types); // Do not change this
    foreach ($allowed_image_types as $mime_type => $ext) {
        if($file_ext==$ext ){
            $return=false;
            break;
        } else {
            $return=true;
        }
    }

    if ($userfile_size > (7*1048576)) { # 7 means 7 mb
        $return=false;
    }
} else {
    $return=false;
}

//Everything is ok, so we can upload the image.
if (strlen($return)==0){
    $widthAndHeight = getimagesize($userfile_tmp . "." . $file_ext); //EDITED QUESTION
    $width  = $widthAndHeight[0];
    $height = $widthAndHeight[1];

    if ($width > 80){
        $scale = 80/$width;
        $height = $height * $scale;
        $data = resizeImage($userfile_name,80,$height,$scale);
        $data = mysql_real_escape_string($data);
    } else {
        $data = mysql_real_escape_string($userfile_name);
    }

    $update = mysql_query("UPDATE `avatar` set image = '{$data}' WHERE userid = '" .  $_SESSION['userid'] . " . '");
} else {
    $return=false;
}

mysql数据库中的数据类型是MediumBlob,因为它只存储小文件

抱歉没有提到我的问题,代码不起作用。错误是:

Warning: getimagesize(C:\wamp\tmp\php991B.tmp.png) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\XXX\avatar.php on line 82**strong text**

After I got the post, how to resize to a smaller width (maximum width is 80, min also 80) and what should I check for security purposes?

My current code:

if(!empty($_FILES)) {
# Resize Image function
$return=true;
function resizeImage($originalImage,$toWidth,$toHeight){
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;

    // Recalculate new size with default ratio
    if ($yscale>$xscale){
        $new_width = round($width * (1/$yscale));
        $new_height = round($height * (1/$yscale));
    }
    else {
        $new_width = round($width * (1/$xscale));
        $new_height = round($height * (1/$xscale));
    }

    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    return $imageResized;
}

// Get the file information
$userfile_name = $_FILES['profile_picture']['name'];
$userfile_tmp  = $_FILES['profile_picture']['tmp_name'];
$userfile_size = $_FILES['profile_picture']['size'];
$userfile_type = $_FILES['profile_picture']['type'];
$filename = basename($_FILES['profile_picture']['name']);
$file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));

//Only process if the file is a JPG and below the allowed limit
if((!empty($_FILES["profile_picture"])) && ($_FILES['profile_picture']['error'] == 0)) {
    $allowed_image_types = array('image/pjpeg'=>"jpg",'image/jpeg'=>"jpg",'image/jpg'=>"jpg",'image/png'=>"png",'image/x-png'=>"png",'image/gif'=>"gif");
    $allowed_image_ext = array_unique($allowed_image_types); // Do not change this
    foreach ($allowed_image_types as $mime_type => $ext) {
        if($file_ext==$ext ){
            $return=false;
            break;
        } else {
            $return=true;
        }
    }

    if ($userfile_size > (7*1048576)) { # 7 means 7 mb
        $return=false;
    }
} else {
    $return=false;
}

//Everything is ok, so we can upload the image.
if (strlen($return)==0){
    $widthAndHeight = getimagesize($userfile_tmp . "." . $file_ext); //EDITED QUESTION
    $width  = $widthAndHeight[0];
    $height = $widthAndHeight[1];

    if ($width > 80){
        $scale = 80/$width;
        $height = $height * $scale;
        $data = resizeImage($userfile_name,80,$height,$scale);
        $data = mysql_real_escape_string($data);
    } else {
        $data = mysql_real_escape_string($userfile_name);
    }

    $update = mysql_query("UPDATE `avatar` set image = '{$data}' WHERE userid = '" .  $_SESSION['userid'] . " . '");
} else {
    $return=false;
}

The datatype in mysql database is MediumBlob since it only store small file

Sorry for not mention my problem, the code its not working. The error is:

Warning: getimagesize(C:\wamp\tmp\php991B.tmp.png) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\XXX\avatar.php on line 82**strong text**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖竹 2024-10-12 12:41:00

更好的方法是将图像存储在文件系统中并将对文件的引用存储在数据库中。 (对于调整大小,您可以使用 GD 库)

尝试此链接: http://www .php.net/manual/en/function.imagecopyresized.php

Better way will be - to store the image in file system and store a ref to the file in the database. (For the resizing you can use GD library)

Try this link: http://www.php.net/manual/en/function.imagecopyresized.php

荒芜了季节 2024-10-12 12:41:00

如果您确实不需要为此编写自己的代码,您可以尝试一些已经存在的解决方案。

这是一个优秀的脚本
http://phpthumb.gxdlabs.com/

我不确定您是否可以使用将图像数据保存到数据库它,但最坏的情况是

您调整图像大小并将其保存到本地文件夹
使用file_get_contents读取图像并保存到db
删除服务器上的临时图像。

但我同意那些说你应该将图像存储在文件系统中而不是数据库中的人的观点。

If you don't really need to write your own code for this, you can try out some of the solutions that are already out there.

This is an excellent script
http://phpthumb.gxdlabs.com/

I am not sure if you can save image data to a database using it, but worst case scenario is

you resize the image and save it to a local folder
use file_get_contents to read the image and save to db
delete temp image on server.

But I agree with those who say you should store the images in the filesystem, not the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文