PHP画像処理のコード共有

WBOY
リリース: 2016-07-29 08:47:45
オリジナル
814 人が閲覧しました

現在、3 つの機能のみが実装されています: 1: 画像のスケーリング、2: 画像のトリミング、3: 画像のウォーターマークの追加
インスタンス化では、2 番目のパラメーターに異なる値を渡すことで、異なる機能が実現されます

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


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);
//ウォーターマークが読み込まれる場所を計算する関数を呼び出します
$posit $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 < $src_x+$dst_w || $this->height < $src_y+$dst_h){ //この判断は、画像外のインターセプト
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);
case 2://jpeg
$this- >imgtype=2;
$img=imagecreatefromjpeg($imgad);
ケース 3://png
$img=imagecreatefrompng($imgad);デフォルト:
return false;
}
return $img;
}
private function imagesize(){ //画像の幅を取得する
return imagesize(){ / /画像の高さを取得します
returnimagesy($this->image);
}
private function proimagesize(){ //比例的に拡大縮小された画像の幅と高さを計算します
if($this->value1 && ($ this->width < $this->height)) { // 一定スケーリングアルゴリズム
$this->value1=round(($this->value2/ $this->height)*$this- >幅);
}else{
$this->value2=round(($this->value1/ $this->height)
}
}
プライベート関数 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;
default:
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)
}
}

;
以上、PHP画像処理のコードシェアリングを内容も含めて紹介しましたが、PHPチュートリアルに興味のある友人の参考になれば幸いです。


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!