php 上传图片并生成缩略图
2021-01-16 17:11
标签:mime类型 while end copy rect upload open str array php 上传图片并生成缩略图 标签:mime类型 while end copy rect upload open str array 原文地址:https://www.cnblogs.com/qiuxd/p/13376405.htmlphp
// clearDir(‘upload/images/‘);
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);
}
}
}
?>