jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码

本例用到其他2个php class.upload.php和 functions.php还有css和js以及img文件

完整实例代码点击此处本站下载。

效果图如下:

实现代码如下:

JavaScript代码如下:

代码如下:

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#filelist").niceScroll({ 
        cursorwidth: "8px", 
        cursorborderradius: "0px", 
        cursoropacitymin: 0.1, 
        cursoropacitymax: 0.3 
    }); 
 
    $(".side-pane").niceScroll({ 
        cursorwidth: "8px", 
        cursorborderradius: "0px", 
        cursoropacitymin: 0.1, 
        cursoropacitymax: 0.3 
    }); 
 
    $(".time").timeago(); 
}); 
</script>

javascript代码如下:

代码如下:

<script type="text/javascript"> 
        // <![CDATA[
 
            $('#upload_button').click(function() {
                $('.side-pane').html('');
                $('#upload_button').hide();
                $('#pickfiles').hide();
                $('#upload_info').show();
                $('#upload_info').css("display","inherit");
                uploader.start();
 
                $('#filelist').block({
                    message: '<center><div class="start-message">Upload in Progress</div></center>',
                    css: {
                        border: 'none',
                        backgroundColor: 'none'
                    },
                    overlayCSS: {
                        backgroundColor: '#fff',
                        opacity: '0',
                        cursor: 'wait'
                    }
                });
            });
 
            var uploader = new plupload.Uploader({
                runtimes : 'flash, html5',
                browse_button : 'pickfiles',
                container : 'uploader',
                max_file_size : '10mb',
                url : 'upload.php',
                flash_swf_url : 'uploader/uploader.swf',
                filters : [
                    { title : "Image files", extensions : "jpg,jpeg,gif,png" }
                ]
            });
 
            uploader.bind('Init', function(up, params) {});
            uploader.init();
 
            uploader.bind('FilesAdded', function(up, files) {
                /*
                    @@ Show / hide various elements
                */
                $('.upload-message').hide();
                $('.info-message').hide();
                $('#upload_button').show();
                $('#filelist_header').show();
 
                $.each(files, function(i, file) {
                    $('#filelist').append(
                        '<div id="' + file.id + '" class="filecontainer">' +
                        '<div class="filename"> '+file.name + '</div>'+
                        '<div class="filesize">' + plupload.formatSize(file.size) + '</div>'+
                        '<div class="filestatus" id="status_'+file.id+'">0%</div>'+
                        '<div class="filedel"><a id="remove_'+file.id+'" href="javascript:;"><img src="img/uploader/delete.gif" /></a></div>' +
                        '</div>');
 
                    $('#remove_'+file.id).click(function(e) {
                        uploader.removeFile(file)
                        $('#'+file.id).hide('slow', function() { ('#'+file.id).remove(); });
                    });
                });
 
                up.refresh();
                $('#filelist').animate({scrollTop: $('#filelist').attr("scrollHeight")}, 1500);
            });
 
            uploader.bind('UploadProgress', function(up, file) {
                $('#status_' + file.id).html(file.percent + "%");
                    if(file.percent == 100) {
                        $('#' + file.id).block({
                            message: '',
                            css: {
                                border: 'none',
                                backgroundColor: 'none'
                            },
                            overlayCSS: {
                                backgroundColor: '#fff',
                                opacity: '0.7',
                                cursor: 'wait'
                            }
                        });
                    }
                $('#percent').width(uploader.total.percent+"%");
                $('#upRate').text(Math.ceil(uploader.total.bytesPerSec/1024)+" kb/sec");
            });
 
            uploader.bind('FileUploaded', function(up, file, response) {
                var input = $("#uploaded_photos");
                var data = response.response;
                var details = data.split(',');
                    if(details[0] == 'success') {
                        var photo_html = '<div class="padding-10 hm-photo clearfix"><a href="../upload/source/'+details[4]+'" target="_blank"><img src="../upload/small/'+details[4]+'"></a><p class="small-text light-text">'+details[1]+'</p><abbr class="time small-text" title="'+details[2]+'"></abbr></div>';
                        input.val(input.val() + details[1] + ":");
                    } else {
                        var photo_html = '<div class="clearfix">'+details[1]+'</div>';
                    }
                $('.side-pane').prepend(photo_html);
                $('.time').timeago();
            });
 
            uploader.bind('UploadComplete', function(up, files) {
                $('#upload_info').hide();
                $('#filelist').unblock({
                    onUnblock: function () {
                        $('#filelist_header').hide();
                        $('#filelist').html('<div style="margin-top: 180px; text-align: center;"><strong>ok</strong><br/>All photos have been uploaded.</div>');
                    }
                });
            });
 
        // ]]> 
        </script>

