2010-11-30 54 views
1

我的帖子后,如何调整到一个较小的宽度(最大宽度为80,最小也80),我应该检查安全的目的呢?PHP保存到mysql数据库之前调整图像大小?

我当前的代码:

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

您的问题有没有关系数据库。它说“找不到文件”。检查文件路径。存储在数据库中 – 2010-11-30 10:05:50

+0

长的文件,你必须开始独立的PHP程序,从而在页面上的每个图像再拍连接到数据库。这对网络开发来说是不可接受的开销。所有静态数据(如图像)应按原样提供,而不是通过动态页面。 – 2010-11-30 10:07:55

回答

0

如果你并不真的需要为此编写自己的代码,你可以尝试一些已经出来的解决方案那里。

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

我不知道,如果你可以使用它的图像数据保存到数据库中,但最坏的情况是

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

但我同意那些谁说,你应该将图像存储在文件系统,而不是数据库。

相关问题