$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
);
}
}
相关推荐
在这个"HTML5 CSS3:无插件拖拽上传图片实例源码"中,我们重点关注的是利用HTML5的拖放(Drag and Drop)API以及File API来实现图片的无插件上传功能。这种技术使得用户可以通过直接在页面上拖动文件到指定区域,...
总之,HTML5拖拽上传图片实例通过JavaScript和HTML5的API实现了用户友好的图片上传功能。它不仅提高了用户体验,也简化了上传流程。在实际开发中,可以根据需求进行优化,如增加多文件上传、进度条显示、错误处理等...
标题中的“无插件拖拽上传图片实例html源码”是指一种基于HTML5技术实现的图片上传功能,它不需要依赖任何外部插件,用户可以通过直接拖放图片到网页上来完成图片上传操作。这个实例通常会涉及到HTML5的新特性,如`...
在这个“html页面实现拖拽上传完整实例源码.zip”压缩包中,包含了一系列关键文件,用于构建一个功能完备的拖拽上传系统。 1. **index.php**:这是主入口文件,通常用于初始化页面结构并调用JavaScript库,如`...
### HTML5 拖拽上传实现详解 #### 一、引言 随着HTML5的普及与浏览器技术的进步,用户界面的交互方式变得越来越多样化。其中,“拖拽上传”功能就是一个非常实用且广受欢迎的例子。从Gmail的附件拖拽上传到网易...
3. 拖放上传:HTML5的拖放API使得用户可以直接从桌面拖拽文件到网页上。在layui中,我们需要监听`dragover`和`drop`事件,阻止默认的浏览器行为,并获取到拖放的文件。 4. 图片预览与缩放:利用HTML5的Canvas元素和...
在本文中,我们将深入探讨如何在Vue.js框架中实现一个功能丰富的图片上传组件,这个组件支持多图上传、拖拽上传以及在PC和手机端的兼容性。Vue.js是一个轻量级的前端JavaScript框架,以其易用性和灵活性而受到开发者...
拖拽上传则是允许用户将文件从桌面或文件夹直接拖入页面的特定区域进行上传。这需要监听`dragenter`, `dragleave`, 和 `drop`事件。在Vue组件模板中,我们可以设置一个`<div>`作为拖放区域: ```html ...
本文将深入探讨如何使用H5实现多图片预览、上传功能,并支持点击和拖拽操作,同时保持界面的美观和接口的简洁性。在图片上传控件的设计中,用户体验和功能实现是两个关键要素。 首先,我们要了解HTML5引入的新特性...
【jQuery图片拖拽实例】是一个基于JavaScript库jQuery实现的交互式功能,允许用户通过鼠标拖动操作来移动页面上的图片。这个实例展示了如何利用jQuery的事件处理和DOM操作能力,为用户提供直观且易于使用的界面体验...
在处理拖放数据时,确保符合数据安全和隐私政策,尤其是涉及到用户上传的图片或文件时。需要对用户输入进行验证和过滤,防止XSS或CSRF攻击。 综上所述,这个资源集合对于想要在移动端利用HTML5拖放功能的开发者来...
本项目"layui拖拽图片上传表单代码.zip"提供了一个实例,演示了如何利用这些技术实现一个功能完善的图片上传功能,包括拖拽上传和表单验证。 jQuery是一个轻量级的JavaScript库,它简化了DOM操作、事件处理、动画...
在“5.html5拖拽异步上传文件.zip”中,我们主要探讨如何利用这些特性来实现用户友好的文件上传体验。 拖放功能是HTML5提供的一种用户交互方式,允许用户通过鼠标直接从桌面或浏览器内部拖动元素到指定区域。在文件...
在“vue上传demo支持多选拖拽上传”这个项目中,我们主要探讨的是如何利用Vue.js实现文件上传功能,特别是支持多选文件和拖放操作。在JavaScript开发中,文件上传是一个常见的需求,而Vue.js提供了丰富的API和插件来...
2. **html5+nodejs+express实现拖拽上传的功能**:这个实例结合了HTML5的拖放API与后端的Node.js及Express框架。HTML5的拖放API允许用户直接在网页上拖动元素,包括文件,然后通过Ajax或Fetch API将文件数据发送到...
因为标题写的是实例,所以本次就不做讲解了,因为这个实例我也算是东拼西凑整出来的,参考了大概5、6款拖拽上传的插件和demo,然后把其中好的地方挑出来,最后就成了这么一个实例,一起来看下吧(地址不能保证长久...
【jQuery - 1.4.2 图片拖拽排序实例详解】 在Web开发中,实现用户界面的交互性是提高用户体验的重要手段。jQuery库因其简洁的API和强大的功能,成为了JavaScript开发者常用的选择。本实例主要关注的是使用jQuery ...
HTML5图片拖放功能是现代Web开发中的一个重要特性,它允许用户通过鼠标或触摸设备将图像从一个位置拖放到另一个位置,甚至可以在不同的浏览器窗口或应用程序之间进行操作。这一功能的实现基于HTML5的Drag and Drop ...