上面2个js都放在index.php里面

XML/HTML代码如下:

代码如下:

<div id="uploader" class="main-pane"> 
            <div id="filelist_header" class="clearfix"> 
                <div class="filename">Name</div> 
                <div class="filesize">Size</div> 
                <div class="filestatus">Status</div> 
                <div class="filedel"></div> 
                <div></div> 
            </div> 
 
            <div id="filelist"></div> 
 
            <div class="action-btns"> 
                <a id="pickfiles" class="login-button btn">Select files to upload</a> 
                <a href="javascript:;" id="upload_button" class="login-button upload hide">Upload</a> 
            </div> 
 
            <center> 
                <div id="upload_info"> 
                    <div id="upload_info_inner"> 
                        <div class="progressbg"> 
                            <div id="percent" class="progress"></div> 
                        </div> 
                    </div> 
 
                    <div id="unknown"> 
                        <div id="time2go"></div> 
                        <div id="upRate"></div> 
                    </div> 
                </div> 
            </center> 
 
            <form method="POST" action="process.php" id="processupload"> 
                <input type="hidden" name="uploaded_photos" id="uploaded_photos" /> 
                <input type="hidden" name="fkey" value="<?php echo $fkey; ?>" /> 
            </form> 
        </div>

upload.php页面代码如下:

代码如下:

<?php 
/*
    @@ Including the functions.php for using the necessary functions.
*/ 
include('functions.php'); 
 
/*
    @@ This is the file upload class which does all the uploading work.
*/ 
include('class.upload.php'); 
 
