$r
=
new
stdClass();
header(
'content-type: application/json'
);
$maxsize
= 10;
if
(
$_FILES
[
'xfile'
][
'size'
] > (
$maxsize
* 1048576)){
$r
->error =
"图片大小不超过 $maxsize MB"
;
}
$folder
=
'files/'
;
if
(!
is_dir
(
$folder
)){
mkdir
(
$folder
);
}
$folder
.=
$_POST
[
'folder'
] ?
$_POST
[
'folder'
] .
'/'
:
''
;
if
(!
is_dir
(
$folder
)){
mkdir
(
$folder
);
}
if
(preg_match(
'/image/i'
,
$_FILES
[
'xfile'
][
'type'
])){
$filename
=
$_POST
[
'value'
] ?
$_POST
[
'value'
] :
$folder
. sha1(@microtime() .
'-'
.
$_FILES
[
'xfile'
][
'name'
]) .
'.jpg'
;
}
else
{
$tld
= split(
','
,
$_FILES
[
'xfile'
][
'name'
]);
$tld
=
$tld
[
count
(
$tld
) - 1];
$filename
=
$_POST
[
'value'
] ?
$_POST
[
'value'
] :
$folder
. sha1(@microtime() .
'-'
.
$_FILES
[
'xfile'
][
'name'
]) .
$tld
;
}
$types
= Array(
'image/png'
,
'image/gif'
,
'image/jpeg'
);
if
(in_array(
$_FILES
[
'xfile'
][
'type'
],
$types
)){
$source
=
file_get_contents
(
$_FILES
[
"xfile"
][
"tmp_name"
]);
imageresize(
$source
,
$filename
,
$_POST
[
'width'
],
$_POST
[
'height'
],
$_POST
[
'crop'
],
$_POST
[
'quality'
]);
}
else
{
move_uploaded_file(
$_FILES
[
"xfile"
][
"tmp_name"
],
$filename
);
}
$path
=
str_replace
(
'test.php'
,
''
,
$_SERVER
[
'SCRIPT_NAME'
]);
$r
->filename =
$filename
;
$r
->path =
$path
;
$r
->img =
'<img src="'
.
$path
.
$filename
.
'" alt="image" />'
;
echo
json_encode(
$r
);
function
imageresize(
$source
,
$destination
,
$width
= 0,
$height
= 0,
$crop
= false,
$quality
= 80) {
$quality
=
$quality
?
$quality
: 80;
$image
= imagecreatefromstring(
$source
);
if
(
$image
) {
$w
= imagesx(
$image
);
$h
= imagesy(
$image
);
if
((
$width
&&
$w
>
$width
) || (
$height
&&
$h
>
$height
)) {
$ratio
=
$w
/
$h
;
if
((
$ratio
>= 1 ||
$height
== 0) &&
$width
&& !
$crop
) {
$new_height
=
$width
/
$ratio
;
$new_width
=
$width
;
}
elseif
(
$crop
&&
$ratio
<= (
$width
/
$height
)) {
$new_height
=
$width
/
$ratio
;
$new_width
=
$width
;
}
else
{
$new_width
=
$height
*
$ratio
;
$new_height
=
$height
;
}
}
else
{
$new_width
=
$w
;
$new_height
=
$h
;
}
$x_mid
=
$new_width
* .5;
$y_mid
=
$new_height
* .5;
error_log
(
'height: '
.
$new_height
.
' - width: '
.
$new_width
);
$new
= imagecreatetruecolor(
round
(
$new_width
),
round
(
$new_height
));
imagecopyresampled(
$new
,
$image
, 0, 0, 0, 0,
$new_width
,
$new_height
,
$w
,
$h
);
if
(
$crop
) {
$crop
= imagecreatetruecolor(
$width
?
$width
:
$new_width
,
$height
?
$height
:
$new_height
);
imagecopyresampled(
$crop
,
$new
, 0, 0, (
$x_mid
- (
$width
* .5)), 0,
$width
,
$height
,
$width
,
$height
);
}
imageinterlace(
$crop
?
$crop
:
$new
, true);
$dext
=
strtolower
(
pathinfo
(
$destination
, PATHINFO_EXTENSION));
if
(
$dext
==
''
) {
$dext
=
$ext
;
$destination
.=
'.'
.
$ext
;
}
switch
(
$dext
) {
case
'jpeg'
:
case
'jpg'
:
imagejpeg(
$crop
?
$crop
:
$new
,
$destination
,
$quality
);
break
;
case
'png'
:
$pngQuality
= (
$quality
- 100) / 11.111111;
$pngQuality
=
round
(
abs
(
$pngQuality
));
imagepng(
$crop
?
$crop
:
$new
,
$destination
,
$pngQuality
);
break
;
case
'gif'
:
imagegif(
$crop
?
$crop
:
$new
,
$destination
);
break
;
}
@imagedestroy(
$image
);
@imagedestroy(
$new
);
@imagedestroy(
$crop
);
}
}
相关推荐
无插件拖拽上传图片实例html源码,直接可以使用,有需要下载!!
代码为博客实例代码:http://blog.csdn.net/lmj623565791/article/details/31513065 有问题博客中留言
支持拖拽上传的完整实例源码,
拖拽上传最重要的就是js部分的代码,它实现了70%的功能,另外30%仅仅是把图片信息提交到后台,然后做对应的处理,比如压缩啊,裁剪啊云云,感兴趣的朋友可以参考下哈,希望可以帮助到你
mvc3开发的上传图片功能,有完美的样式,可以拖拽图片进去,非常完美的实例,请多多评价!
因为标题写的是实例,所以本次就不做讲解了,因为这个实例我也算是东拼西凑整出来的,参考了大概5、6款拖拽上传的插件和demo,然后把其中好的地方挑出来,最后就成了这么一个实例,一起来看下吧(地址不能保证长久...
html5图片拖放
Ext4+Servlet/Struts2+HTML5实现实时获取文件上传进度、本地预览、文件拖拽上传的相册实例
UploadiFive基于HTML5和jQuery,不在需要Flash的支持,可以进行批量上传、拖拽上传、手机上传等。 官方网站:http://www.uploadify.com/ 官方文档:http://www.uploadify.com/documentation/ 应用实例:...
异步拖拽上传文件–小实例 upload.html <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and...
由于没有服务器支持在文章里不能做上传演示,下载实例 拖拽上传需要什么支持 1:需要浏览器支持 drop 事件。(响应拖拽事件获取file对象); 2:XMLHttpRequest 对象有 sendAsBinary 方法(用于发送数据); 以上两个条件 ...
随着HTML5的普及,现在大部分浏览器都支持拖拽功能,今天我们要说的就是实现一套拖拽上传的功能(Angularjs+nodejs)。 一、首先前端这款插件是基于AngularJS的,下面我们来看主要代码。 引入js: [removed][removed...
本文实例讲述了JS实现的文件拖拽上传功能。分享给大家供大家参考,具体如下: <!doctype html> <html> <head> <meta charset=utf-8> <title>www.jb51.net JS文件拖拽上传</title>...
UploadiFive基于HTML5和jQuery,不在需要Flash的支持,可以进行批量上传、拖拽上传、手机上传等。 官方网站:http://www.uploadify.com/ 官方文档:http://www.uploadify.com/documentation/ 应用实例:...
util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...
Layui 的上传是最常用的, 不可或缺, 记录一下代码, 以后复制都能用!! 1.前端HTML: 修改头像 点击上传图片,或将图片拖拽到此处 </div>
上传头像: 相关关键词: ondragover(拖动元素在投放区内移动) ondrop (元素放在投放区触发但是要去处理浏览器默认事件的影响:ondragenter、...上传头像图片</title> <link rel=stylesheet type=text/c
使用bootstrap界面美观,可预览,可拖拽上传,可配合ajax异步或同步上传,下面是效果图: 前端代码:fileinput.html <!DOCTYPE html> <!-- release v4.1.8, copyright 2014 - 2015 Kartik Visweswaran --&...
util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...