1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php 上传图片并生成缩略图 php 上传图片并生成缩略图

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

时间:2021-07-04 06:30:21

相关推荐

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);

}

}

}?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。