ホームページ バックエンド開発 PHPチュートリアル php_PHP チュートリアルでサムネイル画像クラスを生成する

php_PHP チュートリアルでサムネイル画像クラスを生成する

Jul 13, 2016 am 10:46 AM
php アップロード 写真 写真 大きい 小さい 私たち 指定 はい 生成する 親切 欲しい 必要

画像が大きい場合は、指定したサイズの小さい画像を生成する必要があり、これはリソースを節約することができ、現在では一般的な方法でもあります。サムネイルを生成するための php クラス。カスタムの高さと幅をサポートします。高さと幅に応じたスクリーンショットの撮影にも対応しています

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

クラスサイズ変更画像
{
//画像タイプ
var $type
//実際の幅
変数 $幅
//実際の身長
変数 $高さ
//幅を変更しました
var $resize_width
//変更後の高さ
変数 $resize_height
//画像をトリミングするかどうか
var $カット
//ソース画像
var $srcimg
//対象の画像アドレス
変数 $dstig
//一時的に作成された画像
ヴァルドルム
関数 Resizeimage($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 -> dst_img($dstpath); //-- $this->width = imagex($this->im); $this->height = imagey($this->im); //画像を生成する
$this->newimg(); ImageDestroy ($this->im); }
関数 newimg()
{
//変更された画像の割合
$resize_ratio = ($this->resize_width)/($this->resize_height); //実際の画像の比率
$ratio = ($this->幅)/($this->高さ); 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 ->身長); ImageJpeg ($newimg,$this->dstimg);                                                                                         If($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) /$resize_ratio)); ImageJpeg ($newimg,$this->dstimg);                                                                                                                                                                    その他
//トリミングなし
                                                                          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->gt ;身長); ImageJpeg ($newimg,$this->dstimg);                                                                                         If($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-> ;身長); 
                ImageJpeg ($newimg,$this->dstimg); 
            }
        }
    }
    //初期化图象
    関数 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); 
        }
    }
    //图象目标地址
    関数 dst_img($dstpath)
    {
        $full_length = strlen($this->srcimg); 
        $type_length = strlen($this->type); 
        $name_length = $full_length-$type_length; 

$name = substr($this->srcimg,0,$name_length-1); 
        $this->dstimg = $dstpath; 

//echo $this->dstimg; 
    }
}

$resizeimage = new assignimage("11.jpg", "200", "150", "1","17.jpg"); 
?>

实例二

代码如下 复制幣

/**
* サムネイルを生成します
* @著者 yangzhiguo0903@163.com
* @param string ソース画像の絶対完全なアドレス {ファイル名とサフィックス付き}
* @param string ターゲット画像の絶対完全アドレス {ファイル名とサフィックス付き}
* @param int サムネイルの幅 {0: 現時点では、ターゲットの高さを 0 にすることはできず、ターゲットの幅はソース画像の幅 * (ターゲットの高さ/ソース画像の高さ)}
* @param int サムネイルの高さ {0: 現時点では、ターゲットの幅を 0 にすることはできず、ターゲットの高さはソース画像の高さ * (ターゲットの幅/ソース画像の幅)}
* @param int {幅と高さは 0 以外である必要があります} をトリミングするかどうか
* @param int/float Scale {0: スケーリングなし、0 * @return boolean
​*/
関数 img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
{
    if(!is_file($src_img))
    {
        false を返します;
    }
    $ot = ファイル拡張子($dst_img);
    $otfunc = 'イメージ' 。 ($ot == 'jpg' ? 'jpeg' : $ot);
    $srcinfo = getimagesize($src_img);
    $src_w = $srcinfo[0];
    $src_h = $srcinfo[1];
    $type = strto lower(substr(image_type_to_extension($srcinfo[2]), 1));
    $createfun = 'imagecreatefrom' 。 ($type == 'jpg' ? 'jpeg' : $type);

$dst_h = $height;
    $dst_w = $width;
    $x = $y = 0;

/**
* * サムネイルはソース画像のサイズを超えてはなりません (幅または高さが 1 つしかない場合)
​​*/
    if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0))
    {
        $比例 = 1;
    }
    if($width>$src_w)
    {
        $dst_w = $width = $src_w;
    }
    if($height>$src_h)
    {
        $dst_h = $height = $src_h;
    }

