ホームページ  >  記事  >  バックエンド開発  >  画像を処理するための PHP クラス実装コード_PHP チュートリアル

画像を処理するための PHP クラス実装コード_PHP チュートリアル

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

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

/**
* 著者:yagas
* メール:yagas60@21cn.com
*/
class Image
{
/**クラス保護された変数*/
protected $th_width = 100;
protected $th_height = 50; //画質
protected $transparent = 50; //ウォーターマークの透明度
protected $background = "255,255,255"; //背景色
/**
* サムネイルファイルを生成
* @param $src オリジナル画像ファイル
* @param $dst ターゲットファイル
*/
public function subject($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 ファイル出力を生成するかどうか
*/
パブリック関数scale($src, $dst=null, $zoom=1, $output=true)
{
if(!file_exists($src)) die('ファイルがありません');
if(!$zoom) die('ズームを定義しません。');
$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 {
//非比例スケーリング
$resize_im = $this->imageresize($src_im, $new_width, $zoom[1]);
if(!$output) {
header ("Content-type: image/jpeg");
imagejpeg($resize_im, null, $this->quality)
}
else {
$new_file = empty($dst);
imagejpeg($resize_im, $new_file, $this->quality);
}
imagedestroy($nIm);
/**
* 画像をトリミングします
* @param $src オリジナルファイル
* @param $dst ターゲットファイル
* @param $output ターゲットファイルを生成するかどうか
*/
パブリック関数キャプチャ($) src , $dst=null, $output=true) {
if(!file_exists($src)) die('ファイルが存在しません。');
$width = $this->th_width; - >th_height;
$src_im = $this->IM($src);
$old_height = imagey($src_im);高さ );
$rgb =explode(",", $this->background);
$white = imagecolorallocate($capture, $rgb[1], $rgb[2]); imagefill ($capture, 0, 0, $white);
//画像がサムネイルより大きい場合にサイズを変更する
if($old_width > $width && $old_height>$height) {
$resize_im = $this-> ;imageresize ($src_im, $width);
//画像の比率が基準を満たしていない場合、比率を再計算してトリミングします
if(imagesy($resize_im) $proportion = $old_height/ $this->th_height ;
$resize_im = $this->imageresize($src_im, $old_width/$proportion);
$posx = 0;
else {
//画像がサムネイルより小さい場合、画像は中央に表示されます
$posx = ($width-$old_width)/2;
$resize_im = $src_im; ;
}
imagecopy($capture、$resize_im、$ posx、$posy、0、0、imagesx($resize_im)、imagesy($resize_im));
if(!$output) {
header("コンテンツタイプ: image/jpeg");
imagejpeg($capture, null, $this->quality);
}
else {
$new_file = empty($dst)? $src:$dst;
imagejpeg($capture, $new_file, $this->quality);
imagedestroy($src_im);
imagedestroy($capture);
/**
* ウォーターマーク画像を書き込みます
* @param $src ウォーターマークを入れる必要がある画像
* @param $mark ウォーターマーク画像
* @param $transparent ウォーターマークの透明度
*/
パブリック関数マーク($src, $mark, $dst='', $output=true)
{
$mark_info = getimagesize($mark)
$src_info = getimagesize($src); $mark_info;
list($sw,$sh) = $src_info;
$py = $sh - $mh; ;
$mim = $this->IM($マーク);
imagecopymerge($im, $mim, $px, $py, 0, 0, $mw, $mh, $this->transparent); if($output){
$new_file = empty($ dst);
imagejpeg($im, $this->quality)
}
else
{
header('コンテンツ-type: 画像/jpeg');
imagejpeg($im)
imagedestroy($mim);
/**
* ファイルを通じてさまざまな 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 画像 GD オブジェクト
* @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;
}
}
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/320758.html技術記事复制代码代码如下: ?php /*** 著者:yagas * メール:yagas60@21cn.com*/ class Image { /**クラス保護された変数*/ protected $th_width = 100;保護された $th_height = 50;保護されています...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。