2011-09-24 73 views
2

我有一个基于会员的网站,我建立了一个系统,注册用户可以提交评论。使用PHP将这些数据提交到MySQL数据库,然后查询到审查页面,如review.php?id = 246。但是,我希望用户能够上传图像,将图像存储在服务器上的文件夹中,将文件名存储在MySQL数据库中,然后将其全部查询到每个人的review.php页面上。我将如何做到这一点?Image for individual post/article using MySQL and PHP

在此先感谢你们!

+1

这个任务的特定部分会让你麻烦吗?文件上传在PHP手册中有出色的描述。你不知道如何将数据存储在MySQL或什么? –

+0

你究竟想知道什么?正如你所说的,逻辑就是fi9,所以你在这方面有什么困难? – Astha

+0

阅读我的解决方案 –

回答

1

比方说,你有表id, review_id, img列。另外id列必须是自动增量int。创建以下文件夹:photos其中包含名为original的文件夹和resized

下面的代码将调整图像大小,保存原始图像和调整图像大小,并将其地址设置为数据库表格。我希望它能为你效劳

<?php 
/* DB connection*/ 
require 'db.php'; 
/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 


/*registration sheck*/ 
$submit=$db->real_escape_string($_POST['submit']); 
$review=$db->real_escape_string($_POST['review']); 

function createThumb($upfile, $dstfile, $max_width, $max_height){ 
    $size = getimagesize($upfile); 
    $width = $size[0]; 
    $height = $size[1]; 
    $x_ratio = $max_width/$width; 
    $y_ratio = $max_height/$height; 
    if(($width <= $max_width) && ($height <= $max_height)) { 
      $tn_width = $width; 
      $tn_height = $height; 
    } elseif (($x_ratio * $height) < $max_height) { 
      $tn_height = ceil($x_ratio * $height); 
      $tn_width = $max_width; 
    } else { 
      $tn_width = ceil($y_ratio * $width); 
      $tn_height = $max_height; 
    } 
    if($size['mime'] == "image/jpeg"){ 
      $src = ImageCreateFromJpeg($upfile); 
      $dst = ImageCreateTrueColor($tn_width, $tn_height); 
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 
      imageinterlace($dst, true); 
      ImageJpeg($dst, $dstfile, 100); 
    } else if ($size['mime'] == "image/png"){ 
      $src = ImageCreateFrompng($upfile); 
      $dst = ImageCreateTrueColor($tn_width, $tn_height); 
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 
      Imagepng($dst, $dstfile); 

    } else { 

      $src = ImageCreateFromGif($upfile); 
      $dst = ImageCreateTrueColor($tn_width, $tn_height); 
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 
      imagegif($dst, $dstfile); 
    } 
} 



if($submit){ 

$result = $db->query("INSERT INTO reviews (`review_id`) VALUE '$review'") or die($db->error)); 

$review_id = $db->insert_id; 
if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') { 
    $ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1); 

    $imgNormal = 'img'.$new_user_id.$ext; 
    $normalDestination = "photos/original/" . $imgNormal; 
    $httprootmedium = "photos/resized/" . $imgNormal; 
    move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination); 
    createThumb($normalDestination,$httprootmedium,200,300); #For 500x300 Image 
} 

$result = $db->query("UPDATE review SET img='$imgNormal' WHERE id='$review_id'") or 
?> 
+1

你的createThumb函数相当臃肿。怎么样减少一点? –

+1

另外,为什么不根据getimagesize信息设置扩展名? –

+0

随时修改它 –