这是一个 图片批量上传的demo
选择多张图片,一次上传,并且生成缩略图以及原图。
index.php 选择文件 upload.php 上传文件 并生成缩略图 thumbnail.php 返回缩略图到浏览器
index.php
1 <?php 2 session_start(); 3 $_SESSION["file_info"] = array(); 4 ?> 5 <!DOCTYPE html> 6 <html> 7 <head> 8 <title>SWFUpload Demos - Application Demo</title> 9 <link href="css/default.css" rel="stylesheet" type="text/css" /> 10 <script type="text/javascript" src="../swfupload/swfupload.js"></script> 11 <script type="text/javascript" src="js/handlers.js"></script> 12 <script type="text/javascript"> 13 var swfu; 14 window.onload = function () { 15 swfu = new SWFUpload({ 16 // Backend Settings 17 upload_url: "upload.php", 18 post_params: {"PHPSESSID": "<?php echo session_id(); ?>"}, 19 20 // File Upload Settings 21 file_size_limit : "2 MB", // 2MB 22 file_types : "*.jpg", 23 file_types_description : "JPG Images", 24 file_upload_limit : 0, 25 26 // Event Handler Settings - these functions as defined in Handlers.js 27 // The handlers are not part of SWFUpload but are part of my website and control how 28 // my website reacts to the SWFUpload events. 29 swfupload_preload_handler : preLoad, 30 swfupload_load_failed_handler : loadFailed, 31 file_queue_error_handler : fileQueueError, 32 file_dialog_complete_handler : fileDialogComplete, 33 upload_progress_handler : uploadProgress, 34 upload_error_handler : uploadError, 35 upload_success_handler : uploadSuccess, 36 upload_complete_handler : uploadComplete, 37 38 // Button Settings 39 button_image_url : "images/SmallSpyGlassWithTransperancy_17x18.png", 40 button_placeholder_id : "spanButtonPlaceholder", 41 button_ 180, 42 button_height: 18, 43 button_text : '<span class="button">Select Images <span class="buttonSmall">(2 MB Max)</span></span>', 44 button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 10pt; }', 45 button_text_top_padding: 0, 46 button_text_left_padding: 18, 47 button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT, 48 button_cursor: SWFUpload.CURSOR.HAND, 49 50 // Flash Settings 51 flash_url : "../swfupload/swfupload.swf", 52 flash9_url : "../swfupload/swfupload_FP9.swf", 53 54 custom_settings : { 55 upload_target : "divFileProgressContainer" 56 }, 57 58 // Debug Settings 59 debug: false 60 }); 61 }; 62 </script> 63 </head> 64 <body> 65 <div id="content"> 66 <?php 67 if( !function_exists("imagecopyresampled") ){ 68 ?> 69 <div class="message"> 70 <h4><strong>Error:</strong> </h4> 71 <p>Application Demo requires GD Library to be installed on your system.</p> 72 <p>Usually you only have to uncomment <code>;extension=php_gd2.dll</code> by removing the semicolon <code>extension=php_gd2.dll</code> and making sure your extension_dir is pointing in the right place. <code>extension_dir = "c:phpextensions"</code> in your php.ini file. For further reading please consult the <a href="http://ca3.php.net/manual/en/image.setup.php">PHP manual</a></p> 73 </div> 74 <?php 75 } else { 76 ?> 77 <form> 78 <div style=" 180px; height: 18px; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;"> 79 <span id="spanButtonPlaceholder"></span> 80 </div> 81 </form> 82 <?php 83 } 84 ?> 85 <div id="divFileProgressContainer" style="height:75px;"></div> 86 <div id="thumbnails"></div> 87 </div> 88 </body> 89 </html>
thumbnail.php
1 <?php 2 // This script accepts an ID and looks in the user's session for stored thumbnail data. 3 // It then streams the data to the browser as an image 4 5 // Work around the Flash Player Cookie Bug 6 if (isset($_POST["PHPSESSID"])) { 7 session_id($_POST["PHPSESSID"]); 8 } 9 10 session_start(); 11 $image_id = isset($_GET["id"]) ? $_GET["id"] : false; 12 if ($image_id === false) { 13 header("HTTP/1.1 500 Internal Server Error"); 14 echo "No ID"; 15 exit(0); 16 } 17 18 if (!is_array($_SESSION["file_info"]) || !isset($_SESSION["file_info"][$image_id])) { 19 header("HTTP/1.1 404 Not found"); 20 exit(0); 21 } 22 23 header("Content-type: image/jpeg") ; 24 header("Content-Length: ".strlen($_SESSION["file_info"][$image_id])); 25 echo $_SESSION["file_info"][$image_id]; 26 exit(0); 27 ?>
upload.php
1 <?php 2 /* Note: This thumbnail creation script requires the GD PHP Extension. 3 If GD is not installed correctly PHP does not render this page correctly 4 and SWFUpload will get "stuck" never calling uploadSuccess or uploadError 5 */ 6 7 // Get the session Id passed from SWFUpload. We have to do this to work-around the Flash Player Cookie Bug 8 if (isset($_POST["PHPSESSID"])) { 9 session_id($_POST["PHPSESSID"]); 10 } 11 12 session_start(); 13 ini_set("html_errors", "0"); 14 15 // Check the upload 16 if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { 17 echo "ERROR:invalid upload"; 18 exit(0); 19 } 20 21 22 // Get the image and create a thumbnail 23 $img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]); 24 if (!$img) { 25 echo "ERROR:could not create image handle ". $_FILES["Filedata"]["tmp_name"]; 26 exit(0); 27 } 28 29 $width = imageSX($img); //600 30 $height = imageSY($img); //400 31 32 if (!$width || !$height) { 33 echo "ERROR:Invalid width or height"; 34 exit(0); 35 } 36 37 // Build the thumbnail 38 $target_width = 100; 39 $target_height = 100; 40 $target_ratio = $target_width / $target_height; //目标比例 1.0 41 42 $img_ratio = $width / $height; //原始比例 1.5 长方形 43 44 //比例调整 45 46 if ($target_ratio > $img_ratio) { //目标比例,大于原始比例 47 $new_height = $target_height; 48 $new_width = $img_ratio * $target_height; 49 } else { 50 $new_height = $target_width / $img_ratio; 51 $new_width = $target_width; 52 } 53 54 if ($new_height > $target_height) { 55 $new_height = $target_height; 56 } 57 if ($new_width > $target_width) { 58 $new_height = $target_width; 59 } 60 61 $new_img = ImageCreateTrueColor(100, 100); 62 if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0)) { // Fill the image black 63 echo "ERROR:Could not fill new image"; 64 exit(0); 65 } 66 67 if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height)) { 68 echo "ERROR:Could not resize image"; 69 exit(0); 70 } 71 72 if (!isset($_SESSION["file_info"])) { 73 $_SESSION["file_info"] = array(); 74 } 75 76 //upload source img crate new filename 77 $file_pathinfo_arr = pathinfo($_FILES['Filedata']['name']); 78 $file_extension = $file_pathinfo_arr['extension']; 79 $upload_path = dirname(__FILE__)."/attach/"; 80 $file_time = date("YmdHis",time()); 81 $source_picname = "scource_".$file_time.".".$file_extension; 82 $new_picname = "thumb_".$file_time.".".$file_extension; 83 84 // Use a output buffering to load the image into a variable 85 ob_start(); 86 imagejpeg($new_img); 87 $imagevariable = ob_get_contents(); 88 ob_end_clean(); 89 90 $file_id = md5($_FILES["Filedata"]["tmp_name"] + rand()*100000); 91 //upload 92 if (!move_uploaded_file($_FILES['Filedata']['tmp_name'],$upload_path.$source_picname)) 93 { 94 echo "ERROR:upload source file failed"; 95 exit(0); 96 } 97 98 $_SESSION["file_info"][$file_id] = $imagevariable; 99 100 echo "FILEID:" . $file_id; // Return the file id to the script 101 102 ?>