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
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]')";
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 na开发者_开发问答med 1001a and two other 1001 and 1001b?
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>
You need to store $imgNormal value in database only.
for more reference click on below link:
http://pastebin.com/Ed2YHV6w
The better way to do it is by using constants.
Define your constants for the various image sizes.
Like:
constants.php:
<?php
define("IMG_50x50", "thumb-50x50");
define("IMG_150x90", "thumb-150x90");
define("IMG_500x400", "thumb-500x500");
?>
In your .php file:
<?
include "constants.php";
...
... code to retrieve the results.
...
# for 50x50 image
$imgName = IMG_50x50 . "-" . $row["image-file-name"];
echo "<img src='/common/img/path/". $imgName ."' />";
...
...
?>
So, always upload one file and just store the file-name in the DB record. Use image-resize code from @Chauhan's answer to generate different version of images (you need not have to store them in the DB) - use the constants.php to name the different size file-name.
精彩评论