如何以正常的插入形式(MySql)上传图像?上传后,图像应该有三个不同大小和不同名称的版本 [英] How can I upload images in a normal insert form (MySql)? after upload the image should have three versions of different sizes and different names

查看:86
本文介绍了如何以正常的插入形式(MySql)上传图像?上传后,图像应该有三个不同大小和不同名称的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个插入表单:

Now I have an insert form just like that:

$sql="INSERT INTO products (pname, pcat, pimg1, pimg2, pimg3, pnotes, pclient, pclientaddress, pclientphone)
VALUES
('$_POST[pname]','$_POST[pcat]','$_POST[pimg1]','$_POST[pimg2]','$_POST[pimg3]','$_POST[pnotes]','$_POST[pclient]','$_POST[pclientaddress]','$_POST[pclientphone]')";

有没有办法上传一张图片,而不是输入三张不同图片的网址值它有不同的大小,原来的一个名为1001a,另外两个1001和1001b?

Instead of entering the URL value of three different images, is there a way I can upload one image and have there different sizes of it, the original one to be named 1001a and two other 1001 and 1001b?

推荐答案

查找下面的PHP代码使用GD库上传和裁剪图像。您只能在数据库中保存一个图像名称,而其他损坏的图像将使用相同的名称进行访问,但它应存储在不同的目录中,如下所示:

Find below php code to upload and crop image using GD library. You can save only one image name in database and other croped images will be access using the same name, but it should be stored in different-different directory as below:

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

您只需要在数据库中存储$ imgNormal值。

You need to store $imgNormal value in database only.

如需更多参考,请点击以下链接:

for more reference click on below link:

http ://pastebin.com/Ed2YHV6w

这篇关于如何以正常的插入形式(MySql)上传图像?上传后,图像应该有三个不同大小和不同名称的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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