當前位置:首頁 » 編程語言 » phppng透明

phppng透明

發布時間: 2025-07-19 22:34:17

『壹』 php 生成png 圖片 字體本身是斜體字 生成圖片 缺一塊

這個其實也簡單,你這個之所以生成這樣,是因為你生成的字體是垂直的,你現在的字體是傾斜的,所以你要給他一個斜度,imagetftext($im,20,$傾斜,10,20,$black,$font.$text);調整好角度以後問題就解決了

『貳』 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);

希望我的回答你能滿意

『叄』 PHP圖片生成

給你一個php 圖像處理類,完全能實現你的功能,你自己研究一下吧

<?php
class image
{
var $w_pct = 50; //透明度
var $w_quality = 80; //質量
var $w_minwidth = 300; //最小寬
var $w_minheight = 300; //最小高
var $thumb_enable; //是否生成縮略圖
var $watermark_enable; //是否生水印
var $interlace = 0; //圖像是否為隔行掃描的
var $fontfile; //字體文件
var $w_img ; //默認水印圖

function __construct()
{
global $SITE_CONFING;
$this->thumb_enable = $SITE_CONFING['thumb_enable'];
$this->watermark_enable = $SITE_CONFING['watermark_enable'];
$this->set($SITE_CONFING['watermark_minwidth'], $SITE_CONFING['watermark_minheight'], $SITE_CONFING['watermark_quality'], $SITE_CONFING['watermark_pct'], $SITE_CONFING['watermark_fontfile'],$SITE_CONFING['watermark_img']);
}

function image()
{
$this->__construct();
}

function set($w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100,$fontfile,$w_img)
{
$this->w_minwidth = $w_minwidth;
$this->w_minheight = $w_minheight;
$this->w_quality = $w_quality;
$this->w_pct = $w_pct;
$this->fontfile = $fontfile;
$this->w_img = $w_img;
}

function info($img)
{
$imageinfo = getimagesize($img); //返回圖像信息數組 0=>寬的像素 1=>高的像素 2=>是圖像類型的標記 3 =>是文本字元串,內容為「height="yyy" width="xxx"」,
if($imageinfo === false) return false;
$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //獲取圖像文件類型 $imageinfo[2]是圖像類型的標記
$imagesize = filesize($img); //圖像大小
$info = array(
'width'=>$imageinfo[0],
'height'=>$imageinfo[1],
'type'=>$imagetype,
'size'=>$imagesize,
'mime'=>$imageinfo['mime']
);
return $info;
}

function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 50, $suffix='_thumb', $autocut = 0)
{
if(!$this->thumb_enable || !$this->check($image)) return false;
$info = $this->info($image); //獲取圖片信息
if($info === false) return false;
$srcwidth = $info['width']; //源圖寬
$srcheight = $info['height']; //源圖高
$pathinfo = pathinfo($image);
$type = $pathinfo['extension']; //取得擴展名
if(!$type) $type = $info['type']; //如果沒有取到,用$info['type']
$type = strtolower($type);
unset($info);
$scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //獲取縮略比例
//獲取按照源圖的比列
$createwidth = $width = (int)($srcwidth*$scale); //取得縮略寬
$createheight = $height = (int)($srcheight*$scale); //取得縮略高
$psrc_x = $psrc_y = 0;
if($autocut) //按照縮略圖的比例來獲取
{
if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) //如果縮略圖按比列比源圖窄的話
{
$width = $maxheight/$height*$width; //寬按照相應比例做處理
$height = $maxheight; //高不變
}
elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果縮略圖按比列比源圖寬的話
{
$height = $maxwidth/$width*$height;
$width = $maxwidth;
}
$createwidth = $maxwidth;
$createheight = $maxheight;
}
$createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的圖像處理函數
$srcimg = $createfun($image); //新建圖像
if($type != 'gif' && function_exists('imagecreatetruecolor'))
$thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像
else
$thumbimg = imagecreate($width, $height); //新建一個基於調色板的圖像