if(isset($_FILES["file"])) { 
    /*
        @@ Generating unique name for the photo.
    */ 
    $time = time(); 
    $rand_1 = rand(999, 999999); 
    $rand_2 = rand(999999, 999999999); 
    $rand_3 = rand(); 
    $unique_value = $time.'_'.$rand_1.'_'.$rand_2.'_'.$rand_3; 
 
    /*
        @@ Folder creation for each and every day. This ensures performance even with millions of images.
    */ 
    $folder = date('zY'); 
        if(substr($folder, 0) == 0) { 
            $folder = '367'.date('Y'); 
        } 
 
    /*
        @@ This folder is for the source image files.
    */ 
    $pfolder = '../upload/source/';  
        if(!is_dir($pfolder)) { 
            mkdir($pfolder, 0755); 
        } 
 
    /*
        @@ This folder is for the image files with 120x120 dimensions.
    */ 
    $tfolder = '../upload/small/'; 
        if(!is_dir($tfolder)) { 
            mkdir($tfolder, 0755); 
        } 
 
    /*
        @@ Assigning the upload file to the upload class for all the processing.
    */ 
    $handle = new Upload($_FILES["file"]); 
        if($handle->uploaded) { 
            $extension = $handle->file_src_name_ext; 
            $mime = $handle->file_src_mime; 
 
                if(($mime == 'image/gif') || ($mime == 'image/jpg') || ($mime == 'image/png') || ($mime == 'image/bmp') || ($mime == 'image/pjpeg') | ($mime == 'image/jpeg')) { 
                    /*
                        @@ Check if the uploaded filetype is an image or not.
                    */ 
                    if(($extension == 'gif') || ($extension == 'jpg') || ($extension == 'jpeg') || ($extension == 'png') || ($extension == 'bmp') || ($extension == 'pjpeg')) { 
                        if($handle->image_src_x > 500) { 
                                /*
                                    @@ Check if the filesize is smaller than 10 MB. 10 MB = 10485760 bytes.
                                */ 
                            if($handle->file_src_size < 10485760) { 
                                /*
                                    @@ Getting the actual file name (with and without extension) and sanitizing it for inserting in the database.
                                */ 
                                $real_name = clean_input($handle->file_src_name); 
                                $body_name = clean_input($handle->file_src_name_body); 
 
                                $handle->file_new_name_body = $unique_value.'_'.$body_name; 
                                $handle->Process($pfolder); 
 
                                $handle->image_resize = true; 
                                $handle->image_ratio_crop = 'T'; 
                                $handle->image_y = 120; 
                                $handle->image_x = 120; 
                                $handle->file_new_name_body = $unique_value.'_'.$body_name; 
                                $handle->Process($tfolder); 
 
                                    if($handle->processed) { 
                                        $actual_name = $handle->file_dst_name; 
                                        $size = ceil($handle->file_src_size / 1024); 
 
                                        ## Sending photo details back to the uploader. 
                                        $date = date("c", $time); 
 
                                            ## Reducing the length of real name if it exceeds 50 characters. 
                                            if(strlen($real_name) > 50) { 
                                                $real_name = substr($real_name, 0, 50).'..'; 
                                            } 
                                            echo 'success,'.$real_name.','.$date.','.$folder.','.$actual_name; 
                                    } else { 
                                        echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>There was an error uploading the photo.</div>'; 
                                    } 
                            } else { 
                                echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>The photo is bigger than the allowed upload size of <strong>10MB</strong>.</div>'; 
                            } 
                        } else { 
                            echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>You are trying to upload a photo with smaller dimensions.</div>'; 
                        } 
                    } else { 
                        echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>Only photo uploads are allowed.</div>'; 
                    } 
                } else { 
                    echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>Only photo uploads are allowed.</div>'; 
                } 
        } else { 
            echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>An upload error occured.</div>';            
        } 
    /*
        @@ Return the json response to the script.
    */ 
    $handle->Clean(); 
} else { 
    echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>An upload error occured.</div>'; 
}

希望本文所述对大家的PHP+jQuery程序设计有所帮助。

(0)