if(!$幅 && !$高さ && !$比率)
    {
        false を返します;
    }
    if(!$比例)
    {
        if($cut == 0)
        {
            if($dst_w && $dst_h)
            {
                if($dst_w/$src_w> $dst_h/$src_h)
                {
                    $dst_w = $src_w * ($dst_h / $src_h);
                    $x = 0 - ($dst_w - $width) / 2;
                }
                それ以外
                {
                    $dst_h = $src_h * ($dst_w / $src_w);
                    $y = 0 - ($dst_h - $height) / 2;
                }
            }
            else if($dst_w xor $dst_h)
            {
                if($dst_w && !$dst_h) //有宽無高
                {
                    $proper = $dst_w / $src_w;
                    $height = $dst_h = $src_h * $propor;
                }
                else if(!$dst_w && $dst_h) //有高無宽
                {
                    $proper = $dst_h / $src_h;
                    $width = $dst_w = $src_w * $propor;
                }
            }
        }
        それ以外
        {
            if(!$dst_h) //裁断時無高
            {
                $height = $dst_h = $dst_w;
            }
            if(!$dst_w) //裁断時無宽
            {
                $width = $dst_w = $dst_h;
            }
            $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
            $dst_w = (int)round($src_w * $propor);
            $dst_h = (int)round($src_h * $propor);
            $x = ($width - $dst_w) / 2;
            $y = ($height - $dst_h) / 2;
        }
    }
    それ以外
    {
        $proportion = min($proportion, 1);
        $height = $dst_h = $src_h * $proportion;
        $width = $dst_w = $src_w * $proportion;
    }

$src = $createfun($src_img);
    $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
    $white = imagecolorallocate($dst, 255, 255, 255);
    imagefill($dst, 0, 0, $white);

if(function_exists('imagecopyresampled'))
    {
        imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    }
    それ以外
    {
        imagecopyresize($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    }
    $otfunc($dst, $dst_img);
    imagedestroy($dst);
    imagedestroy($src);
    true を返します;
}

使用方法

代码如下 复制幣

$src_img = "./ROSI_050_002.JPG";
$dst_img = "./ROSI_050_002_thumb.jpg";
$stat = img2thumb($src_img, $dst_img, $width = 200, $height = 300, $cut = 0, $proportion = 0);
if($stat){
 echo '画像のサイズ変更が成功しました!
';
 echo ''; 
}その他{
 echo '画像のサイズ変更に失敗しました!'; 
}

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/632963.html技術記事略図の生成は当我们要上传图片時果图片较大我们要生成一指定大小小图列表表表示,这样可节省资源又是做现在常用法...
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

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

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

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

Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Dec 24, 2024 pm 04:42 PM

PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP データベースの操作 CakePHP データベースの操作 Sep 10, 2024 pm 05:25 PM

CakePHP でデータベースを操作するのは非常に簡単です。この章では、CRUD (作成、読み取り、更新、削除) 操作について理解します。

CakePHP の日付と時刻 CakePHP の日付と時刻 Sep 10, 2024 pm 05:27 PM

Cakephp4 で日付と時刻を操作するには、利用可能な FrozenTime クラスを利用します。

CakePHP ファイルのアップロード CakePHP ファイルのアップロード Sep 10, 2024 pm 05:27 PM

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP ルーティング CakePHP ルーティング Sep 10, 2024 pm 05:25 PM

この章では、ルーティングに関連する次のトピックを学習します。

CakePHP について話し合う CakePHP について話し合う Sep 10, 2024 pm 05:28 PM

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

CakePHP バリデータの作成 CakePHP バリデータの作成 Sep 10, 2024 pm 05:26 PM

Validator は、コントローラーに次の 2 行を追加することで作成できます。

CakePHP のロギング CakePHP のロギング Sep 10, 2024 pm 05:26 PM

CakePHP へのログインは非常に簡単な作業です。使用する関数は 1 つだけです。 cronjob などのバックグラウンド プロセスのエラー、例外、ユーザー アクティビティ、ユーザーが実行したアクションをログに記録できます。 CakePHP でのデータのログ記録は簡単です。 log()関数が提供されています

See all articles