if(function_exists('imageresampled')) //重采樣拷貝部分圖像並調整大小,真彩
//imageresampled(新圖,源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)
imageresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
else //拷貝部分圖像並調整大小,調色板
imageresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
if($type=='gif' || $type=='png')
{
//imagecolorallocate 為一幅圖像分配顏色
$background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 給基於調色板的圖像填充背景色, 指派一個綠色
// imagecolortransparent 將某個顏色定義為透明色
imagecolortransparent($thumbimg, $background_color); // 設置為透明色,若注釋掉該行則輸出綠色的圖
}
// imageinterlace 激活或禁止隔行掃描
if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
$imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
//imagejpeg imagegif imagepng
if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //獲取文件名
//aaa.gif aaa_thumb.gif
$imagefun($thumbimg, $filename); //新建圖像
imagedestroy($thumbimg); //銷毀縮略圖
imagedestroy($srcimg); //銷毀源圖
return $filename;
}
//watermark(源圖,生成文件,生成位置,水印文件,水印文本,背景色)
function watermark($source, $target = '', $w_pos = 0, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc')
{
if(!$this->watermark_enable || !$this->check($source)) return false;
if(!$target) $target = $source;
if ($w_img == '' && $w_text == '')
$w_img = $this->w_img;
$source_info = getimagesize($source);
$source_w = $source_info[0]; //獲取寬
$source_h = $source_info[1]; //獲取高
if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //寬和高達不到要求直接返回
switch($source_info[2]) //新建圖片
{
case 1 :
$source_img = imagecreatefromgif($source);
break;
case 2 :
$source_img = imagecreatefromjpeg($source);
break;
case 3 :
$source_img = imagecreatefrompng($source);
break;
default :
return false;
}
if(!empty($w_img) && file_exists($w_img)) //水印文件
{
$ifwaterimage = 1; //是否水印圖
$water_info = getimagesize($w_img); //水印信息
$width = $water_info[0];
$height = $water_info[1];
switch($water_info[2])
{
case 1 :
$water_img = imagecreatefromgif($w_img);
break;
case 2 :
$water_img = imagecreatefromjpeg($w_img);
break;
case 3 :
$water_img = imagecreatefrompng($w_img);
break;
default :
return;
}
}
else
{
$ifwaterimage = 0;
//imagettfbbox 本函數計算並返回一個包圍著 TrueType 文本范圍的虛擬方框的像素大小。
//imagettfbbox ( 字體大小, 字體角度, 字體文件,文件 )
$temp = imagettfbbox(ceil($w_font*1.2), 0, $this->fontfile, $w_text);//取得使用 truetype 字體的文本的范圍
$width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置
$height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置
unset($temp);
}
switch($w_pos)
{
case 0: //隨機位置
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
case 1: //左上角
$wx = 5;
$wy = 5;
break;
case 2: //上面中間位置
$wx = ($source_w - $width) / 2;
$wy = 0;
break;
case 3: //右上角
$wx = $source_w - $width;
$wy = 0;
break;
case 4: //左面中間位置
$wx = 0;
$wy = ($source_h - $height) / 2;
break;
case 5: //中間位置
$wx = ($source_w - $width) / 2;
$wy = ($source_h - $height) / 2;
break;
case 6: //底部中間位置
$wx = ($source_w - $width) / 2;
$wy = $source_h - $height;
break;
case 7: //左下角
$wx = 0;
$wy = $source_h - $height;
break;
case 8: //右面中間位置
$wx = $source_w - $width;
$wy = ($source_h - $height) /2;
break;
case 9: //右下角
$wx = $source_w - $width;
$wy = $source_h - $height ;
break;
default: //隨機
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
}
if($ifwaterimage) //如果有水印圖
{
//imagemerge 拷貝並合並圖像的一部分
//參數(源圖,水印圖,拷貝到源圖x位置,拷貝到源圖y位置,從水印圖x位置,從水印圖y位置,高,寬,透明度)
imagemerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
}
else
{
if(!empty($w_color) && (strlen($w_color)==7))
{
$r = hexdec(substr($w_color,1,2)); //獲取紅色
$g = hexdec(substr($w_color,3,2)); //獲取綠色
$b = hexdec(substr($w_color,5)); //獲取藍色
}
else
{
return;
}
//imagecolorallocate 基於調色板的圖像填充背景色
//imagestring 水平地畫一行字元串
//imagestring(源圖,字體大小,位置X,位置Y,文字,顏色)
//參數($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
//imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
}
//輸出到文件或者瀏覽器
switch($source_info[2])
{
case 1 :
imagegif($source_img, $target); //以 GIF 格式將圖像輸出到瀏覽器或文件
break;
case 2 :
imagejpeg($source_img, $target, $this->w_quality); //以 JPEG 格式將圖像輸出到瀏覽器或文件
break;
case 3 :
imagepng($source_img, $target); //以 PNG 格式將圖像輸出到瀏覽器或文件
break;
default :
return;
}
if(isset($water_info))
{
unset($water_info); //銷毀
}
if(isset($water_img))
{
imagedestroy($water_img); //銷毀
}
unset($source_info);
imagedestroy($source_img);
return true;
}
//gd庫必須存在,後綴為jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
function check($image)
{
return extension_loaded('gd') &&
preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) &&
file_exists($image) &&
function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
//imagecreatefromjpeg
//imagecreatefromgif
//imagecreatefrompng
}
}

