php 上传图片并生成缩略图,php 上传图片并生成缩略图

if ($_FILES[‘file‘][‘error‘] == 0) {$MAX_FILE_SIZE = 300000;if ($_FILES[‘file‘][‘size‘] > $MAX_FILE_SIZE) {exit(‘文件超出指定大小‘);

}$allowSuffix = [‘jpg‘, ‘png‘, ‘jpeg‘];$myImg = explode(‘.‘, $_FILES[‘file‘][‘name‘]);$mySuffix = array_pop($myImg);if (!in_array($mySuffix, $allowSuffix)) {exit(‘文件类型不对‘);

}$allowMine =[‘image/jpg‘,

‘image/jpeg‘,

‘image/pjpeg‘,

‘image/gif‘];if (!in_array($_FILES[‘file‘][‘type‘], $allowMine)) {exit(‘文件mime类型不对‘);

}$path = "upload/images/";$name = date(‘Ymdhis‘).mt_rand(0, 9). ‘.‘ .$mySuffix;if(is_uploaded_file($_FILES[‘file‘][‘tmp_name‘])) {if (move_uploaded_file($_FILES[‘file‘][‘tmp_name‘], $path.$name)) {$thumb_url = create_thumb($path.$name);$thumb_url2 = create_thumb($path.$name, 200, true);echo ‘上传成功, 缩略图路径为: ‘.$thumb_url;echo ‘方缩略图路径为: ‘.$thumb_url2;

}else{echo ‘上传失败‘;

}

}else{exit(‘不是上传文件‘);

}

}else{exit(‘上传失败‘);

}/**

* 给图片生成缩略图*/

function create_thumb($url, $max_width = 200, $is_square = false) {$image = imagecreatefromjpeg($url);list($ow, $oh) = getimagesize($url);$path_arr = explode(‘.‘, $url);//如果要求生成正方形的缩略图

if ($is_square) {if ($ow > $max_width) {$nw = $max_width;$new_image = imagecreatetruecolor($nw, $nw);$white = imagecolorallocate($new_image, 255, 255, 255);

imagefilledrectangle($new_image, 0, 0, $nw, $nw, $white);$o_wh = $ow > $oh ? $oh : $ow;

imagecopyresampled($new_image, $image, 0, 0, 0, 0, $nw, $nw, $o_wh, $o_wh);

imagejpeg($new_image, $path_arr[0].‘_thumbf.jpg‘);$result = $path_arr[0].‘_thumbf.jpg‘;

}else{$new_image = imagecreatetruecolor($ow, $ow);$white = imagecolorallocate($new_image, 255, 255, 255);

imagefilledrectangle($new_image, 0, 0, $ow, $ow, $white);if ($ow > $oh) {

imagecopyresampled($new_image, $image, 0, ($ow - $oh)/ 2, 0, 0, $ow, $oh, $ow, $oh);

}else{

imagecopyresampled($new_image, $image, 0, 0, 0, 0, $ow, $ow, $ow, $ow);

}

imagejpeg($new_image, $path_arr[0].‘_thumbf.jpg‘);$result = $path_arr[0].‘_thumbf.jpg‘;

}

}else{if ($ow > $max_width) {$percent = $max_width / $ow;$nw = $max_width;$nh = $oh * $percent;$new_image = imagecreatetruecolor($nw, $nh);

imagecopyresampled($new_image, $image, 0, 0, 0, 0, $nw, $nh, $ow, $oh);

imagejpeg($new_image, $path_arr[0].‘_thumb.jpg‘);$result = $path_arr[0].‘_thumb.jpg‘;

}else{$new_image = imagecreatetruecolor($ow, $oh);$white = imagecolorallocate($new_image, 255, 255, 255);

imagefilledrectangle($new_image, 0, 0, $ow, $oh, $white);

imagecopyresampled($new_image, $image, 0, 0, 0, 0, $ow, $oh, $ow, $oh);

imagejpeg($new_image, $path_arr[0].‘_thumb.jpg‘);$result = $path_arr[0].‘_thumb.jpg‘;

}

}

imagedestroy($image);return $result;

}//清空文件夹

function clearDir($dir) {if (is_dir($dir)) {if ($dh = opendir($dir)) {while ($file=readdir($dh)) {if (is_file($dir.$file)) {unlink($dir.$file);

}

}closedir($dh);

}

}

}?>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值