图片批量上传,并生成缩略图demo

摘要:
Index.php选择文件upload.php上载文件并生成缩略图缩略图。php将缩略图返回到浏览器索引。php1˂?

这是一个 图片批量上传的demo

选择多张图片,一次上传,并且生成缩略图以及原图。

图片批量上传,并生成缩略图demo第1张

index.php 选择文件  upload.php 上传文件  并生成缩略图  thumbnail.php 返回缩略图到浏览器

index.php

图片批量上传,并生成缩略图demo第2张图片批量上传,并生成缩略图demo第3张
 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="http://t.zoukankan.com/css/default.css" rel="stylesheet" type="text/css" />
10 <script type="text/javascript" src="http://t.zoukankan.com/swfupload/swfupload.js"></script>
11 <script type="text/javascript" src="http://t.zoukankan.com/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   style="height:75px;"></div>
86     <div id="thumbnails"></div>
87 </div>
88 </body>
89 </html>
View Code

thumbnail.php

图片批量上传,并生成缩略图demo第4张图片批量上传,并生成缩略图demo第5张
 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 ?>
View Code

upload.php

图片批量上传,并生成缩略图demo第6张图片批量上传,并生成缩略图demo第7张
  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 ?>
View Code

免责声明:文章转载自《图片批量上传,并生成缩略图demo》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇LocalBroadcastManager 使用小解关于uboot的一些优化下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python+OpenCV图像处理之模板匹配

模板匹配就是在整个图像区域中发现与给定子图像匹配的小块区域 在OpenCV中,提供了相应的函数完成这个操作: matchTemplate 函数:在模板和输入图像之间寻找匹配,获得匹配结果图像 minMaxLoc 函数:在给定的矩阵中寻找最大和最小值,并给出它们的位置 几种常见的模板匹配算法: ①TM_SQDIFF是平方差匹配;TM_SQDIFF_NORME...

PHP调用Linux的命令行执行文件压缩命令&amp;amp;&amp;amp;创建文件夹修改权限

一开始,我和普通青年一样,想到用PHP内置的 ZipArchive纠结的是环境上没安装zip扩展,想采用用PHP调用Linux的命令行 ,执行压缩命令,感兴趣的朋友可以了解下,希望本文对你有所帮助 前几天工作中,需要将3个txt文件,打包成*.zip down到本地…… 一开始,我和普通青年一样,想到用PHP内置的 ZipArchive,代码看起来应该是...

php如何将图片gif,jpg或mysql longblob或blob字段值转换成16进制字符串!编辑

php如何将图片gif,jpg或mysql longblob或blob字段值转换成16进制字符串!经过查找居然如此简单,一个函数就搞定了。 数据库脚本: -- -------------------------------------------------------- -- -- 表的结构 `highot_attachment` -- CREATE T...

iOS开发基础知识--碎片26

  iOS开发基础知识--碎片26  1:UICollectionView如果在数据不够一屏时上下滚动 当数据不多,collectionView.contentSize小于collectionView.frame.size的时候,UICollectionView是不会滚动的,可以增加下面代码就可以: self.myCollectionView.alwa...

Bypass_disable_func

1、系统命令执行函数的黑名单绕过 system() shell_exec() === ``反引号 exec() passthru() popen() proc_open() pcntl_exec() dl() // 加载自定义 php 扩展,5.3以后被废弃 2、通过LD_PRELOAD偷梁换柱 先来了解下 LD_PRELOAD: LD_PRELOAD...

php中访问文件或文件夹相关操作

1、filetype() //可以输出相关文件类型,如:dir(表示目录)/file(表示文件) 如:echo filetype("c:/") 输出结果为:dir 如:echo filetype("f:/num.txt") 输出结果为:file 2、stat() //获得指定文件名参数目标文件基本属性 $stt=stat("f:/num.txt"); pr...