ホームページ  >  記事  >  バックエンド開発  >  PHPの各種画像処理関数コードまとめ_PHPチュートリアル

PHPの各種画像処理関数コードまとめ_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:00:38791ブラウズ

1. 画像リソースを作成します

imagecreatetruecolor(width,height);
imagecreatefromgif(画像名);
imagecreatefrompng(画像名);
imagecreatefromjpeg(画像名) さまざまな画像を描画します imagegif(画像リソース、保存パス);
imagepng()
imagejpeg( );

2. 画像属性を取得します

imagesx(res//width
imagesy(res//height
getimagesize(file path)
4 つのセルを含む配列を返します。インデックス 0 には画像の幅のピクセル値が含まれ、インデックス 1 には画像の高さのピクセル値が含まれます。インデックス 2 画像タイプのタグです: 1 = GIF、2 = JPG、3 = PNG、4 = SWF、5 = PSD、6 = BMP、7 = TIFF (インテル バイト オーダー)、8 = TIFF (モトローラ バイト オーダー) )、9 = JPC、10 = JP2、11 = JPX、12 = JB2、13 = SWC、14 = IFF、15 = WBMP、16 = XBM これらのタグは、PHP 4.3.0 の新しい IMAGETYPE 定数に対応します。はテキスト文字列で、内容は「height="yyy" width="xxx"」で、IMG タグに直接使用できます
画像リソースの破棄
imagedestroy(image resource);

3. 透明処理

PNGとjpegの透明色は正常ですが、gifだけが異常です

imagecolortransparent(resource image [,int color])//色を透明に設定します
imagecolorstotal()
imagecolorforindex();

4. 写真のトリミング

imagecopyresize()
imagecopyresampled();

5. 透かし(テキスト、画像)を追加します

文字列エンコード変換文字列 iconv ( string $in_charset , string $out_charset , string $str )

6. 画像の回転

imagerotate();//指定した角度で​​画像を反転します

7. 写真の反転

X 軸に沿って反転

Y 軸に沿って反転

8. 研ぐ

index() のイメージカラー

イメージカラーat()

写真にグラフィックを描画 $img=imagecreatefromgif("./images/map.gif");

コードをコピー コードは次のとおりです:

$red= imagecolorallocate($img, 255, 0, 0) ;
imageline($img, 0, 0, 100, 100, $red);
imageellipse($img, 200, 100, 100, 100, $red);
imagegif($img, "./images/マップ2.gif");
imagedestroy($img);

写真の通常のスケーリング

コードをコピーする コードは次のとおりです:

$filename="./images/hee.jpg";
$per=0.3;
list($width, $height) =getimagesize($ ファイル名);
$n_w=$width*$per;
$n_h=$width*$per;

$new=imagecreatetruecolor($n_w, $n_h);

$img=imagecreatefromjpeg($filename);
//画像の一部をコピーして調整します

imagecopyresize($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
//画像出力新しい画像、名前を付けて保存

imagejpeg($new, "./images/hee2.jpg");

imagedestroy($new);
imagedestroy($img);


画像は比例的に拡大縮小され、透明色は処理されません
コードをコピーします コードは次のとおりです:

function thumn( $background, $width, $ height, $newfile) {
list($s_w, $s_h)=getimagesize($background);// 元の画像の高さと幅を取得します

if ($width && ($s_w < $s_h)) {
$width = ($height / $s_h) * $s_w;
} else {
$height = ($width / $s_w) * $s_h;
}

$new=imagecreatetruecolor($width, $height);

$img=imagecreatefromjpeg($background);

imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);

imagejpeg($new, $newfile);

imagedestroy($new);
imagedestroy($img);
}

thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg");


gif 透明色処理
コードをコピー コードは以下の通りです:

function thumn($background, $width, $height, $newfile) {
list($s_w, $s_h)=getimagesize($background);

if ($width && ($s_w < $s_h)) {
$width = ($height / $s_h) * $s_w;
} else {
$height = ($width / $s_w) * $s_h;
}

$new=imagecreatetruecolor($width, $height);

$img=imagecreatefromgif($background);

$otsc=imagecolortransparent($img);
if($otsc >=0 && $otst < imagecolorstotal($img)){// インデックス色判断
$tran=imagecolorsforindex($img, $otsc);/ /インデックス颜色值

$newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]);

imagefill($new, 0, 0, $newt);

imagecolortransparent($new, $newt);
}

