图像上传集成到注册页面

时间:2011-06-19 15:30:50

标签: php mysql image upload

请给我建议:我想将图片上传整合到我的注册过程中。你知道一些插件或脚本用于此目的(上传,调整大小,链接到db_table)?

4 个答案:

答案 0 :(得分:2)

使用GD库查找以下用于上传和裁剪图像的PHP代码。

<?php
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);
   }
}

//usage

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 = time().$ext;
    $normalDestination = "Photos/Orignal/" . $imgNormal;
    $httpRootLarge = "Photos/Large/" . $imgNormal;
    $httpRootSmall = "Photos/Small/" . $imgNormal;
    $httpRootThumb = "Photos/Thumb/" . $imgNormal;
    move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination);
    createThumb($normalDestination,$httpRootLarge,680,604); #For 604x604 Image 
    createThumb($normalDestination,$httpRootSmall,500,300); #For 500x300 Image
    createThumb($normalDestination,$httpRootThumb,130,100); #For 130x100 Image
}
?>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="upload_Image" id="upload_Image" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

在你的mysql表中保存$ imgNormal值。

答案 1 :(得分:1)

您可以在mysql中上传图片并使用imagemagic或gd库调整大小。

答案 2 :(得分:0)

您可以使用move_uploaded_file to upload,但请注意您的表单元素还必须具有属性enctype="multipart/form-data"

对于图像缩放,您可以使用GD库中的函数。快速搜索“php scale image”会发现有关该主题的许多有用链接。

更不用说manual itself有许多有用的示例脚本,包括你所追求的那样。

你似乎对PHP很陌生。我建议你去了解PHP manual。使用它作为您的第一个PHP资源,然后在互联网上的文章第二。在用完所有其他选项后,请到这里来。看来你甚至都没有尝试解决这个问题或者自己研究它。

答案 3 :(得分:0)

你可以查看一个文件上传者:http://github.com/valums/file-uploader和phpthumb来调整大小,虽然正如其他海报所提到的那样,如果你想了解更多关于它的话,自己做的并不复杂。