PHP调整图像之前保存到mysql数据库? [英] PHP resize image before save to mysql database?

查看:106
本文介绍了PHP调整图像之前保存到mysql数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我当前的代码:

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,因为它只存储小文件

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**


推荐答案

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

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)

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

这篇关于PHP调整图像之前保存到mysql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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