画像を処理するための PHP クラスの実装コード
Jun 13, 2016 pm 12:20 PM
コードをコピー コードは次のとおりです:
<?php
/**
* 著者:yagas
* メール:yagas60@21cn.com
*/
クラスイメージ
{
/**クラス保護された変数*/
protected $th_width = 100;
protected $th_height = 50;
protected $quality = 85; //画質
protected $transparent = 50; //透かしの透明度
protected $background = "255,255,255";
/**
* サムネイルファイルを生成
* @param $src オリジナル画像ファイル
* @param $dst ターゲットファイル
*/
パブリック関数thumb($src, $dst=null, $output=true)
{
$thumb = array($this->th_width, $this->th_height);
$this->scale($src, $thumb, $dst, $output);
/**
* 画像をパーセンテージで拡大縮小します
* @param string $src オリジナル画像ファイル
* @param string $dst 入力ターゲット ファイル
* @param float/array $zoom スケーリング率、ブルーム倍率浮動小数点型の場合、配列型の場合は指定されたサイズでスケールします
* @param boolean $output ファイル出力を生成するかどうか
*/
functionscale($src, $dst=null, $zoom=1, $output=true)
{
if(!file_exists($src)) die('ファイルが存在しません。'); if(!$zoom) die('ズームの定義を解除します。');
$src_im = $this->IM($src);
$old_width = imagex($src_im); is_float($zoom)) {
//パーセンテージでズーム
$new_width = $old_width * $zoom;
}
elseif(is_array($zoom)) {
//ズームサイズ
$new_width = $zoom[0];
}
//ズームの高さが定義されているかどうか
if(!isset($zoom[1])) {
/ /スケーリング
$resize_im = $this->imageresize($src_im, $new_width);
}
else {
//非スケーリング
$this ->imageresize ($src_im, $new_width, $zoom[1]);
}
if(!$output) {
header("Content-type: image/jpeg"); size_im, null, $this->quality);
}
else {
$new_file = empty($dst)?
imagejpeg($resize_im , $new_file, $this->quality);
imagedestroy($im);
}
/**
* 画像をトリミング
* @param $src オリジナルファイル
* @param $dst ターゲットファイル
* @param $output ターゲットファイルを生成するかどうか
*/
Capture($src, $dst=null, $output=true) {
if(!file_exists($src)) die('ファイルが存在しません。');
$width = $this ->th_width ;
$height = $this->th_height;
$src_im = $this->IM($src);
$old_height = imagey ($src_im);
$capture = imagecreatetruecolor($width, $height);
$rgb =explode(",", $this->background);
$white = imagecolorallocate($capture) , $rgb[0], $rgb[1], $rgb[2]);
imagefill($capture, 0, 0, $white); //画像がサムネイルより大きい場合when
if($old_width > $width && $old_height>$height) {
$resize_im = $this->imageresize($src_im, $width)
// 画像の比率は異なります。適切な正規化時に、トリミングの比率を再計算します。
if(imagesy($resize_im) < $height) {
$proportion = $old_height/$this->th_height;
$resize_im = $this- >imageresize($src_im, $old_width/$proportion);
}
$posy = 0;
}
else {
//サムネール時に画像を中央に配置する
$posy = ($height-$old_height)/2;
$resize_im = $src_im; 🎜 >}
imagecopy($capture、$resize_im、$posx、$posy、0、0、imagesx($resize_im)、imagesy($resize_im));
if(!$output) {
header ("Content-type: image/jpeg");
imagejpeg($capture, null, $this->quality)
}
else {
$new_file = empty($dst) ) ? $src:$dst;
imagejpeg($capture, $new_file, $this->quality)
}
@imagedestroy($resize_im);
imagedestroy($capture);
}
/**
* ウォーターマーク画像の書き込み
* @param $src ウォーターマークを入れる必要がある画像
* @param $mark ウォーターマーク画像
* @param $transparent ウォーターマークの透明度
*/
パブリック関数 mark($src, $mark, $dst='', $output=true)
{
$mark_info = getimagesize($mark);
$src_info = getimagesize($src);
list($mw,$mh) = $mark_info; sh ) = $src_info;
$px = $sw - $mw;
$im = $this->IM($src); $ mim = $this->IM($mark);
imagecopymerge($im, $mim, $px, $py, 0, 0, $mw, $mh, $this->transparent); 🎜 >if($output){
$new_file = empty($dst);
imagejpeg($im, $new_file, $this->quality); 🎜 >else
{
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im); $ ミム);
}
/**
* ファイルを通じてさまざまな GD オブジェクトを取得します
*/
保護関数 IM($file)
{
if(!file_exists($file)) die('ファイルが存在しません。');
$info = getimagesize($file);
switch($info['mime'])
{
case 'image/gif':
$mim = imagecreatefromgif($file);
休憩;
case 'image/png':
$mim = imagecreatefrompng($file);
imagealphablending($mim, false);
imagesavealpha($mim, true);
休憩;
case 'image/jpeg':
$mim = imagecreatefromjpeg($file);
休憩;
デフォルト:
die('ファイル形式エラー。');
}
$mim を返す;
}
/**
* 画像の拡大縮小処理
* @param resource $src_im image GD object
* @param integer $width 画像の幅
* @param integer $height 画像の高さ(そうでない場合)高さを設定すると、画像は比例して拡大縮小されます
* @return resuorce $im は GD オブジェクトを返します
*/
保護関数 imageresize($src_im, $width, $height=null) {
$old_width = imagex($src_im);
$old_height = imagey($src_im);
$proportion = $old_width/$old_height;
$new_width = $width;
$new_height = is_null($height)? Round($new_width / $proportion):$height;
//新しい画像を作成し、背景色を塗りつぶします
$im = imagecreatetruecolor($new_width, $new_height);
$rgb =explode(",", $this->background);
$white = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
imagefill($im, 0, 0, $white);
//对图片进行缩放
imagecopyresize($im, $src_im, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
$im を返します;
}
/**
* クラス変数の割り当て
*/
パブリック関数 __set($key, $value)
{
$this->$key = $value;
}
/**
* クラス変数値を取得
*/
public function __get($key)
{
return $this->$key;
}
}
?>

人気の記事

人気の記事

ホットな記事タグ

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

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

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

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

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

ホットトピック











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

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