/**
縮略圖
1.新建一個圖像資源 通過 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
2.imageresampled 拷貝圖像,並調整大小

水印:圖片水印,文字水印
1. 創建圖像
2.加水印
圖片水印:imagemerge 把2張圖合並在一起
文字水印:imagettftext 向圖像寫入文字

*/
?>

『肆』 如何在PHP中開啟GD庫支持詳解

開啟GD庫是php中一個重要的參數,常用來處理圖像,像圖片的任何處理都需要GD庫的支持,下面我來介紹各種系統中GD庫的開啟方法與命令,大家可參考。

Windows下開啟PHP的GD庫支持
找到php.ini,打開內容,找到:
;extension=php_gd2.dll
把最前面的分號「;」去掉,再保存即可,如果本來就沒有分號,那就是已經開啟了。
linux下開啟PHP的GD庫支持
#開啟GD庫支持有以下幾種方法
##檢測GD庫是否安裝命令
php5 -m | grep -i gd
或者
php -i | grep -i --color gd
##如未安裝GD庫,則為伺服器安裝,方法如下
### 如果是源碼安裝,則加入參數
--with-gd
### 如果是debian系的linux系統,用apt-get安裝,如下
apt-get install php5-gd
### 如果是CentOS系的系統,用yum安裝,如下
yum install php-gd
### 如果是suse系的linux系統,用yast安裝,如下
yast -i php5_gd
### 如果嫌這個世界不夠蛋疼呢,可以在原先編譯PHP不支持GD的情況下附加
先下zlib源碼,libpng源碼,gd源碼
解壓後到源碼目錄
zlib目錄
./configure --prefix=/usr/local/zlib
make ; make install
make clean
libpng目錄
cp scripts/makefile.linux ./makefile
./configure --prefix=/usr/local/libpng
make ; make install
make clean
gd目錄
./configure --prefix=/usr/local/libgd --with-png=/usr/local/libpng
make ; make install
make clean
最後在php.ini中,搜到[gd]後,在下面加一行
extension=/usr/local/libgdgd.so
然後重啟php服務,如果不行,試試reboot
好了,不過最後提醒一下,要知道這個世界很多意外的,源碼安裝,只添加gd庫這一個情況下,PHP版本和庫的版本各異
,所以:
- 不保證這么付出了這么多後有回報
- 不保證能夠成功載入gd.so
- 不保證不懷孕
所以如果是源碼安裝,最好還是在編譯PHP的時候加參數--with-gd

GD庫函數
GetImageSize
作用:取得圖片的大小[即長與寬] 用法:array GetImageSize(string filename, array [imageinfo]);
ImageArc
作用:畫弧線 用法:int ImageArc(int im, int cx, int cy, int w, int h, int s, int e, int col);
ImageChar
作用:寫出橫向字元 用法:int ImageChar(int im, int font, int x, int y, string c, int col);
ImageCharUp
作用:寫出豎式字元 用法:int ImageCharup(int im, int font, int x, int y, string c, int col);
ImageColorAllocate
作用:匹配顏色 用法:int ImageColorAllocate(int im, int red, int green, int blue);

ImageColorTransparent
作用:指定透明背景色 用法:int ImageColorTransparent(int im, int [col]);

ImageCopyResized
作用:復制新圖並調整大小 用法:int ImageCopyResized(int dst_im, int src_im, int dstX, int dstY, int srcX,
int srcY, int dstW, int dstH, int srcW, int srcH);

ImageCreate
作用:建立新圖 用法:int ImageCreate(int x_size, int y_size);

ImageDashedLine
作用:繪虛線 用法:int ImageDashedLine(int im, int x1, int y1, int x2, int y2, int col);
ImageDestroy 作用:結束圖形 用法解釋:int ImageDestroy(int im);

ImageFill
作用:圖形著色 用法:int ImageFill(int im, int x, int y, int col);
ImageFilledPolygon作用:多邊形區域著色 用法:int ImageFilledPolygon(int im, array points, int num_points,
int col);

ImageFilledRectangle作用:矩形區域著色 用法:int ImageFilledRectangle(int im, int x1, int y1, int x2, int
y2, int col);

ImageFillToBorder作用:指定顏色區域內著色 用法:int ImageFillToBorder(int im, int x, int y, int border,
int col);

