當前位置:首頁 » 編程語言 » 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);

希望我的回答你能滿意

熱點內容
java二叉堆 發布:2025-05-25 15:24:59 瀏覽:759
主機管理系統源碼 發布:2025-05-25 15:12:39 瀏覽:512
資料庫網頁版 發布:2025-05-25 15:07:50 瀏覽:875
用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