Please give me advise: I want to integrate image upload to my registration process. Do you know some plugin or script for this purpose (upload, resize, put link to db_table)?
Find below php code to upload and crop image using GD library.
<?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>
save $imgNormal value in your mysql table.
You can upload images in mysql and resize it using imagemagic or gd libraries.
You can use move_uploaded_file to upload, but note that your form element will also have to have the attribute enctype="multipart/form-data"
For image scaling, you can use functions from the GD library. A quick search on "php scale image" reveals many useful links on the subject.
Not to mention the manual itself has many useful example scripts, including such a one you're after.
It seems that you're quite new to PHP. I suggest you get to know the PHP manual. Use it as your first PHP resource, then articles on the internet second. After you've exhausted all other options, come here. It appears you didn't even try to solve this or research it yourself.
You could look at a file uploader like: http://github.com/valums/file-uploader and phpthumb for the resize, although as the other posters mention, it's not that complicated to do yourself if you want to learn more about it.
精彩评论