PHP画像処理コード共有_PHPチュートリアル
Jul 21, 2016 pm 03:21 PM
php
コード
共有
関数
追加
画像処理
写真
存在する
例
成し遂げる
透かし
現在のところ
親切
ズーム
作物
合格
現在、3 つの機能のみが実装されています: 1: 画像のスケーリング、2: 画像のトリミング、3: 画像のウォーターマークの追加
インスタンス化では、2 番目のパラメーターに異なる値を渡すことで、異なる機能が実現されます
コードをコピー コードは次のとおりです:
<?php
include "image.class.php";
$image=new image("2.png", 1, "300", "500", "5.png " ); //画像のズーム機能を使用する
$image=new image("2.png", 2, "0,0", "50,50", "5.png"); //画像のトリミング機能を使用する
$image=new image("2.png", 3, "1.png", "0", "5.png"); //画像透かし関数を使用します
$image->outimage(); ? >
コードは次のとおりです:
<?php
/*既知の問題: 1. 画像拡大縮小機能では、imagecreatetruecolor 関数を使用してキャンバスを作成し、透明処理アルゴリズムを使用しますが、PNG 形式の画像を透明にすることはできません。imagecreate 関数でキャンバスを作成するとこの問題は解決できますが、スケーリングされた画像の色数が少なすぎます
*
*
*type value:
* (1): ここでの画像スケーリング関数の使用を表します。時間、$value1 はズームされた画像の幅を表し、$value2 はズームされた画像の高さを表します
* (2): 画像トリミング機能の使用を表します。このとき、$value1 はトリミング開始点の座標を表します。 : 原点から始まり、その前に x が付きます。 軸の後ろには y 軸があり、中央に $value2 があり、これも切り抜きの幅と高さを表します。 "20, 20".
* (3): 画像のウォーターマーク機能の使用を表します。このとき、$value1 は画像内のウォーターマークの位置を表します。 10 個の値から選択できます。1 は左上、2 は左中央、3 は左と右、4 は左中央、5 は中央中央、6 は下中央、8 は中央下を表します。 , 9 は右下を表し、0 はランダムな位置を表します
*
*/
class image{
private $types; //使用される関数番号、1 は画像ズーム関数、2 は画像トリミング関数です 3. 画像透かし関数を追加します画像
private $imgtype;//画像のフォーマット
private $image; //画像リソース
private $width;//画像の幅
private $height;//画像の高さ
private $value1;渡されたさまざまな型の値に従って、$value1 はそれぞれ異なる値を表します
private $value2;//渡されたさまざまな型の値に従って、$value2 はそれぞれ異なる値を表します
private $endaddress;//アドレス出力後 +ファイル名
function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){
$this->types=$types;
$this->image= $this ->imagesources($imageaddress);
$this->width=$this->imagesizex();
$this->height=$this->imagesize(); ;value1 =$value1;
$this->value2=$value2;
}
function outimage(){ // 渡されたさまざまな型の値に応じてさまざまな関数を出力しますin
switch ($this->types){
case 1:
$this->scaling();
case 2:
$this->clipping();
$this ->imagewater();
break;
default:
return false;
}
}
private function imagewater(){ // 関数を使用して画像の長さと幅を取得しますウォーターマーク ファイル
$imagearrs= $this->getimagearr($this->value1);
//ウォーターマークが読み込まれる位置を計算する関数を呼び出します
$positionarr=$this->position($this- >value2, $imagearrs[0], $imagearrs[1]);
//ウォーターマークを追加
imagecopy($this->image, $this->imagesources($this->value1), $positionarr[ 0], $positionarr[1], 0 , 0, $imagearrs[0], $imagearrs[1]);
//出力メソッドを呼び出して保存します
$this->output($this->image) ;
}
private function Clipping(){ //画像切り抜き関数
//渡された値をそれぞれ変数に代入
list($src_x, $src_y)=explode(",", $this->value1) ;
list($dst_w, $dst_h)=explode (",", $this->value2);
if($this->width height return false}
//新しいキャンバスリソースを作成します
$newimg=imagecreatetruecolor($dst_w, $dst_h);
//Crop
imagecopyresampled($newimg, $this->image , 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h);
// 出力メソッドを呼び出して保存します
$this-> Output($newimg);
}
private function scaling( ){ //画像スケーリング関数
//比例スケーリングの幅と高さを取得します
$this->
//パラメータに従ってスケーリングします。そして、処理されたファイルを保存するために出力関数を呼び出します
$this-> ;output($this->imagescaling())
}
private function imagesources($imgad){ //画像タイプを取得し、画像リソースを開きます
$imagearray=$this->getimagearr($imgad);
switch ($imagearray[2]){
case 1://gif
$this->imgtype=1;
$img=imagecreatefromgif($imgad) );
ケース 2://jpeg
$this->imgtype=2;
$img=imagecreatefromjpeg($imgad); 3;
$img=imagecreatefrompng($imgad);
default :
return $img;
private function imagex(){ //画像の幅を取得する->image);
}
private function imagesizey(){ //画像の高さを取得します
return imagey($this->image);
}
private function proimagesize(){ //画像の幅と高さを計算します比例的にスケーリングされた画像
if($this->value1 && ($this->gt ;width < $this->height)) { //均等スケーリングアルゴリズム
$this->value1=round(($this- >値2/ $this->高さ)*$this->幅);
}else{
$this->value2=round(($this->value1/ $this->幅) * $this->高さ);
プライベート関数 imagescaling(){//画像スケーリング関数、処理された画像リソースを返します
$newimg=imagecreatetruecolor($this->value1, $this->value2);
$tran=imagecolortransparent($this-> ; image);//透明度アルゴリズムの処理
if($tran >= 0 && $tran < imagecolorstotal($this->image)){
$tranar=imagecolorsforindex($this->image, $tran) ;
$newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']);
imagefill($newimg, 0, 0, $newcolor); ($newimg, $newcolor);
}
imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this-> ; width, $this->height);
return $newimg;
}
private関数output($image){//出力画像
switch($this->imgtype){
case 1:
imagegif($image) , $this->endaddress);
case 2:
imagejpeg($image, $this->endaddress);
imagepng($image, $this->endaddress); );
break;
デフォルト:
return false;
}
private function getimagearr($imagesou){//
return getimagesize($imagesou);
private functionposition($num, $width, $height){//渡された数値に基づいて位置の座標を返します。 $width と $height はそれぞれ、挿入された画像の幅と高さを表します
switch($num){
case 1:
$位置arr[0] =0;
$位置arr[1]=0;
ケース2:
$位置arr[0]=($this->幅-$width)/2; =0;
ケース 3:
$positionarr[0]=$this->幅-$width;
ケース 4:
$positionarr; 0;
$ 位置arr[1]=($this->高さ-$高さ)/2;
ケース 5:
$位置arr[0]=($this->幅-$幅)/2 ;
$positionarr [1]=($this->高さ-$height)/2;
ケース 6:
$positionarr[0]=$this->幅-$width; 1]=($this->height-$height)/2;
ケース 7:
$positionarr[0]=0;
break;
ケース 8:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=$this->height-$height; 9:
$positionarr[0]=$this->width-$width;
$positionarr[1]=$this->height-$height;
case 0:
$positionarr[0]= rand(0, $this->width-$width);
$positionarr[1]=rand(0, $this->height-$height);
return $positionarr; function __destruct() {
imagedestroy($this->image)
}
}
;
http://www.bkjia.com/PHPjc/324918.html
www.bkjia.com
本当
http://www.bkjia.com/PHPjc/324918.html
技術記事
現在、3 つの関数のみが実装されています: 1: 画像のスケーリング、2: 画像のトリミング、3: 画像のウォーターマークの追加。インスタンス化では、異なる関数の複製を実現するために 2 番目のパラメーターに異なる値が渡されます。
<?php
/*既知の問題: 1. 画像拡大縮小機能では、imagecreatetruecolor 関数を使用してキャンバスを作成し、透明処理アルゴリズムを使用しますが、PNG 形式の画像を透明にすることはできません。imagecreate 関数でキャンバスを作成するとこの問題は解決できますが、スケーリングされた画像の色数が少なすぎます
*
*
*type value:
* (1): ここでの画像スケーリング関数の使用を表します。時間、$value1 はズームされた画像の幅を表し、$value2 はズームされた画像の高さを表します
* (2): 画像トリミング機能の使用を表します。このとき、$value1 はトリミング開始点の座標を表します。 : 原点から始まり、その前に x が付きます。 軸の後ろには y 軸があり、中央に $value2 があり、これも切り抜きの幅と高さを表します。 "20, 20".
* (3): 画像のウォーターマーク機能の使用を表します。このとき、$value1 は画像内のウォーターマークの位置を表します。 10 個の値から選択できます。1 は左上、2 は左中央、3 は左と右、4 は左中央、5 は中央中央、6 は下中央、8 は中央下を表します。 , 9 は右下を表し、0 はランダムな位置を表します
*
*/
class image{
private $types; //使用される関数番号、1 は画像ズーム関数、2 は画像トリミング関数です 3. 画像透かし関数を追加します画像
private $imgtype;//画像のフォーマット
private $image; //画像リソース
private $width;//画像の幅
private $height;//画像の高さ
private $value1;渡されたさまざまな型の値に従って、$value1 はそれぞれ異なる値を表します
private $value2;//渡されたさまざまな型の値に従って、$value2 はそれぞれ異なる値を表します
private $endaddress;//アドレス出力後 +ファイル名
function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){
$this->types=$types;
$this->image= $this ->imagesources($imageaddress);
$this->width=$this->imagesizex();
$this->height=$this->imagesize(); ;value1 =$value1;
$this->value2=$value2;
}
function outimage(){ // 渡されたさまざまな型の値に応じてさまざまな関数を出力しますin
switch ($this->types){
case 1:
$this->scaling();
case 2:
$this->clipping();
$this ->imagewater();
break;
default:
return false;
}
}
private function imagewater(){ // 関数を使用して画像の長さと幅を取得しますウォーターマーク ファイル
$imagearrs= $this->getimagearr($this->value1);
//ウォーターマークが読み込まれる位置を計算する関数を呼び出します
$positionarr=$this->position($this- >value2, $imagearrs[0], $imagearrs[1]);
//ウォーターマークを追加
imagecopy($this->image, $this->imagesources($this->value1), $positionarr[ 0], $positionarr[1], 0 , 0, $imagearrs[0], $imagearrs[1]);
//出力メソッドを呼び出して保存します
$this->output($this->image) ;
}
private function Clipping(){ //画像切り抜き関数
//渡された値をそれぞれ変数に代入
list($src_x, $src_y)=explode(",", $this->value1) ;
list($dst_w, $dst_h)=explode (",", $this->value2);
if($this->width height return false}
//新しいキャンバスリソースを作成します
$newimg=imagecreatetruecolor($dst_w, $dst_h);
//Crop
imagecopyresampled($newimg, $this->image , 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h);
// 出力メソッドを呼び出して保存します
$this-> Output($newimg);
}
private function scaling( ){ //画像スケーリング関数
//比例スケーリングの幅と高さを取得します
$this->
//パラメータに従ってスケーリングします。そして、処理されたファイルを保存するために出力関数を呼び出します
$this-> ;output($this->imagescaling())
}
private function imagesources($imgad){ //画像タイプを取得し、画像リソースを開きます
$imagearray=$this->getimagearr($imgad);
switch ($imagearray[2]){
case 1://gif
$this->imgtype=1;
$img=imagecreatefromgif($imgad) );
ケース 2://jpeg
$this->imgtype=2;
$img=imagecreatefromjpeg($imgad); 3;
$img=imagecreatefrompng($imgad);
default :
return $img;
private function imagex(){ //画像の幅を取得する->image);
}
private function imagesizey(){ //画像の高さを取得します
return imagey($this->image);
}
private function proimagesize(){ //画像の幅と高さを計算します比例的にスケーリングされた画像
if($this->value1 && ($this->gt ;width < $this->height)) { //均等スケーリングアルゴリズム
$this->value1=round(($this- >値2/ $this->高さ)*$this->幅);
}else{
$this->value2=round(($this->value1/ $this->幅) * $this->高さ);
プライベート関数 imagescaling(){//画像スケーリング関数、処理された画像リソースを返します
$newimg=imagecreatetruecolor($this->value1, $this->value2);
$tran=imagecolortransparent($this-> ; image);//透明度アルゴリズムの処理
if($tran >= 0 && $tran < imagecolorstotal($this->image)){
$tranar=imagecolorsforindex($this->image, $tran) ;
$newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']);
imagefill($newimg, 0, 0, $newcolor); ($newimg, $newcolor);
}
imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this-> ; width, $this->height);
return $newimg;
}
private関数output($image){//出力画像
switch($this->imgtype){
case 1:
imagegif($image) , $this->endaddress);
case 2:
imagejpeg($image, $this->endaddress);
imagepng($image, $this->endaddress); );
break;
デフォルト:
return false;
}
private function getimagearr($imagesou){//
return getimagesize($imagesou);
private functionposition($num, $width, $height){//渡された数値に基づいて位置の座標を返します。 $width と $height はそれぞれ、挿入された画像の幅と高さを表します
switch($num){
case 1:
$位置arr[0] =0;
$位置arr[1]=0;
ケース2:
$位置arr[0]=($this->幅-$width)/2; =0;
ケース 3:
$positionarr[0]=$this->幅-$width;
ケース 4:
$positionarr; 0;
$ 位置arr[1]=($this->高さ-$高さ)/2;
ケース 5:
$位置arr[0]=($this->幅-$幅)/2 ;
$positionarr [1]=($this->高さ-$height)/2;
ケース 6:
$positionarr[0]=$this->幅-$width; 1]=($this->height-$height)/2;
ケース 7:
$positionarr[0]=0;
break;
ケース 8:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=$this->height-$height; 9:
$positionarr[0]=$this->width-$width;
$positionarr[1]=$this->height-$height;
case 0:
$positionarr[0]= rand(0, $this->width-$width);
$positionarr[1]=rand(0, $this->height-$height);
return $positionarr; function __destruct() {
imagedestroy($this->image)
}
}
;
http://www.bkjia.com/PHPjc/324918.html
www.bkjia.com
本当
http://www.bkjia.com/PHPjc/324918.html
技術記事
現在、3 つの関数のみが実装されています: 1: 画像のスケーリング、2: 画像のトリミング、3: 画像のウォーターマークの追加。インスタンス化では、異なる関数の複製を実現するために 2 番目のパラメーターに異なる値が渡されます。
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

人気の記事
レポ:チームメイトを復活させる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
スプリットフィクションを打ち負かすのにどれくらい時間がかかりますか?
3週間前
By DDD
ハローキティアイランドアドベンチャー:巨大な種を手に入れる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.説明されたエネルギー結晶と彼らが何をするか(黄色のクリスタル)
1週間前
By 尊渡假赌尊渡假赌尊渡假赌

人気の記事
レポ:チームメイトを復活させる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
スプリットフィクションを打ち負かすのにどれくらい時間がかかりますか?
3週間前
By DDD
ハローキティアイランドアドベンチャー:巨大な種を手に入れる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.説明されたエネルギー結晶と彼らが何をするか(黄色のクリスタル)
1週間前
By 尊渡假赌尊渡假赌尊渡假赌

ホットな記事タグ

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック
Gmailメールのログイン入り口はどこですか?
7287
9


Java チュートリアル
1622
14


CakePHP チュートリアル
1342
46


Laravel チュートリアル
1259
25


PHP チュートリアル
1206
29



Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法
