【问题标题】:PHP resize image before save to mysql database?PHP在保存到mysql数据库之前调整图像大小?
【发布时间】:2011-05-17 18:24:08
【问题描述】:

收到帖子后,如何调整到更小的宽度(最大宽度为 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**

【问题讨论】:

  • 您的问题与数据库无关。它说“找不到文件”。检查文件路径。
  • 将文件存储在数据库中,您必须启动单独的 PHP 进程来为页面上的每个图像提供服务,并让另一个连接到数据库。对于 Web 开发来说,这是不可接受的开销。所有静态数据(如图片)都应按原样提供,而不是通过动态页面提供。

标签: php database image-manipulation


【解决方案1】:

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

试试这个链接:http://www.php.net/manual/en/function.imagecopyresized.php

【讨论】:

  • 我对在文件系统和数据库上存储图像进行了研究,各有优缺点。虽然大多数人更喜欢文件系统。
  • 但是一张 80x80 RGB 图像只有 18.75KB
  • 同意 Don,这就是我将它存储在 Blob 中的原因
  • 我更喜欢 ImageMagick 调整大小,因为使用 GD 更难处理 Lanczos 重采样。
【解决方案2】:

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

这是一个很棒的脚本 http://phpthumb.gxdlabs.com/

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

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

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

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 2014-03-07
    • 2012-12-14
    • 2011-09-01
    • 1970-01-01
    • 2013-07-26
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多