php文件接收上传的图片,PHP文件(图片)上传

提交表单,表单设置属性enctype="multipart/form-data"

提交处理页面:

$imgname = strtolower($_FILES['image']['name']);

$type = substr(strrchr($imgname, '.'), 1);

if (!in_array($type, array('jpg', 'jpeg', 'png', 'gif', 'bmp'))) {

echo('当前图片图片格式不支持,支持jpg、gif、png、bmp格式!');

exit;

}

$upload_tmp_file = ROOT . '/data/tmp/article_' . random(6, 0) . '.' . $type;

$filepath = '/data/attach/article/article' . random(6, 0) . '.' . $type;

mkdir(ROOT . '/data/attach/article');

if (move_uploaded_file($_FILES['image']['tmp_name'], $upload_tmp_file)) {

image_resize($upload_tmp_file, ROOT . $filepath, 260, 180);    //生成缩略图

echo('上传成功');

}

//创建目录

function mkdir($path) {

if (!file_exists($path)) {

forcemkdir(dirname($path));

mkdir($path, 0777);

}

}

//生成缩略图

function image_resize($src, $dst, $width, $height, $crop = 0) {

if (!list($w, $h) = getimagesize($src))

return "Unsupported picture type!";

$type = strtolower(substr(strrchr($src, "."), 1));

if ($type == 'jpeg')

$type = 'jpg';

switch ($type) {

case 'bmp': $img = imagecreatefromwbmp($src);

break;

case 'gif': $img = imagecreatefromgif($src);

break;

case 'jpg': $img = imagecreatefromjpeg($src);

break;

case 'png': $img = imagecreatefrompng($src);

break;

default : return false;

}

// resize

if ($crop) {

if ($w < $width or $h < $height) {

rename($src, $dst);

return true;

}

$ratio = max($width / $w, $height / $h);

$h = $height / $ratio;

$x = ($w - $width / $ratio) / 2;

$w = $width / $ratio;

} else {

if ($w < $width and $h < $height) {

rename($src, $dst);

return true;

}

$ratio = min($width / $w, $height / $h);

$width = $w * $ratio;

$height = $h * $ratio;

$x = 0;

}

$new = imagecreatetruecolor($width, $height);

// preserve transparency

if ($type == "gif" or $type == "png") {

imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));

imagealphablending($new, false);

imagesavealpha($new, true);

}

imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);

switch ($type) {

case 'bmp': imagewbmp($new, $dst);

break;

case 'gif': imagegif($new, $dst);

break;

case 'jpg': imagejpeg($new, $dst);

break;

case 'png': imagepng($new, $dst);

break;

}

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值