新浪云-PHP实现上传原图,缩略图

时间:2023-03-09 20:01:30
新浪云-PHP实现上传原图,缩略图
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<head>
<title>新浪云图片上传程序</title>
</head>
<form enctype="multipart/form-data" method="post" name="upform">
<input name="upfile" type="file">
<input type="submit" value="上传">
</form>
<?php
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function create_guid() {
$charid = strtoupper(md5(uniqid(mt_rand(), true)));
$uuid =
substr($charid, , )
.substr($charid, , )
.substr($charid,, )
.substr($charid,, );
return $uuid;
}
function UploadSmallImage($src,$w,$smallscr)
{
$temp=pathinfo($src);
$name=$smallscr;//文件名
$dir=$temp["dirname"];//文件所在的文件夹
$extension=$temp["extension"];//文件扩展名
$savepath="image_small/{$name}";//缩略图保存路径,新的文件名为*.thumb.jpg
$info=getimagesize($src);
$width=$info[];//获取图片宽度
$height=$info[];//获取图片高度
$type=$info[];
switch($type){
case :$im=imagecreatefromgif($src);break;
case :$im=imagecreatefromjpeg($src);break;
case :$im=imagecreatefrompng($src);break;
default:break;
}
if ($w == ) {
$h = ;
} elseif ($w == ) {
$h = ;
} elseif ($w == ) {
$h = ;
}
$temp_img=imagecreatetruecolor($w,$h);//创建画布
imagecopyresized($temp_img,$im,,,,,$w,$h,$width,$height);
$s = new SaeStorage();
ob_start();
imagejpeg($temp_img);
$imgstr = ob_get_contents();
$s->write('w376161501',$savepath,$imgstr);
ob_end_clean();
imagedestroy($im);
return $savepath;
}
function UploadBigImage($src,$smallscr)
{
$temp=pathinfo($src);
$name=$smallscr;//文件名
$dir=$temp["dirname"];//文件所在的文件夹
$extension=$temp["extension"];//文件扩展名
$savepath="image_big/{$name}";//缩略图保存路径,新的文件名为*.thumb.jpg
$info=getimagesize($src);
$width=$info[];//获取图片宽度
$height=$info[];//获取图片高度
$type=$info[];
switch($type){
case :$im=imagecreatefromgif($src);break;
case :$im=imagecreatefromjpeg($src);break;
case :$im=imagecreatefrompng($src);break;
default:break;
}
$s = new SaeStorage();
ob_start();
imagejpeg($im);
$imgstr = ob_get_contents();
$s->write('w376161501',$savepath,$imgstr);
ob_end_clean();
return $savepath;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$file = $_FILES["upfile"];
$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo['extension'];
$imagesmall=create_guid().".".$ftype;
$big=UploadBigImage($_FILES['upfile']['tmp_name'],$imagesmall);
$small=UploadSmallImage($_FILES['upfile']['tmp_name'],,$imagesmall);
echo '原图:'.$small.'缩略图:'.$big;
} ?>
</body>
</html>