ホームページ  >  記事  >  php教程  >  PHP はサムネイルを生成します: PHP はスマート サムネイルを生成します

PHP はサムネイルを生成します: PHP はスマート サムネイルを生成します

WBOY
WBOYオリジナル
2016-06-21 08:49:591132ブラウズ

class assignimage {
//画像タイプ
var $type;
//実際の幅
var $width;
//実際の高さ
var $height;
//幅変更
var $resize_width;
//高さ変更
var $resize_height;
//トリミングするかどうか
var $cut;
//ソース画像
var $srcimg;
//宛先画像アドレス
var $dstimg;
//一時的に作成された画像
var $im
function raiseimage($img; 、$wid、$hei、$c、$dstpath) {
$this->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
//画像のタイプ
$this->type = strto lower ( substr ( strrchr ( $this ->srcimg, "." ), 1 ) );
//初期化イメージ
$this->initi_img ();
//ターゲットイメージアドレス
$this->gt ;dst_img ( $dstpath );
$this->width = imagex ( $this->im );
$this->height = imagey ( $this->im );
//画像を生成
$this- >newimg ();
ImageDestroy ( $this->im );
}
function newimg() {
//変更後 画像の比率
$resize_ratio = ( $this->resize_width) / ($this->resize_height);
//実際の画像の比率
$ratio = ($this->width) / ($this->height) );
if (($this->cut) == "1") //画像を切り取る
{
if ($ratio >= $resize_ratio) //高優先
{
$newimg = imagecreatetruecolor ( $this->resize_width, $this->resize_height );
imagecopyresampled ( $newimg, $this->im, 0, 0, 0, 0, $this-> ;resize_width, $this->resize_height, (($this->height) * $resize_ratio), $this->height );
ImageJpeg ( $newimg, $this->dstimg );
}
if ($ratio < $resize_ratio) //幅優先
{
$newimg = imagecreatetruecolor ( $this->resize_width, $this- >resize_height );
imagecopyresampled ( $newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, ( ($this->width) / $ size_ratio) );
ImageJpeg ( $newimg, $this->dstimg );
}
} else //画像をトリミングしません
{
if ($ratio >= $resize_ratio) {
$newimg = imagecreatetruecolor ( $this->resize_width, ($this->resize_width) / $ratio );
imagecopyresampled ( $newimg, $this ->im, 0, 0 , 0, 0, $this->resize_width, ($this->resize_width) / $ratio, $this->width, $this->height );
ImageJpeg ( $newimg, $this- >dstimg );
}
if ($ratio < $resize_ratio) {
$newimg = imagecreatetruecolor ( ($this->resize_height) * $ratio, $this->resize_height );
imagecopyresampled ( $newimg, $this->im, 0, 0, 0, 0, ($this->resize_height) * $ratio, $this->resize_height, $this->width, $ this->height );
ImageJpeg ( $newimg, $this->dstimg );
}
}
}
//初期化画像 Like
function initi_img() {
if ($this->type == "jpg") {
$this->im = imagecreatefromjpeg ( $this->srcimg );
}
if ($this ->type == "gif") {
$this->im = imagecreatefromgif ( $this->srcimg );
}
if ($this ->type == "png ") {
$this->im = imagecreatefrompng ( $this->srcimg );
}
}
//画像ターゲットアドレス
function dst_img($dstpath) {This記事リンク http://www.cxybl.com/html/wlbc/Php/20130326/37402.html



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。