新浪云-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, 0, 8)
.substr($charid, 8, 4)
.substr($charid,12, 4)
.substr($charid,16, 4);
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[0];//获取图片宽度
$height=$info[1];//获取图片高度
$type=$info[2];
switch($type){
case 1:$im=imagecreatefromgif($src);break;
case 2:$im=imagecreatefromjpeg($src);break;
case 3:$im=imagecreatefrompng($src);break;
default:break;
}
if ($w == 385) {
$h = 500;
} elseif ($w == 225) {
$h = 300;
} elseif ($w == 75) {
$h = 100;
}
$temp_img=imagecreatetruecolor($w,$h);//创建画布
imagecopyresized($temp_img,$im,0,0,0,0,$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[0];//获取图片宽度
$height=$info[1];//获取图片高度
$type=$info[2];
switch($type){
case 1:$im=imagecreatefromgif($src);break;
case 2:$im=imagecreatefromjpeg($src);break;
case 3:$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'],225,$imagesmall);
echo '原图:'.$small.'缩略图:'.$big;
}

?> 
</body> 
</html>
原文地址:https://www.cnblogs.com/wangboke/p/5603081.html