ImageFontHeight
作用:取得字型的高度 用法:int ImageFontHeight(int font);

ImageFontWidth作用:取得字型的寬度 用法:int ImageFontWidth(int font);

ImageInterlace作用:使用交錯式顯示與否 用法:int ImageInterlace(int im, int [interlace]);

ImageLine作用:繪實線 用法:int ImageLine(int im, int x1, int y1, int x2, int y2, int col);

ImageLoadFont作用:載入點陣字型 用法:int ImageLoadFont(string file);

ImagePolygon作用:繪多邊形 用法:int ImagePolygon(int im, array points, int num_points, int col);

ImageRectangle作用:繪矩形 用法:int ImageRectangle(int im, int x1, int y1, int x2, int y2, int col);

ImageSetPixel作用:繪點 用法:int ImageSetPixel(int im, int x, int y, int col); ImageString 作用:繪橫式
字元串 用法:int ImageString(int im, int font, int x, int y, string s, int col);

ImageStringUp作用:繪直式字元串 用法:int ImageStringUp(int im, int font, int x, int y, string s, int
col);

ImageSX
作用:取得圖片的寬度 用法:int ImageSX(int im);

ImageSY
作用:取得圖片的高度 用法:int ImageSY(int im);

ImageTTFBBox
作用:計算 TTF 文字所佔區域 用法:array ImageTTFBBox(int size, int angle, string fontfile, string text);

ImageTTFText作用:寫 TTF 文字到圖中 用法:array ImageTTFText(int im, int size, int angle, int x, int y,
int col, string fontfile, string text); ImageColorAt 作用:取得圖中指定點顏色的索引值 用法:int
ImageColorAt(int im, int x, int y);

ImageColorClosest
作用:計算色表中與指定顏色最接近者 用法:int ImageColorClosest(int im, int red, int green, int blue);

ImageColorExact
作用:計算色表上指定顏色索引值 用法:int ImageColorExact(int im, int red, int green, int blue);

ImageColorResolve
作用:計算色表上指定或最接近顏色的索引值 用法:int ImageColorResolve(int im, int red, int green, int
blue);
ImageColorSet
作用:設定色表上指定索引的顏色 用法:boolean ImageColorSet(int im, int index, int red, int green, int
blue); ImageColorsForIndex 作用:取得色表上指定索引的顏色 用法:array ImageColorsForIndex(int im, int
index);

ImageColorsTotal
作用:計算圖的顏色數 用法:int ImageColorsTotal(int im); ImagePSLoadFont 作用:載入 PostScript 字型 用法
:int ImagePSLoadFont(string filename);

ImagePSFreeFont
作用:卸下 PostScript 字型 用法:void ImagePSFreeFont(int fontindex);

ImagePSEncodeFont
作用:PostScript 字型轉成向量字 用法:int ImagePSEncodeFont(string encodingfile);

ImagePSText作用:寫 PostScript 文字到圖中 用法:array ImagePSText(int image, string text, int font, int
size, int foreground, int background, int x, int y, int space, int tightness, float angle, int
antialias_steps);

ImagePSBBox作用:計算 PostScript 文字所佔區域 用法: array ImagePSBBox(string text, int font, int size,
int space, int width, float angle);

ImageCreateFromPNG作用:取出 PNG 圖型 用法:int ImageCreateFromPng(string filename);

ImagePNG作用:建立 PNG 圖型 用法:int ImagePng(int im, string [filename]);

ImageCreateFromGIF作用:取出 GIF 圖型 用法:int ImageCreateFromGif(string filename);

ImageGIF作用:建立 GIF 圖型 用法:int ImageGif(int im, string [filename]);

熱點內容
求圓周率的演算法 發布:2025-07-20 02:31:46 瀏覽:124
南京壓縮機維修 發布:2025-07-20 02:31:45 瀏覽:70
編譯pve內核 發布:2025-07-20 02:30:10 瀏覽:305
linux計算機 發布:2025-07-20 02:29:30 瀏覽:675
python編程從零基礎到項目實戰 發布:2025-07-20 02:28:11 瀏覽:956
我的世界手機版伺服器名稱和ip 發布:2025-07-20 02:18:39 瀏覽:241
shell腳本備份資料庫 發布:2025-07-20 02:12:55 瀏覽:826
手機照片那個文件夾 發布:2025-07-20 02:06:07 瀏覽:153
聖安地列斯哪個版本支持安卓10 發布:2025-07-20 01:56:04 瀏覽:26
rman全備份腳本 發布:2025-07-20 01:43:12 瀏覽:832