thinkphp整合uploadify,单图片上传+预览
用户头像上传并预览,简洁实用的头像上传功能。
网上找了半天头像上传程序,没发现好用的,要么不兼容某些浏览器,要么功能不符合要求。查找相关资料后,发现thinkphp整合uploadify能满足我项目的需求。
动手整起,
首先,下载jquery.uploadify插件(最新版是3.2.1),把里面的jquery.uploadify.min.js放置到项目JS目录中,还有样式文件uploadify.css也放到项目CSS目录中,把uploadify.swf文件放置到随意目录,稍后正确引用即可。
第二步,在模板中引用以上JS和CSS文件(当然,jquery库文件也是必不可少的),在head区写入以下代码:<script><br />
$(function() {<br />
$('#file_upload').uploadify({<br />
'swf' : '/Public/js/uploadify.swf',<br />
'uploader' : '/Index/uploadify',<br />
'buttonText' : '上传头像',<br />
'onUploadSuccess' : function(file, data, response) {<br />
$('#image').attr('src','/Public/images/130_'+data);<br />
$('#pic').val(data);<br />
},<br />
});<br />
});<br />
</script>
这一步,实际上直接复制插件中的uploadify.php中的代码到控制中也是能接收图片并上传的,但不太好使(比如没有重命名、不能生成多张规格图片等),所以我控制器直接用了Thinkphp的方法,还有要解释下,onUploadSuccess方法是在文件上传成功后执行的方法,我这里是把默认显示的图片的src替换掉,以实时显示预览图,并且由于上传图片只是把图片传上去了,但并没有更新数据库,所以要把图片名赋给一个隐藏表单,在点击保存时再更新数据库。(这样做似乎不科学,但我想不出更好的办法了)
第三步:public function uploadify()<br>
{<br>
if (!empty($_FILES)) {<br>
import("@.ORG.UploadFile");<br>
$upload = new \Org\UploadFile();<br>
$upload->maxSize = 2048000;<br>
$upload->allowExts = array('jpg','jpeg','gif','png');<br>
$upload->savePath = "./Public/images/";<br>
$upload->thumb = true; //设置缩略图<br>
$upload->imageClassPath = "@.ORG.Image";<br>
$upload->thumbPrefix = "130_,75_,24_"; //生成多张缩略图<br>
$upload->thumbMaxWidth = "130,75,24";<br>
$upload->thumbMaxHeight = "130,75,24";<br>
$upload->saveRule = uniqid; //上传规则<br>
$upload->thumbRemoveOrigin = true; //删除原图<br>
<br>
if(!$upload->upload()){<br>
$this->error($upload->getErrorMsg());//获取失败信息<br>
} else {<br>
$info = $upload->getUploadFileInfo();//获取成功信息<br>
}<br>
echo $info[0]['savename']; //返回文件名给JS作回调用<br>
}<br>
}
第四步,接着,构建如下HTML代码:<div> <img src="/static/imghw/default1.png" data-src="/Public/images/130_{$u.photo}" class="lazy" alt="thinkphp整合uploadify,单图片上传+预览" ><br>
<div><input></div>
<br>
</div>
显示效果如下:
点击图片下方的上传头像,图片马上就上传到指定服务器目录,并且把缩略图给显示出来。
本程序有个弊端:如果用户不停地点击上传,会造成服务器上图片泛滥。。因为只有最后一次上传的才会保存入数据库,其他的都是废的了,而现在没有解决之。(提供一个思路:用户头像上传的时候,把用户ID作为图片名称前缀或后缀,以此来判断图片是否有用)
还有就是,上传成功后会有个complete的提示,我没有做处理,这个可以屏蔽掉的,如果不需要(需要的话可以自己定义样式)。
还有人说那个上传按钮不好看,可以用图片替换(buttonImage='xxx.png'),我反而觉得用图片替换更不好看。实际上这个是可以修改uploadify.css实现的,看我上面的上传按钮(提示:按钮的宽度和高度可以修改jquery.uploadify.min.js来实现,搜索"120"能找到[因为默认宽度是120],稍有基础就能看懂的)。
还有,这个插件是支持多图片上传的,不过我目前还没动手做,稍后几天可能会用到,到时再看如何修改。
有问题请跟帖,我会关注的。一起探讨与挖掘这个插件的强大之处!
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

PHP variables store values during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above
