ホームページ  >  記事  >  バックエンド開発  >  PHP画像比例拡大縮小クラスSimpleImage_PHPチュートリアルの使用方法と使用例の共有

PHP画像比例拡大縮小クラスSimpleImage_PHPチュートリアルの使用方法と使用例の共有

WBOY
WBOYオリジナル
2016-07-13 10:34:17868ブラウズ

使用例:
幅とスケールを比例的に設定します

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

include('SimpleImage.php');
$image = new SimpleImage ();
$image->load('picture.jpg');
$image->resizeToWidth(250);
$image->save('picture2.jpg');?>

Set高さ、比例的に拡大縮小します
コードをコピーします コードは次のとおりです:

include('SimpleImage.php');
$image = new SimpleImage();
$image- >load('picture .jpg');
$image->resizeToHeight(500);
$image->save('picture2.jpg');
$image->resizeToHeight(200);
$ image->save( 'picture3.jpg');?>

50%に拡大縮小
コードをコピー コードは次のとおりです:

include('SimpleImage. php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->scale(50);
$image->save('picture2. jpg');?>

スケーリング後に画面に直接出力
コードをコピー コードは次のとおりです:

header('Content-Type: image/jpeg' );
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToWidth(150);
$image ->output();?>


使用例:

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

include("SimpleImage.php");//画像処理クラスは以下です

$url="http://f3.v.veimg.cn/meadincms/1/2013/0703/20130703100937552.jpg";
$picfile = down($url);//画像をダウンロード (ダウンロードするパス)処理が完了した後、画像は空になる可能性があります。ここでは処理は実行されません)
$res = new SimpleImage();//画像処理の例
$res = $res->load($picfile);
$tmpfile = tempfile ().'.jpg ';//画像を保存するパスファイルを作成
$width = '30';//画像の幅を設定
$res->resizeToWidth($width);
$res- >save($tmpfile) ;//処理された画像を保存します (拡張子 .jpg なし)
//ここで合計 3 つのファイルが生成されます。1 つはダウンロードされた画像ファイル、1 つは一時ファイル、最後のファイルは加工された画像ファイル。最初の 2 つのファイルは最適化してクリーンアップする必要があります。


function down($url)
{
] = $header;
}

$http['タイムアウト'] = 50;

$ctx = stream_context_create(array(

'http' ));

if (!$content) {

false を返します。
$tmpfile = tempfile();

file_put_contents($tmpfile, $content);

return $tmpfile;

}

function tempfile()

{

$path = dirname(__FILE__)(time() / 300);

if(!file_exists($ path)){

する

タッチ($file);

return $file;

}


画像処理ソースコード (公式ウェブサイトアドレス: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/):

コードをコピー

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



/*
* ファイル: SimpleImage.php
* 著者: Simon Jarvis
* 著作権: 2006 Simon Jarvis
* 日付: 08/11/06
* リンク: http://www.white -hat-web-design.co.uk/articles/php-image-resizing.php
*
* このプログラムはフリー ソフトウェアです。 Free Software Foundation が発行する GNU General Public License
* の条項に基づいて再配布したり、
* 変更したりすることができます。ライセンスのバージョン 2
*、または (オプションで) それ以降のバージョンのいずれかです。
*
* このプログラムは役立つことを願って配布されています
* ただし、いかなる保証もありません。
*商品性や特定目的への適合性についての暗黙の保証もありません。詳細については、
* GNU 一般公衆利用許諾書を参照してください。
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {

var $image;
var $image_type;

functionload($filename) {

$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {

$this->image = @imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {

$this->image = @imagecreatefromgif($filename);
} elseif( $ this->image_type == IMAGETYPE_PNG ) {

$this->image = @imagecreatefrompng($filename);
}

if (!$this->image) {
falseを返す;
}

戻る$this;
}

function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {

if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image, $filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {

imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {

imagepng($this ->image,$filename);
}
if( $permissions != null) {

chmod($filename,$permissions);
}
}
関数出力($image_type=IMAGETYPE_JP)例) {

if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {

imagegif($this->image);
} elseif ( $image_type == IMAGETYPE_PNG ) {

imagepng($this->image);
}
}
function getWidth() {

return imagex($this->image);
}
function getHeight() {

画像を返しますy( $this->image);
}
function sinnerToHeight($height) {

$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio ;
$this->resize($width,$height);
}

function raiseToWidth($width) {
if ($this->getWidth() < $width) {
$width = $this->getWidth();
}
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}

functionscale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->サイズ変更($width,$height);
}

関数リサイズ($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0 、0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}


関数リサイズ2 ($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if( $this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG ) {
$current_transparent = imagecolortransparent($this->image);
if($current_transparent != -1) {
$transparent_color = imagecolorsforindex($this->image, $current_transparent);
imagecolorallocate($新しい画像、$transparent_color[ 'red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($new_image, 0, 0, $current_transparent);
imagecolortransparent($new_image, $current_transparent);
}もし( $this->image_type == IMAGETYPE_PNG) {
imagealphablending($new_image, false);
$color = imagecolorallocatealpha($new_image, 0, 0, 0,
imagefill($new_image, 0, 0, $color );
imagesavealpha($new_image, true);
}
}
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth () , $this->getHeight());
$this->image = $new_image;  
}

}

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/751937.html技術記事使用方法の例: 設定度、等比放复制代次例: ?php include('SimpleImage.php'); $image = 新しい SimpleImage(); $image-load('picture.jpg'); $image-resi...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。