PHP image upload and generate thumbnail effect This tutorial is a PHP image upload and then generates a small image from the uploaded image. It is a very good file upload class. If you are looking for a similar program, you can come and have a look
PHP tutorial image upload and generate thumbnails Effect
This tutorial is about uploading php images and then generating small images from the uploaded images. It is a very good file uploading class. If you are looking for a similar program, you can check it out
*/
function uploadimage($upname,$smallmark=1,$dstsw,$dstsh=0,$path_dim,$path_xim,$newname,$smallname=0,$filetype="null") {
global $webaddr,$_files,$my;
$phpv=str_replace('.', '', php_version);
$filename=$upname;
$max_file_size = 2147483648; //Upload file size limit, unit byte 2m
$path_im = $path_dim; //Generate large image saving folder path
$path_sim = $path_xim; //Thumbnail saving folder path
$simclearly=75;
$simclearlypng =$phpv>=512?7:75; //Thumbnail resolution 0-100, the larger the number, the clearer the file size is
$smallmark = $smallmark; //Whether to generate thumbnails (1 means additional generation, others not);
$dst_sw =$dstsw; //Define the thumbnail width. I use equal proportion scaling for the height, so just compare the width.
$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);if (!is_uploaded_file($_files[$filename][tmp_name])) {
dsetcookie('setok','upload1');
header("location:bKjia.c0m/profile");
exit;
}
$file = $_files[$filename];
$pinfo = pathinfo($file["name"]);
If ($filetype=="null") {
$filetype = $pinfo['extension'];
}
If (!in_array(strtolower($pinfo['extension']),array("jpg","jpeg","png","gif"))) {
dsetcookie('setok','upload3');
header("location:bKjia.c0m/profile");
exit;
}if($max_file_size < $file["size"]) {//Check the file size
dsetcookie('setok','upload2');
header("location:bKjia.c0m/profile");
exit;
}
If(!in_array($file["type"],$uptypes)) { //Check file type
dsetcookie('setok','upload3');
header("location:bKjia.c0m/profile");
exit;
}
If(!file_exists($path_im)) {
mkdir($path_im);
}$filename = $file["tmp_name"];
$im_size = getimagesize($filename);$src_w = $im_size[0];
$src_h = $im_size[1];
$src_type = $im_size[2];$all_path = $path_im.$newname.".".$filetype;//Path + file name, currently named after the upload time
If (file_exists($all_path)) {
@unlink($all_path);
}
If(!move_uploaded_file ($filename,$all_path)) {
dsetcookie('setok','upload4');
header("location:bKjia.c0m/profile");
exit;
}
$pinfo = pathinfo($all_path);
$fname = $pinfo[basename];switch($src_type) {//Determine the source image file type
case 1://gif
$src_im = @imagecreatefromgif($all_path);//Get the image from the source image file
break;
case 2://jpg
$src_im = @imagecreatefromjpeg($all_path);
break;
case 3://png
$src_im = @imagecreatefrompng($all_path);
break;
//case 6:
//$src_im=imagecreatefromwbmp($all_path);
//break;
default:
dsetcookie('setok','upload3');
header("location:bKjia.c0m/profile");
exit;
}if($smallmark == 1) {
If(!file_exists($path_sim)) {//Check whether the thumbnail directory exists, if it does not exist, create it
mkdir($path_sim);
}
If ($smallname) $newname=$smallname;
$sall_path = $path_sim.$newname.".".$filetype;
If (file_exists($sall_path)) {
@unlink($sall_path);
}
If($src_w <= $dst_sw) { // Original image size <= Thumbnail size
If ($dstsh==0) {
$dst_sim = @imagecreatetruecolor($src_w,$src_h); //New thumbnail true color bitmap
$sx=$sy=0;
} else {
$dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //New thumbnail true color bitmap
$sx=($dstsw-$src_w)/2;
$sy=($dstsh-$src_h)/2;
}
$img = @imagecreatefrompng("images/phbg.png");
@imagecopymerge($dst_sim,$img,0,0,0,0,$dstsw,$dstsh,100); //The original image is written into the new true color bitmap
@imagecopymerge($dst_sim,$src_im,$sx,$sy,0,0,$src_w,$src_h,100); //The original image is written into the new true color bitmap
}If($src_w > $dst_sw) { // Original image size > Thumbnail size
$dst_sh = $dst_sw/$src_w*$src_h;
If ($dst_sh<$dstsh) {
$dst_sh=$dstsh;
$dst_sw=$dst_sh/$src_h*$src_w;
}
If ($dstsh==0) {
$dst_sim = @imagecreatetruecolor($dst_sw,$dst_sh); //Create a new thumbnail true color bitmap (reducing the size of the original image proportionally)
} else {
$dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //Create a new thumbnail true color bitmap (reducing the size of the original image proportionally)
}
@imagecopyresampled($dst_sim,$src_im,0,0,0,0,$dst_sw,$dst_sh,$src_w,$src_h); //The original image is written into the new true color bitmap
}switch($src_type) {
case 1:@imagegif($dst_sim,$sall_path,$simclearly);//Generate gif file, picture resolution 0-100
break;
case 2:@imagejpeg($dst_sim,$sall_path,$simclearly);//Generate jpg file, picture resolution 0-100
break;
case 3:@imagepng($dst_sim,$sall_path,$simclearlypng);//Generate png file, image resolution 0-100
break;
//case 6:
//imagewbmp($dst_sim,$sall_path);
break;
}
//Release cache
@imagedestroy($dst_sim);
}
@imagedestroy($src_im);
Return $newname.".".$filetype;
}
?>