标签:
if($_FILES[‘img_slide‘][‘name‘]=="")
{
$phpto_dir_name="";
header("Content-type:text/html;charset=utf-8");
echo "没有选择上传图片";
return $this->getDefaultView();
}else{
//将临时文件复制到upload_image目录下
$img_slide=($_FILES[‘img_slide‘][‘tmp_name‘]);
$smalladdrname="../upfile/";//缩略图存放目录位置
$RESIZEWIDTH=‘795‘;//定义最大宽
$RESIZEHEIGHT=‘440‘;//定义最大高
if($_FILES[‘img_slide‘][‘size‘]){
if($_FILES[‘img_slide‘][‘type‘] == "image/pjpeg" || $_FILES[‘img_slide‘][‘type‘] == "image/jpeg"){
$im = imagecreatefromjpeg($_FILES[‘img_slide‘][‘tmp_name‘]);
}elseif($_FILES[‘img_slide‘][‘type‘] == "image/x-png" || $_FILES[‘img_slide‘][‘type‘] == "image/png"){
$im = imagecreatefrompng($_FILES[‘img_slide‘][‘tmp_name‘]);
}elseif($_FILES[‘img_slide‘][‘type‘] == "image/gif"){
$im = imagecreatefromgif($_FILES[‘img_slide‘][‘tmp_name‘]);
}
if($im){
//图片限制大小
if( $width = imagesx($im) < 795 || $height = imagesy($im) < 440 ){
echo "图片太小了" .
"location.href=‘index.php?module=Slide‘;</script>";
return $this->getDefaultView();
}
//生成新的文件名
$basename = time().rand(10,99);
$newname = $smalladdrname.‘slt_‘.$basename.‘.jpg‘;
// 图片是否存在
if(file_exists($newname.".jpg")){
unlink($newname.".jpg");
}
$this->ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$newname);
ImageDestroy ($im);
$phpto_dir_name = basename($newname);
}else{
echo "上传失败";
return $this->getDefaultView();
}
}
//插入数据
$sql = "insert into ntc_slide(img_title,img_slide,img_url,bgcolor,sort,add_date) " .
"values(‘$img_title‘,‘$phpto_dir_name‘,‘$img_url‘,‘$bgcolor‘,‘$sort‘,CURRENT_TIMESTAMP)";
$r = $db->insert($sql);
}
//调用方法
function ResizeImage($im,$maxwidth,$maxheight,$name){
$smalladdrname="../upfile/";//缩略图存放目录位置
$RESIZEWIDTH=‘795‘;//定义最大宽
$RESIZEHEIGHT=‘440‘;//定义最大高
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
$newwidth = 795;
$newheight = 440;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$smalladdrname.$name);
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$smalladdrname.$name);
}
}
标签:
原文地址:http://www.cnblogs.com/www-xiaobo-com/p/4802749.html