要求:
1,图片上传后重命名
2,上传后的原图不留在服务器
3,图片根据高与宽压缩
4,生成三张缩略图
5,文件名写入数据库
在本地测试可以,传上服务器就不能用
请高手诊断并修改,谢谢
代码如下:
<?php
header('Content-type: text/html;charset=utf-8');
$upimg=$_FILES['upfile']; if(!file_exists($_SESSION['user_id']))//检查照片目录是否存在
{
mkdir($_SESSION['user_id']);
} //开始压缩图片
function createThumbImage($original,$thumb_path,$new_width,$new_height,$ext=null)
{
if ($ext==null)
{
$ext=end(explode(".",$original)); //文件后缀名 jpg
}
$ext=strtolower($ext); //将英文字母转换成上写
$filename = $original;
list($width, $height) = getimagesize($filename);
$image_p = imagecreatetruecolor($new_width, $new_height);
switch ($ext)
{
case 'jpg':
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $thumb_path, 100);
return true;
case 'gif':
$image = imagecreatefromgif($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagegif($image_p, $thumb_path, 100);
return true;
case 'png':
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($image_p, $thumb_path, 100);
return true;
}
return false;
} //获取上传图片的宽高
list($widthold, $heightold) = getimagesize($upimg);
//新大图片的宽高
if($widthold>=$heightold){
if($widthold<=678){
$bigwidth=$widthold;
$bigheight=$heightold;
}else{
$bigwidth=678;
$bigheight=678*$heightold/$widthold;
}
}else{
if($heightold<=450){
$bigwidth=$widthold;
$bigheight=$heightold;
}else{
$bigheight=450;
$bigwidth=450*$widthold/$heightold;
}
} //新中图片的宽高
if($widthold>=$heightold){
if($widthold<=430){
$midwidth=$widthold;
$midheight=$heightold;
}else{
$midwidth=430;
$midheight=430*$heightold/$widthold;
}
}else{
if($heightold<=300){
$midwidth=$widthold;
$midheight=$heightold;
}else{
$midheight=300;
$midwidth=300*$widthold/$heightold;
}
} //新小图片的宽高
if($widthold>=$heightold){
if($widthold<=85){
$smallwidth=$widthold;
$smallheight=$heightold;
}else{
$smallwidth=85;
$smallheight=85*$heightold/$widthold;
}
}else{
if($heightold<=68){
$smallwidth=$widthold;
$smallheight=$heightold;
}else{
$smallheight=68;
$smallwidth=68*$widthold/$heightold;
}
}
//以下生成时间+随机数生成文件名,不重复
$exes=split("\.",$upimg);
$exes=strtolower($exes[1]);
//以上是取得文件的扩展名
$nowdate=date("Ymdhis");
$rd=Rand(1,1000);
$bigfilename="img/".$nowdate."_".$rd."_big.".$exes;
$midfilename="img/".$nowdate."_".$rd."_mid.".$exes;
$smallfilename="img/".$nowdate."_".$rd.".".$exes;
//压缩图片
createThumbImage($upimg,$bigfilename,$bigwidth,$bigheight);
createThumbImage($upimg,$midfilename,$midwidth,$midheight);
createThumbImage($upimg,$smallfilename,$smallwidth,$smallheight);
//压缩图片和保存图片结束
$userimg_query = mysql_query("update db_photo set user_bigimg = '".$bigfilename."' , user_midimg = '".$midfilename."' , user_img = '".$smallfilename."' where user_id = '".$_SESSION['user_id']."'"); if($userimg_query)
{
echo "<script language='javascript'>";
echo "alert('上传已成功!');";
echo "history.go(-1);";
echo "</script>";
}
else
{
echo "<script language='javascript'>";
echo "alert('上传已成功!');";
echo "history.go(-1);";
echo "</script>";
}?>
1,图片上传后重命名
2,上传后的原图不留在服务器
3,图片根据高与宽压缩
4,生成三张缩略图
5,文件名写入数据库
在本地测试可以,传上服务器就不能用
请高手诊断并修改,谢谢
代码如下:
<?php
header('Content-type: text/html;charset=utf-8');
$upimg=$_FILES['upfile']; if(!file_exists($_SESSION['user_id']))//检查照片目录是否存在
{
mkdir($_SESSION['user_id']);
} //开始压缩图片
function createThumbImage($original,$thumb_path,$new_width,$new_height,$ext=null)
{
if ($ext==null)
{
$ext=end(explode(".",$original)); //文件后缀名 jpg
}
$ext=strtolower($ext); //将英文字母转换成上写
$filename = $original;
list($width, $height) = getimagesize($filename);
$image_p = imagecreatetruecolor($new_width, $new_height);
switch ($ext)
{
case 'jpg':
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $thumb_path, 100);
return true;
case 'gif':
$image = imagecreatefromgif($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagegif($image_p, $thumb_path, 100);
return true;
case 'png':
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($image_p, $thumb_path, 100);
return true;
}
return false;
} //获取上传图片的宽高
list($widthold, $heightold) = getimagesize($upimg);
//新大图片的宽高
if($widthold>=$heightold){
if($widthold<=678){
$bigwidth=$widthold;
$bigheight=$heightold;
}else{
$bigwidth=678;
$bigheight=678*$heightold/$widthold;
}
}else{
if($heightold<=450){
$bigwidth=$widthold;
$bigheight=$heightold;
}else{
$bigheight=450;
$bigwidth=450*$widthold/$heightold;
}
} //新中图片的宽高
if($widthold>=$heightold){
if($widthold<=430){
$midwidth=$widthold;
$midheight=$heightold;
}else{
$midwidth=430;
$midheight=430*$heightold/$widthold;
}
}else{
if($heightold<=300){
$midwidth=$widthold;
$midheight=$heightold;
}else{
$midheight=300;
$midwidth=300*$widthold/$heightold;
}
} //新小图片的宽高
if($widthold>=$heightold){
if($widthold<=85){
$smallwidth=$widthold;
$smallheight=$heightold;
}else{
$smallwidth=85;
$smallheight=85*$heightold/$widthold;
}
}else{
if($heightold<=68){
$smallwidth=$widthold;
$smallheight=$heightold;
}else{
$smallheight=68;
$smallwidth=68*$widthold/$heightold;
}
}
//以下生成时间+随机数生成文件名,不重复
$exes=split("\.",$upimg);
$exes=strtolower($exes[1]);
//以上是取得文件的扩展名
$nowdate=date("Ymdhis");
$rd=Rand(1,1000);
$bigfilename="img/".$nowdate."_".$rd."_big.".$exes;
$midfilename="img/".$nowdate."_".$rd."_mid.".$exes;
$smallfilename="img/".$nowdate."_".$rd.".".$exes;
//压缩图片
createThumbImage($upimg,$bigfilename,$bigwidth,$bigheight);
createThumbImage($upimg,$midfilename,$midwidth,$midheight);
createThumbImage($upimg,$smallfilename,$smallwidth,$smallheight);
//压缩图片和保存图片结束
$userimg_query = mysql_query("update db_photo set user_bigimg = '".$bigfilename."' , user_midimg = '".$midfilename."' , user_img = '".$smallfilename."' where user_id = '".$_SESSION['user_id']."'"); if($userimg_query)
{
echo "<script language='javascript'>";
echo "alert('上传已成功!');";
echo "history.go(-1);";
echo "</script>";
}
else
{
echo "<script language='javascript'>";
echo "alert('上传已成功!');";
echo "history.go(-1);";
echo "</script>";
}?>
$userimg_query = mysql_query("update db_photo set user_bigimg = '".$bigfilename."' , user_midimg = '".$midfilename."' , user_img = '".$smallfilename."' where user_id = '".$_SESSION['user_id']."'"); if($userimg_query)
{
echo " <script language='javascript'>";
echo "alert('上传已成功!');";
echo "history.go(-1);";
echo " </script>";
}
else
{
echo " <script language='javascript'>";
echo "alert('上传已成功!');";
echo "history.go(-1);";
echo " </script>";
}
这些怎么看着这么别扭呢?