php上传图片并显示图片代码(php怎么上传图片并保存到数据库)
其实php上传图片并显示图片代码的问题并不复杂,但是又很多的朋友都不太了解php怎么上传图片并保存到数据库,因此呢,今天小编就来为大家分享php上传图片并显示图片代码的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!
php+mysql图片的插入、显示操作代码
如果存图片流,数据库会非常庞大,建议还是存路径,图片放到统一的目录下,方便管理
存就是insert字段用vchar的存相对路径就可以了
读就是查数据库然后放到数组里、
显示<img src='读出来的变量'>就可以了
function uploadPhoto($file){
$this->result= false;
$this->error= false;
//-- save parameters
$this->_file=$file;
//get path
$this->createUploadDir();
$this->_destination=SystemProperties::$UPLOAD_ROOT_PATH['photo'];
//if(!is_null($allowed)){$this->_allowed=$allowed;} else{$this->_allowed= array('jpg','jpeg','gif','png');}
//-- check that FILE array is even set
if(isset($file)&& is_array($file)&&!$this->upload_error($file['error'])){
//-- cool, now set some variables
$fileID=$this->_IDGenerator->getNextId('IMAGE');
$fileExt=$this->ext($file['name']);
$fileName=$fileID.'.'.$fileExt;
$this->createFileDir('image',$fileName);
$fileTmp=$file['tmp_name'];
//$fileSize=$file['size'];
//$fileType=$file['type'];
$fileError=$file['error'];
//-- update name
$this->_name=$this->_destination.$fileName;
//-- it's been uploaded with php
if(is_uploaded_file($fileTmp)){
//-- where to put the file?
$filePath=$this->_fileUtil->getFilePath($fileName);
$output=$this->_destination.$filePath['filePath'];
//resize the img first
$isFileName_f=$this->resizeImage($this->_file,'f',$filePath['noExtFileName'].'_f'.$filePath['ext']);
//or resize the img like this,choosed type in('a','b','c')
//$isFileName_b=$this->resizeImage($this->_file,'b',$filePath['noExtFileName'].'_b'.$filePath['ext']);
if($isFileName_a==true&&$isFileName_b==true&&$isFileName_c==true&&$isFileName_d==true){
//-- just upload it
if(move_uploaded_file($fileTmp,$output)){
chmod($output, 0644);
$this->result= basename($this->_name);
return$this->result;
} else{
$this->error("Could not move'$fileName' to'$this->_destination'");
}
}
} else{
$this->error("Possible file upload attack on'$fileName'");
}
} else{
$this->error("Possible file upload attack");
}
}
用php框架的,和纯php写不一样,但是问题不大就是一个思路,没有完全通用的代码,前台加个上传框,做个form传到后台就不用写了吧,传过来的参数$file就是文件,也是个数组用$_FILE[]能看到,首先校验后缀名或者前台js校验,move_uploaded_file这句是最重要的一句,其他都是辅助准备参数而已,意思就是把传上来的东西放到哪去,路径加文件名$output这个参数就是你要保存到数据库的值了
php中如何调用数据库中的图片并且显示到页面
php是采用二进制形式存储图片及读取显示的,首先通过代码创建数据表,然后上传图片服务器再通过浏览器显示,具体编程代码举例:
1、首先需要创建数据表,具体代码如下图所示。
2、然后写上传图片到服务器的页面 upimage.html用来将图片上传数据库,如下图所示代码。
3、处理图片上传的php upimage.php文件,如下图所示图片已储存到数据库。
4、显示图片的php getimage.php文件,为了看一下效果提前把ID写入代码。
5、预览网站从数据库中提取了图片,并显示到页面上。
PHP 图片上传生成缩略图
//2014年3月5日15:08:02因为需要做缩略图,所以改用thinkphp来做上传,它支持时间戳命名,方便命名,以及更名
//这是以前百度到的,然后使用的缩略图代码,需要cg库支持
/**
*生成缩略图
*@authoryangzhiguo0903@163.com
*@paramstring源图绝对完整地址{带文件名及后缀名}
*@paramstring目标图绝对完整地址{带文件名及后缀名}
*@paramint缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
*@paramint缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
*@paramint是否裁切{宽,高必须非0}
*@paramint/float缩放{0:不缩放,0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
*@returnboolean
*/
functionfileext($file)
{
returnstrtolower(pathinfo($file,PATHINFO_EXTENSION));
}
functionimg2thumb($src_img,$dst_img,$width=75,$height=75,$cut=0,$proportion=0)
{
if(!is_file($src_img))
{
returnfalse;
}
$ot=$this->fileext($dst_img);
$otfunc='image'.($ot=='jpg'?'jpeg':$ot);
$srcinfo=getimagesize($src_img);
$src_w=$srcinfo[0];
$src_h=$srcinfo[1];
$type=strtolower(substr(image_type_to_extension($srcinfo[2]),1));
$createfun='imagecreatefrom'.($type=='jpg'?'jpeg':$type);
$dst_h=$height;
$dst_w=$width;
$x=$y=0;
/**
*缩略图不超过源图尺寸(前提是宽或高只有一个)
*/
if(($width>$src_w&&$height>$src_h)||($height>$src_h&&$width==0)||($width>$src_w&&$height==0))
{
$proportion=1;
}
if($width>$src_w)
{
$dst_w=$width=$src_w;
}
if($height>$src_h)
{
$dst_h=$height=$src_h;
}
if(!$width&&!$height&&!$proportion)
{
returnfalse;
}
if(!$proportion)
{
if($cut==0)
{
if($dst_w&&$dst_h)
{
if($dst_w/$src_w>$dst_h/$src_h)
{
$dst_w=$src_w*($dst_h/$src_h);
$x=0-($dst_w-$width)/2;
}
else
{
$dst_h=$src_h*($dst_w/$src_w);
$y=0-($dst_h-$height)/2;
}
}
elseif($dst_wxor$dst_h)
{
if($dst_w&&!$dst_h)//有宽无高
{
$propor=$dst_w/$src_w;
$height=$dst_h=$src_h*$propor;
}
elseif(!$dst_w&&$dst_h)//有高无宽
{
$propor=$dst_h/$src_h;
$width=$dst_w=$src_w*$propor;
}
}
}
else
{
if(!$dst_h)//裁剪时无高
{
$height=$dst_h=$dst_w;
}
if(!$dst_w)//裁剪时无宽
{
$width=$dst_w=$dst_h;
}
$propor=min(max($dst_w/$src_w,$dst_h/$src_h),1);
$dst_w=(int)round($src_w*$propor);
$dst_h=(int)round($src_h*$propor);
$x=($width-$dst_w)/2;
$y=($height-$dst_h)/2;
}
}
else
{
$proportion=min($proportion,1);
$height=$dst_h=$src_h*$proportion;
$width=$dst_w=$src_w*$proportion;
}
$src=$createfun($src_img);
$dst=imagecreatetruecolor($width?$width:$dst_w,$height?$height:$dst_h);
$white=imagecolorallocate($dst,255,255,255);
imagefill($dst,0,0,$white);
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($dst,$src,$x,$y,0,0,$dst_w,$dst_h,$src_w,$src_h);
}
else
{
imagecopyresized($dst,$src,$x,$y,0,0,$dst_w,$dst_h,$src_w,$src_h);
}
$otfunc($dst,$dst_img);
imagedestroy($dst);
imagedestroy($src);
returntrue;
}
关于php上传图片并显示图片代码和php怎么上传图片并保存到数据库的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。