您好,欢迎访问一九零五行业门户网

php上传图片等比缩放图片的代码

1){ //宽度较大 if($imagewidth > $width){ //缩放图片到背景图片上 $new_width = $width; $new_height = ($width*$imageheight)/$imagewidth; $bg_y = ceil(abs(($height-$new_height)/2)); imagecopyresampled($bgimg, $im, 0, $bg_y, 0, 0, $new_width, $new_height, $imagewidth, $imageheight); }else{ //复制图片到背景图片上 $copy = true; } }else{ //高度较大 if($imageheight > $height){ //缩放图片 $new_height = $height; $new_width = ($height*$imagewidth)/$imageheight; $bg_x = ceil(($width-$new_width)/2); imagecopyresampled($bgimg, $im, $bg_x, 0, 0, 0, $new_width, $new_height, $imagewidth, $imageheight); }else{ //复制图片到背景图片上 $copy = true; } } if($copy){ //复制图片到背景图片上 $bg_x = ceil(($width-$imagewidth)/2); $bg_y = ceil(($height-$imageheight)/2); imagecopy($bgimg, $im, $bg_x, $bg_y, 0, 0, $imagewidth, $imageheight); } $ext = _file_type($mime); $outfun($bgimg, $savepath.'/'.$ext); imagedestroy($bgimg); return $savepath.'/'.$ext; }else{ return false; } } if($_post){ $size = $_post['size']?strtoupper(trim($_post['size'])):'2m'; $imgsize = $_files['img']['size']?$_files['img']['size']/(1024*1024):0; $imgwidth = $imgheight = $_post['width-height']?intval($_post['width-height']):300; //自定定义文件上传大小 ini_set('upload_max_filesize',$size); $mathsize = str_replace('m','',$size); if($imgsize>$mathsize){ echo 图片大小不得超过{$size}!; return; } if($file_name = _file_type($_files['img']['type'])){ if($_files['img']['error'] == upload_err_ok){ $savepath = 'upload/'; if(!is_dir($savepath)){ mkdir($savepath,0644); } //生成缩略图 $thumb_file = _make_thumb($_files['img']['tmp_name'], $imgwidth, $imgheight, $savepath); //move_uploaded_file($_files['img']['tmp_name'],$savepath.$file_name); echo 生成后的图片为:; }else{ echo $_files['img']['error']; return; } }else{ echo 图片格式不正确,请上传jpg,gif,png的格式!; return; }}else{
echo 缩放图片保存成正方形 上传一张图片:
生成缩略图的宽高(单位px):
文件大小上限:
eot; }
复制代码
其它类似信息

推荐信息