相关推荐

  • php实现按指定大小等比缩放生成上传图片缩略图的方法

    本文实例讲述了php实现按指定大小等比缩放生成上传图片缩略图的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: /**  * *  *等比缩放  * @param unknown_type $srcImage   源图片路径  * @param unknown_type $toFile     目标图片路径  * @param unknown_type $maxWidth   最大宽  * @param unknown_type $maxHeight  最大高  * @par

  • PHP Imagick完美实现图片裁切、生成缩略图、添加水印

    本文实例讲解了PHP使用Imagick 裁切.生成缩略图.添加水印自动检测和处理,支持gif,分享给大家供大家参考,具体内容如下 调用方式: include 'imagick.class.php'; $image = new lib_image_imagick(); $image->open('a.gif'); $image->resize_to(100, 100, 'scale_fill'); $image->add_text('1024i.com', 10, 20); $image-

  • php图片处理:加水印、缩略图的实现(自定义函数:watermark、thumbnail)

    废话不说了,贴代码: 复制代码 代码如下: <?php /************************************ //函数: watermark($bigimg, $smallimg, $coord = 1) //作用: 添加水印 //参数: $bigimg 必选.大图片--要加上水印的图片 $smallimg 必选.小图片 $coord 可选.水印在大图中的位置, 1 左上角: 2 右上角: 3 右下角: 4 左下角: 5 中间 //示例: watermark('datu.p

  • php图片的裁剪与缩放生成符合需求的缩略图

    图片太大且规格不统一,显示的控制需要靠JavaScript来完成,用在移动设备上时显示效果不好且流量巨大,需要对现有图片库的图片进行一次处理,生成符合移动设备用的缩略图,将原来客户端JS做的工作转移到服务器端用PHP的GD库来集中处理. 图片源与需要的大小: 复制代码 代码如下: $src_img = "wallpaper.jpg"; $dst_w = 300; $dst_h = 200; 剪裁图像,保证图像区域最大化显示,并按比例缩放到指定大小. 一开始采用了 imagecopyre

  • PHP基于GD库实现的生成图片缩略图函数示例

    本文实例讲述了PHP基于GD库实现的生成图片缩略图函数.分享给大家供大家参考,具体如下: <?php /** * 生成缩略图函数(支持图片格式:gif.jpeg.png和bmp) * @author ruxing.li * @param string $src 源图片路径 * @param int $width 缩略图宽度(只指定高度时进行等比缩放) * @param int $width 缩略图高度(只指定宽度时进行等比缩放) * @param string $filename 保存路径(不指

  • php gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码

    复制代码 代码如下: <?php //上传文件类型列表 $uptypes=array( 'image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png' ); $max_file_size = 200000; //上传文件大小限制, 单位BYTE $path_im = "prod_img/"; //生成大图保存文件夹路径 $path_sim = "

  • PHP用GD库生成高质量的缩略图片

    以下是PHP源代码(ResizeImage.php). 复制代码 代码如下: <?php $FILENAME="image.thumb"; // 生成图片的宽度 $RESIZEWIDTH=400; // 生成图片的高度 $RESIZEHEIGHT=400; function ResizeImage($im,$maxwidth,$maxheight,$name){ $width = imagesx($im); $height = imagesy($im); if(($maxwidt

  • 使用gd库实现php服务端图片裁剪和生成缩略图功能分享

    裁剪示例: 最终裁剪成的图片: 其中虚线框内就是要裁剪出来的图片,最终保存成100宽的图片.代码如下: 复制代码 代码如下: $src_path = '1.jpg';//创建源图的实例$src = imagecreatefromstring(file_get_contents($src_path)); //裁剪开区域左上角的点的坐标$x = 100;$y = 12;//裁剪区域的宽和高$width = 200;$height = 200;//最终保存成图片的宽和高,和源要等比例,否则会变形$fi

  • PHP图片等比例缩放生成缩略图函数分享

    复制代码 代码如下: <?php    /*    *@im     //需要缩放的图片资源    *@filetype //制作的缩略图文件类型    *@dstimW   //缩放的图片的宽度    *@dstimH  //缩放的图片的高度    *@thumbname //缩略图文件名字function makethumb($im,$dstimW,$dstimH,$thumbname ,$filetype){            //获取im的宽度和高度        $pic_W=im

  • php实现上传图片生成缩略图示例

    功能很简单,代码中有注释,直接给大家上代码了 复制代码 代码如下: <?php/** * 上传图片生成缩略图 *  * 需要GD2库的支持 *  * 初始化时需要参数new thumbnails('需要缩略的图片的原始地址','缩略图的宽度','缩略图的高度','(可选参数)缩略图的保存路径'); * 如果最后一个参数不指定,那么缩略图就默认保存在原始图片的所在目录里的small文件夹里, * 如果不存在small文件夹,则会自动创建small文件夹 *  * 初始化之后需要调用方法produc

  • 使用PHP生成图片的缩略图的方法

    功能:支持jpg,jpeg,gif,png,bmp图片格式,支持按原图片的比例进行缩放,可以选择在图片缩放的过程中是否需要对图片进行裁切,加入了图片质量控制,可以实现缩略图片质量最高化.完整类的代码如下: <?php /** * 功能:php生成缩略图片的类 */ class ResizeImage{ public $type;//图片类型 public $width;//实际宽度 public $height;//实际高度 public $resize_width;//改变后的宽度 publi

  • php使用GD库创建图片缩略图的方法

    本文实例讲述了php使用GD库创建图片缩略图的方法.分享给大家供大家参考.具体分析如下: 上传页面的静态html代码: <html> <head> <title>文件上传</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <H1>文件上传</H1> &l

随机推荐