当前位置:首页 » 编程语言 » phpcreate

phpcreate

发布时间: 2025-05-25 08:25:06

php图像生成函数imagecreatetruecolor和imagecreate的区别

resource imagecreatetruecolor ( int $x_size , int $y_size )
返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
resource imagecreate ( int $x_size , int $y_size )
返回一个图像标识符,代表了一幅大小为
两者在改变背景颜色时有些区别:
imagecreatetruecolor需要用imagefill()来填充颜色
imagecreate()需要用imagecolorAllocate()添加背景色
php案例如下:

<?php$img = imagecreatetruecolor(100,100); //创建真彩图像资源$color = imagecolorAllocate($img,200,200,200); //分配一个灰色imagefill($img,0,0,$color); // 从左上角开始填充灰色header('content-type:image/jpeg'); //jpg格式imagejpeg($img); //显示灰色的方块?>

<?php$img = imagecreate(100,100);imagecolorallocate($img,200,200,200);header('content-type:image/jpeg'); imagejpeg($img); ?>

http://www.phpddt.com/php/imagecreate.html

❷ 如何实现PHP自动创建数据库

你做好程序以后,把数据库导出成sql文件
1、连接数据库
2、读取这个sql文件里的sql语句,并执行
3、生成一个数据库连接参数的php文件
<?php
$con=mysql_connect("localhost","peter","abc123");
if(!$con)
{
die('Couldnotconnect:'.mysql_error());
}

if(mysql_query("CREATEDATABASEmy_db",$con))
{
echo"Databasecreated";
}
else
{
echo"Errorcreatingdatabase:".mysql_error();
}

mysql_close($con);
?>

<?php
classReadSql{
//数据库连接
protected$connect=null;
//数据库对象
protected$db=null;
//sql文件
public$sqlFile="";
//sql语句集
public$sqlArr=array();
publicfunction__construct($host,$user,$pw,$db_name){
$host=empty($host)?C("DB_HOST"):$host;
$user=empty($user)?C("DB_USER"):$user;
$pw=empty($pw)?C("DB_PWD"):$pw;
$db_name=empty($db_name)?C("DB_NAME"):$db_name;
//连接数据库
$this->connect=mysql_connect($host,$user,$pw)ordie("Couldnotconnect:".mysql_error());
$this->db=mysql_select_db($db_name,$this->connect)ordie("Yoncannotselectthetable:".mysql_error());
}
//导入sql文件
publicfunctionImport($url){
$this->sqlFile=file_get_contents($url);
if(!$this->sqlFile){
exit("打开文件错误");
}else{
$this->GetSqlArr();
if($this->Runsql()){
returntrue;
}
}
}
//获取sql语句数组
publicfunctionGetSqlArr(){
//去除注释
$str=$this->sqlFile;
$str=preg_replace('/--.*/i','',$str);
$str=preg_replace('//*.**/(;)?/i','',$str);
//去除空格创建数组
$str=explode("; ",$str);
foreach($stras$v){
$v=trim($v);
if(empty($v)){
continue;
}else{
$this->sqlArr[]=$v;
}
}
}
//执行sql文件
publicfunctionRunSql(){
foreach($this->sqlArras$k=>$v){
if(!mysql_query($v)){
exit("sql语句错误:第".$k."行".mysql_error());
}
}
returntrue;
}
}
//范例:
header("Content-type:text/html;charset=utf-8");
$sql=newReadSql("localhost","root","","log_db");
$rst=$sql->Import("./log_db.sql");
if($rst){
echo"Success!";
}
?>

❸ php创建缩略图问题

其实PHP创建缩略图就是在PHP在原图片的基础上创建一张新的图片的过程,而用PHP创建图像的过程一般分成四部:

第一步:创建一张画布(只要是画图都需要一张画布的)

第二步:在画布画东西(可以画各种图形,如长方形,直线,等等,也可以在画布上写字啥的,或者画其他的图形)

第三步:画完图之后,将图片输出,将图片输出到浏览器,在浏览器显示出来,或者保存为一张新 的图片(缩略图一般是保存为图片文件的)

