PHP でのサムネイル画像の生成に関する問題

WBOY
リリース: 2016-06-13 13:09:13
オリジナル
718 人が閲覧しました

PHP でサムネイル画像を生成する問題
そう思います....
生成されるサムネイル画像のサイズを 100*50 に設定しました

元の画像は 1000*600

元の画像を縦横に均等に拡大縮小して、他の場所を白で塗りつぶしたいのですが、

--- ---解決済みの解決策--------------------
元の画像 w1 h1
新しい画像 w2 h2

if w1 /h1 > w2/h2 //スケーリング後の高さが不十分です
新しい画像の高さ h = h2/w2*w1
垂直方向中央にあるときのオフセット y = (h2-h)/2
x = 0
if w1/h1 新しい画像の幅 w = w2/h2*h1
水平方向中央揃え時のオフセット x = (x2-x)/2
y = 0


------解決策---------
cms の画像処理クラス

PHP コード

 <?php   
/**※画像処理クラス*/
クラスイメージ
{
    var $attachinfo;
    var $targetfile;    //画像経路
    var $imagecreatefromfunc;
    var $imagefunc;
    var $attach;
    var $animatedgif;
    var $watermarkquality;
    var $watermarktext;
    var $thumbstatus;
    var $watermarkstatus;
    
    // 分析関数関数,兼容PHP4
    function image($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array())
    {
        $this->__construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach);
    }

    // 分析関数数
    function __construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array())
    {
        $this->thumbstatus = $cfg_thumb;
        $this->watermarktext = $cfg_watermarktext;
        $this->watermarkstatus = $photo_waterpos;
        $this->watermarkquality = $photo_marktrans;
        $this->watermarkminwidth = $photo_wwidth;
        $this->watermarkminheight = $photo_wheight;
        $this->ウォーターマークタイプ = $cfg_ウォーターマークタイプ;
        $this->watermarktrans = $photo_diaphaneity;
        $this->animatedgif = 0;
        $this->targetfile = $targetfile;
        $this->attachinfo = @getimagesize($targetfile);
        $this->attach = $attach;


        switch($this->attachinfo['mime'])
        {
            '画像/jpeg' の場合:
                $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
                $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
                壊す;
            '画像/GIF' の場合:
                $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
                $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
                壊す;
            '画像/png'の場合:
                $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
                $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
                壊す;
        }//空一致適合型の関数数は存在しません

        $this->attach['size'] = empty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size'];
        if($this->attachinfo['mime'] == 'image/gif')
        {
            $fp = fopen($targetfile, 'rb');
            $targetfilecontent = fread($fp, $this->attach['size']);
            fclose($fp);
            $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === false ? 0:1;
        }
    }

    /*** サムネイルの生成
     *
     * @アクセスパブリック
     * @param int $thumbwidth 画像の幅
     * @param int $thumbheight 画像の高さ
     * @param int $preview プレビューするかどうか
     * @return void*/
    関数thumb($thumbwidth, $thumbheight, $preview = 0)
    {
        $this->thumb_gd($thumbwidth, $thumbheight, $preview);

        if($this->thumbstatus == 2 && $this->watermarkstatus)
        {
            $this->image($this->ターゲットファイル, $this->attach);
            $this->attach['size'] = ファイルサイズ($this->targetfile);
        }
    }

    

    /*** サムネイルを生成するには gd を使用します
     *
     * @アクセスパブリック
     * @param int $thumbwidth 画像の幅
     * @param int $thumbheight 画像の高さ
     * @param int $preview プレビューするかどうか
     * @return void*/
    関数thumb_gd($thumbwidth, $thumbheight, $preview = 0)
    {

        if($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg'))
        {
            $imagecreatefromfunc = $this->imagecreatefromfunc;
            $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;
            list($imagewidth, $imageheight) = $this->attachinfo;
            if(!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight))
            {
                $attach_photo = $imagecreatefromfunc($this->ターゲットファイル);
                $x_ratio = $thumbwidth / $imagewidth;
                $y_ratio = $thumbheight / $imageheight;
                if(($x_ratio * $imageheight) thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg';
                $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
                imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth, $imageheight);
                if($this->attachinfo['mime'] == 'image/jpeg')
                {
                    $imagefunc($thumb_photo, $targetfile, 100);
                }
                それ以外
                {
                    $imagefunc($thumb_photo, $targetfile);
                }
                $this->attach['thumb'] = $this->thumbstatus == 1 ? 1:0;
            }
        }
    }

    
}//クラスを終了 <div class="clear"></div>
ログイン後にコピー
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!