imagecopyresize($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);

imagegif($new, $newfile);

imagedestroy($new);
imagedestroy($img);
}

thumn("images/map.gif", 200, 200, "./images/map3.gif");


图片裁断
复制代码代码如下:

function Cut($background 、$cut_x、$cut_y、$cut_width、$cut_height、$location){

$back=imagecreatefromjpeg($background);

$new=imagecreatetruecolor($cut_width, $cut_height);

imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height,$cut_width,$cut_height);

imagejpeg($new, $location);

imagedestroy($new);
imagedestroy($back);
}

cut("./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg");


图片加水印

文字水印

复制代码代码如下:

function mark_text($background, $text, $x, $y){
$back=imagecreatefromjpeg($background);

$color=imagecolorallocate($back, 0, 255, 0);

imagettftext($back, 20, 0, $x, $y, $color, "simkai.ttf", $text);

imagejpeg($back, "./images/hee7.jpg");

imagedestroy($back);
}

mark_text("./images/hee.jpg", "细说PHP", 150, 250);

//图片水印
function mark_pic($background, $waterpic, $x, $y){
$back=imagecreatefromjpeg($background);
$water=imagecreatefromgif($waterpic);
$w_w=imagesx($water );
$w_h=imagesy($water);
imagecopy($back, $water, $x, $y, 0, 0, $w_w, $w_h);
imagejpeg($back,"./images/hee8 .jpg");
imagedestroy($back);
imagedestroy($water);
}
mark_pic("./images/hee.jpg", "./images/gaolf.gif", 50, 200);

写真回転

复制代码代码如下:

$back=imagecreatefromjpeg("./images/hee.jpg");
$new=imagerotate($back, 45, 0);
imagejpeg($new, "./images/hee9.jpg");

图片水平翻转垂直翻转
复制幣代码如下:

functionturn_y($background, $newfile){
$back=imagecreatefromjpeg($背景);

$width=imagesx($back);
$height=imagesy($back);

$new=imagecreatetruecolor($width, $height);

for($x=0; $x imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
}

imagejpeg($new, $newfile);

imagedestroy($back);
imagedestroy($new);
}

関数turn_x($background, $newfile){
$back=imagecreatefromjpeg($background);

$width=imagesx($back);
$height=imagesy($back);

$new=imagecreatetruecolor($width, $height);

for($y=0; $y < $height; $y++){
imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1);
}

imagejpeg($new, $newfile);

imagedestroy($back);
imagedestroy($new);
}

turn_y("./images/hee.jpg", "./images/hee11.jpg");
turn_x("./images/hee.jpg", "./images/hee12.jpg");


写真锐化

复制代码代码如下:

関数シャープ($background, $degree, $save){
$back=imagecreatefromjpeg($background);

$b_x=imagesx($back);
$b_y=imagesy($back);

$dst=imagecreatefromjpeg($background);
for($i=0; $i for($j=0; $j $b_clr1= imagecolorsforindex($back, imagecolorat($back, $i-1, $j-1));\前のピクセルカラー配列
$b_clr2=imagecolorsforindex($back, imagecolorat($back, $i, $j));現在の色の配列を取得します

$r=intval($b_clr2["red"]+$degree*($b_clr2["red"]-$b_clr1["red"]));\深化
$g=intval($b_clr2["green" ]+$degree*($b_clr2["green"]-$b_clr1["green"]));
$b=intval($b_clr2["blue"]+$degree*($b_clr2["blue"]- $b_clr1["青"]));

$r=min(255, max($r, 0));//r の範囲を 0 ~ 255 に制限します
$g=min(255, max($g, 0));
$b=最小 (255, 最大 ($b, 0));

if(($d_clr=imagecolorexact($dst, $r, $g, $b))==-1){//1 に等しいが色の範囲外です
$d_clr=Imagecolorallocate($dst, $r) , $g , $b);//色を作成します
}

imagesetpixel($dst, $i, $j, $d_clr);
}

}
imagejpeg($dst, $save);
imagedestroy($back);
imagedestroy($dst);
}

sharp("./images/hee.jpg", 20, "./images/hee13.jpg");

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/328053.html技術記事 1. 画像リソースを作成します imagecreatetruecolor(width,height); imagecreatefrompng(画像名); さまざまな画像を描画します...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。