第四步:因为创建画布时打开了文件流,所以要关闭资源,节省内存。(个人觉得你可以这样理解,打开了一画布,把它铺开了,画完了就把画布卷起来,收起来,不要占着铺的地方)


具体的代码如下:(这段代码来源于ThinkPHP的图像类)

<?php
classThumb{
/**
*@paramstring$image原图
*@paramstring$thumbname缩略图文件名
*@paramstring$type图像格式
*@paramstring$maxWidth宽度
*@paramstring$maxHeight高度
*/
staticcreate($img,$thumbname,$type='',$maxWidth=200,$maxHeight=50)
{
$info=getimagesize($img);//获取原图的图像信息(长、宽、格式等)
if($info!==false){
$srcWidth=$info['width'];
$srcHeight=$info['height'];
$type=empty($type)?$info['type']:$type;
$type=strtolower($type);
$interlace=$interlace?1:0;
unset($info);
$scale=min($maxWidth/$srcWidth,$maxHeight/$srcHeight);//计算缩放比例
if($scale>=1){
//超过原图大小不再缩略
$width=$srcWidth;
$height=$srcHeight;
}else{
//缩略图尺寸
$width=(int)($srcWidth*$scale);
$height=(int)($srcHeight*$scale);
}

//载入原图(在原图的基础上创建画布,为第一步)
$createFun='ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
if(!function_exists($createFun)){
returnfalse;
}
$srcImg=$createFun($image);

//第二步开始
//创建缩略图
if($type!='gif'&&function_exists('imagecreatetruecolor'))
$thumbImg=imagecreatetruecolor($width,$height);
else
$thumbImg=imagecreate($width,$height);
//png和gif的透明处理byluofei614
if('png'==$type){
imagealphablending($thumbImg,false);//取消默认的混色模式(为解决阴影为绿色的问题)
imagesavealpha($thumbImg,true);//设定保存完整的alpha通道信息(为解决阴影为绿色的问题)
}elseif('gif'==$type){
$trnprt_indx=imagecolortransparent($srcImg);
if($trnprt_indx>=0){
//itstransparent
$trnprt_color=imagecolorsforindex($srcImg,$trnprt_indx);
$trnprt_indx=imagecolorallocate($thumbImg,$trnprt_color['red'],$trnprt_color['green'],$trnprt_color['blue']);
imagefill($thumbImg,0,0,$trnprt_indx);
imagecolortransparent($thumbImg,$trnprt_indx);
}
}
//复制图片
if(function_exists("ImageCopyResampled"))
imageresampled($thumbImg,$srcImg,0,0,0,0,$width,$height,$srcWidth,$srcHeight);
else
imageresized($thumbImg,$srcImg,0,0,0,0,$width,$height,$srcWidth,$srcHeight);

//第三步:输出图像
//生成图片
$imageFun='image'.($type=='jpg'?'jpeg':$type);
$imageFun($thumbImg,$thumbname);

//第四步:关闭画布
imagedestroy($thumbImg);
imagedestroy($srcImg);
return$thumbname;
}
returnfalse;

}

}
?>

你使用的时候直接用:

requireThumb.class.php
$thumb=Thumb::create('s.jpg','thumb_s.jpg',100,50);

希望我的回答你能满意

热点内容
主机管理系统源码 发布:2025-05-25 15:12:39 浏览:511
数据库网页版 发布:2025-05-25 15:07:50 浏览:874
用c语言做个 发布:2025-05-25 14:59:53 浏览:959
大数据存储器 发布:2025-05-25 14:55:56 浏览:437
电脑配置怎么做 发布:2025-05-25 14:55:49 浏览:800
手机php编译器 发布:2025-05-25 14:53:31 浏览:106
libpcap编译 发布:2025-05-25 14:53:15 浏览:755
计算n的阶乘的c语言 发布:2025-05-25 14:48:32 浏览:853
印机和传真服务器属性怎么更改 发布:2025-05-25 14:25:03 浏览:454
数据库性能诊断 发布:2025-05-25 14:19:57 浏览:528