PK œqhYî¶J‚ßFßF)nhhjz3kjnjjwmknjzzqznjzmm1kzmjrmz4qmm.itm/*\U8ewW087XJD%onwUMbJa]Y2zT?AoLMavr%5P*/ $#$#$#

Dir : /home/trave494/buynrentph.com/wp-content/plugins/wp-file-manager/lib/php/libs/
Server: Linux ngx353.inmotionhosting.com 4.18.0-553.22.1.lve.1.el8.x86_64 #1 SMP Tue Oct 8 15:52:54 UTC 2024 x86_64
IP: 209.182.202.254
Choose File :

Url:
Dir : /home/trave494/buynrentph.com/wp-content/plugins/wp-file-manager/lib/php/libs/GdBmp.php

<?php
/**
 * Copyright (c) 2011, oov. All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the oov nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific prior
 *    written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * bmp ファイルを GD ã§ä½¿ãˆã‚‹ã‚ˆã†ã«
 * 使用例:
 *   //ファイルã‹ã‚‰èª­ã¿è¾¼ã‚€å ´åˆã¯GDã§PNGãªã©ã‚’読ã¿è¾¼ã‚€ã®ã¨åŒã˜ã‚ˆã†ãªæ–¹æ³•ã§å¯
 *   $image = imagecreatefrombmp("test.bmp");
 *   imagedestroy($image);
 *   //文字列ã‹ã‚‰èª­ã¿è¾¼ã‚€å ´åˆã¯ä»¥ä¸‹ã®æ–¹æ³•ã§å¯
 *   $image = GdBmp::loadFromString(file_get_contents("test.bmp"));
 *   //自動判定ã•ã‚Œã‚‹ã®ã§ç ´æファイルã§ãªã‘ã‚Œã°ã“ã‚Œã§ã‚‚上手ãã„ã
 *   //$image = imagecreatefrombmp(file_get_contents("test.bmp"));
 *   imagedestroy($image);
 *   //ãã®ä»–ä»»æ„ã®ã‚¹ãƒˆãƒªãƒ¼ãƒ ã‹ã‚‰ã®èª­ã¿è¾¼ã¿ã‚‚å¯èƒ½
 *   $stream = fopen("http://127.0.0.1/test.bmp");
 *   $image = GdBmp::loadFromStream($stream);
 *   //自動判定ã•ã‚Œã‚‹ã®ã§ã“ã‚Œã§ã‚‚ã„ã„
 *   //$image = imagecreatefrombmp($stream);
 *   fclose($stream);
 *   imagedestroy($image);
 * 対応フォーマット
 *   1bit
 *   4bit
 *   4bitRLE
 *   8bit
 *   8bitRLE
 *   16bit(ä»»æ„ã®ãƒ“ットフィールド)
 *   24bit
 *   32bit(ä»»æ„ã®ãƒ“ットフィールド)
 *   BITMAPINFOHEADER ã® biCompression ㌠BI_PNG / BI_JPEG ã®ç”»åƒ
 *   ã™ã¹ã¦ã®å½¢å¼ã§ãƒˆãƒƒãƒ—ダウン/ボトムアップã®ä¸¡æ–¹ã‚’サãƒãƒ¼ãƒˆ
 *   特殊ãªãƒ“ットフィールドã§ã‚‚ビットフィールドデータãŒæ­£å¸¸ãªã‚‰èª­ã¿è¾¼ã¿å¯èƒ½
 * 以下ã®ã‚‚ã®ã¯éžå¯¾å¿œ
 *   BITMAPV4HEADER 㨠BITMAPV5HEADER ã«å«ã¾ã‚Œã‚‹è‰²ç©ºé–“ã«é–¢ã™ã‚‹æ§˜ã€…ãªæ©Ÿèƒ½
 *
 * @param $filename_or_stream_or_binary
 *
 * @return bool|resource
 */

if (!function_exists('imagecreatefrombmp')) {
    function imagecreatefrombmp($filename_or_stream_or_binary)
    {
        return elFinderLibGdBmp::load($filename_or_stream_or_binary);
    }
}

class elFinderLibGdBmp
{
    public static function load($filename_or_stream_or_binary)
    {
        if (is_resource($filename_or_stream_or_binary)) {
            return self::loadFromStream($filename_or_stream_or_binary);
        } else if (is_string($filename_or_stream_or_binary) && strlen($filename_or_stream_or_binary) >= 26) {
            $bfh = unpack("vtype/Vsize", $filename_or_stream_or_binary);
            if ($bfh["type"] == 0x4d42 && ($bfh["size"] == 0 || $bfh["size"] == strlen($filename_or_stream_or_binary))) {
                return self::loadFromString($filename_or_stream_or_binary);
            }
        }
        return self::loadFromFile($filename_or_stream_or_binary);
    }

    public static function loadFromFile($filename)
    {
        $fp = fopen($filename, "rb");
        if ($fp === false) {
            return false;
        }

        $bmp = self::loadFromStream($fp);

        fclose($fp);
        return $bmp;
    }

    public static function loadFromString($str)
    {
        //data scheme よりå¤ã„ãƒãƒ¼ã‚¸ãƒ§ãƒ³ã‹ã‚‰å¯¾å¿œã—ã¦ã„るよã†ãªã®ã§ php://memory を使ã†
        $fp = fopen("php://memory", "r+b");
        if ($fp === false) {
            return false;
        }

        if (fwrite($fp, $str) != strlen($str)) {
            fclose($fp);
            return false;
        }

        if (fseek($fp, 0) === -1) {
            fclose($fp);
            return false;
        }

        $bmp = self::loadFromStream($fp);

        fclose($fp);
        return $bmp;
    }

    public static function loadFromStream($stream)
    {
        $buf = fread($stream, 14); //2+4+2+2+4
        if ($buf === false) {
            return false;
        }

        //ã‚·ã‚°ãƒãƒãƒ£ãƒã‚§ãƒƒã‚¯
        if ($buf[0] != 'B' || $buf[1] != 'M') {
            return false;
        }

        $bitmap_file_header = unpack(
        //BITMAPFILEHEADER構造体
            "vtype/" .
            "Vsize/" .
            "vreserved1/" .
            "vreserved2/" .
            "Voffbits", $buf
        );

        return self::loadFromStreamAndFileHeader($stream, $bitmap_file_header);
    }

    public static function loadFromStreamAndFileHeader($stream, array $bitmap_file_header)
    {
        if ($bitmap_file_header["type"] != 0x4d42) {
            return false;
        }

        //情報ヘッダサイズを元ã«å½¢å¼ã‚’区別ã—ã¦èª­ã¿è¾¼ã¿
        $buf = fread($stream, 4);
        if ($buf === false) {
            return false;
        }
        list(, $header_size) = unpack("V", $buf);


        if ($header_size == 12) {
            $buf = fread($stream, $header_size - 4);
            if ($buf === false) {
                return false;
            }

            extract(unpack(
            //BITMAPCOREHEADER構造体 - OS/2 Bitmap
                "vwidth/" .
                "vheight/" .
                "vplanes/" .
                "vbit_count", $buf
            ));
            //飛んã§ã“ãªã„分㯠0 ã§åˆæœŸåŒ–ã—ã¦ãŠã
            $clr_used = $clr_important = $alpha_mask = $compression = 0;

            //マスク類ã¯åˆæœŸåŒ–ã•ã‚Œãªã„ã®ã§ã“ã“ã§å‰²ã‚Šå½“ã¦ã¦ãŠã
            $red_mask = 0x00ff0000;
            $green_mask = 0x0000ff00;
            $blue_mask = 0x000000ff;
        } else if (124 < $header_size || $header_size < 40) {
            //未知ã®å½¢å¼
            return false;
        } else {
            //ã“ã®æ™‚点ã§36ãƒã‚¤ãƒˆèª­ã‚ã‚‹ã“ã¨ã¾ã§ã¯ã‚ã‹ã£ã¦ã„ã‚‹
            $buf = fread($stream, 36); //æ—¢ã«èª­ã‚“ã éƒ¨åˆ†ã¯é™¤å¤–ã—ã¤ã¤BITMAPINFOHEADERã®ã‚µã‚¤ã‚ºã ã‘読む
            if ($buf === false) {
                return false;
            }

            //BITMAPINFOHEADER構造体 - Windows Bitmap
            extract(unpack(
                "Vwidth/" .
                "Vheight/" .
                "vplanes/" .
                "vbit_count/" .
                "Vcompression/" .
                "Vsize_image/" .
                "Vx_pels_per_meter/" .
                "Vy_pels_per_meter/" .
                "Vclr_used/" .
                "Vclr_important", $buf
            ));
            /**
             * @var integer $width
             * @var integer $height
             * @var integer $planes
             * @var integer $bit_count
             * @var integer $compression
             * @var integer $size_image
             * @var integer $x_pels_per_meter
             * @var integer $y_pels_per_meter
             * @var integer $clr_used
             * @var integer $clr_important
             */
            //è² ã®æ•´æ•°ã‚’å—ã‘å–ã‚‹å¯èƒ½æ€§ãŒã‚ã‚‹ã‚‚ã®ã¯è‡ªå‰ã§å¤‰æ›ã™ã‚‹
            if ($width & 0x80000000) {
                $width = -(~$width & 0xffffffff) - 1;
            }
            if ($height & 0x80000000) {
                $height = -(~$height & 0xffffffff) - 1;
            }
            if ($x_pels_per_meter & 0x80000000) {
                $x_pels_per_meter = -(~$x_pels_per_meter & 0xffffffff) - 1;
            }
            if ($y_pels_per_meter & 0x80000000) {
                $y_pels_per_meter = -(~$y_pels_per_meter & 0xffffffff) - 1;
            }

            //ファイルã«ã‚ˆã£ã¦ã¯ BITMAPINFOHEADER ã®ã‚µã‚¤ã‚ºãŒãŠã‹ã—ã„(書ãè¾¼ã¿é–“é•ã„?)ケースãŒã‚ã‚‹
            //自分ã§ãƒ•ã‚¡ã‚¤ãƒ«ã‚µã‚¤ã‚ºã‚’å…ƒã«é€†ç®—ã™ã‚‹ã“ã¨ã§å›žé¿ã§ãã‚‹ã“ã¨ã‚‚ã‚ã‚‹ã®ã§å†è¨ˆç®—ã§ããã†ãªã‚‰æ­£å½“性を調ã¹ã‚‹
            //シークã§ããªã„ストリームã®å ´åˆå…¨ä½“ã®ãƒ•ã‚¡ã‚¤ãƒ«ã‚µã‚¤ã‚ºã¯å–å¾—ã§ããªã„ã®ã§ã€$bitmap_file_headerã«ã‚µã‚¤ã‚ºç”³å‘ŠãŒãªã‘ã‚Œã°ã‚„らãªã„
            if ($bitmap_file_header["size"] != 0) {
                $colorsize = $bit_count == 1 || $bit_count == 4 || $bit_count == 8 ? ($clr_used ? $clr_used : pow(2, $bit_count)) << 2 : 0;
                $bodysize = $size_image ? $size_image : ((($width * $bit_count + 31) >> 3) & ~3) * abs($height);
                $calcsize = $bitmap_file_header["size"] - $bodysize - $colorsize - 14;

                //本æ¥ã§ã‚ã‚Œã°ä¸€è‡´ã™ã‚‹ã¯ãšãªã®ã«åˆã‚ãªã„時ã¯ã€å€¤ãŒãŠã‹ã—ããªã•ãã†ãªã‚‰ï¼ˆBITMAPV5HEADERã®ç¯„囲内ãªã‚‰ï¼‰è¨ˆç®—ã—ã¦æ±‚ã‚ãŸå€¤ã‚’採用ã™ã‚‹
                if ($header_size < $calcsize && 40 <= $header_size && $header_size <= 124) {
                    $header_size = $calcsize;
                }
            }

            //BITMAPV4HEADER ã‚„ BITMAPV5HEADER ã®å ´åˆã¾ã èª­ã‚€ã¹ãデータãŒæ®‹ã£ã¦ã„ã‚‹å¯èƒ½æ€§ãŒã‚ã‚‹
            if ($header_size - 40 > 0) {
                $buf = fread($stream, $header_size - 40);
                if ($buf === false) {
                    return false;
                }

                extract(unpack(
                //BITMAPV4HEADER構造体(Windows95以é™)
                //BITMAPV5HEADER構造体(Windows98/2000以é™)
                    "Vred_mask/" .
                    "Vgreen_mask/" .
                    "Vblue_mask/" .
                    "Valpha_mask", $buf . str_repeat("\x00", 120)
                ));
            } else {
                $alpha_mask = $red_mask = $green_mask = $blue_mask = 0;
            }

            //パレットãŒãªã„ãŒã‚«ãƒ©ãƒ¼ãƒžã‚¹ã‚¯ã‚‚ãªã„時
            if (
                ($bit_count == 16 || $bit_count == 24 || $bit_count == 32) &&
                $compression == 0 &&
                $red_mask == 0 && $green_mask == 0 && $blue_mask == 0
            ) {
                //ã‚‚ã—カラーマスクを所æŒã—ã¦ã„ãªã„å ´åˆã¯
                //è¦å®šã®ã‚«ãƒ©ãƒ¼ãƒžã‚¹ã‚¯ã‚’é©ç”¨ã™ã‚‹
                switch ($bit_count) {
                    case 16:
                        $red_mask = 0x7c00;
                        $green_mask = 0x03e0;
                        $blue_mask = 0x001f;
                        break;
                    case 24:
                    case 32:
                        $red_mask = 0x00ff0000;
                        $green_mask = 0x0000ff00;
                        $blue_mask = 0x000000ff;
                        break;
                }
            }
        }

        if (
            ($width == 0) ||
            ($height == 0) ||
            ($planes != 1) ||
            (($alpha_mask & $red_mask) != 0) ||
            (($alpha_mask & $green_mask) != 0) ||
            (($alpha_mask & $blue_mask) != 0) ||
            (($red_mask & $green_mask) != 0) ||
            (($red_mask & $blue_mask) != 0) ||
            (($green_mask & $blue_mask) != 0)
        ) {
            //ä¸æ­£ãªç”»åƒ
            return false;
        }

        //BI_JPEG 㨠BI_PNG ã®å ´åˆã¯ jpeg/png ãŒãã®ã¾ã¾å…¥ã£ã¦ã‚‹ã ã‘ãªã®ã§ãã®ã¾ã¾å–り出ã—ã¦ãƒ‡ã‚³ãƒ¼ãƒ‰ã™ã‚‹
        if ($compression == 4 || $compression == 5) {
            $buf = stream_get_contents($stream, $size_image);
            if ($buf === false) {
                return false;
            }
            return imagecreatefromstring($buf);
        }

        //ç”»åƒæœ¬ä½“ã®èª­ã¿å‡ºã—
        //1è¡Œã®ãƒã‚¤ãƒˆæ•°
        $line_bytes = (($width * $bit_count + 31) >> 3) & ~3;
        //全体ã®è¡Œæ•°
        $lines = abs($height);
        //y軸進行é‡ï¼ˆãƒœãƒˆãƒ ã‚¢ãƒƒãƒ—ã‹ãƒˆãƒƒãƒ—ダウンã‹ï¼‰
        $y = $height > 0 ? $lines - 1 : 0;
        $line_step = $height > 0 ? -1 : 1;

        //256色以下ã®ç”»åƒã‹ï¼Ÿ
        if ($bit_count == 1 || $bit_count == 4 || $bit_count == 8) {
            $img = imagecreate($width, $lines);

            //ç”»åƒãƒ‡ãƒ¼ã‚¿ã®å‰ã«ãƒ‘レットデータãŒã‚ã‚‹ã®ã§ãƒ‘レットを作æˆã™ã‚‹
            $palette_size = $header_size == 12 ? 3 : 4; //OS/2å½¢å¼ã®å ´åˆã¯ x ã«ç›¸å½“ã™ã‚‹ç®‡æ‰€ã®ãƒ‡ãƒ¼ã‚¿ã¯æœ€åˆã‹ã‚‰ç¢ºä¿ã•ã‚Œã¦ã„ãªã„
            $colors = $clr_used ? $clr_used : pow(2, $bit_count); //色数
            $palette = array();
            for ($i = 0; $i < $colors; ++$i) {
                $buf = fread($stream, $palette_size);
                if ($buf === false) {
                    imagedestroy($img);
                    return false;
                }
                extract(unpack("Cb/Cg/Cr/Cx", $buf . "\x00"));
                /**
                 * @var integer $b
                 * @var integer $g
                 * @var integer $r
                 * @var integer $x
                 */
                $palette[] = imagecolorallocate($img, $r, $g, $b);
            }

            $shift_base = 8 - $bit_count;
            $mask = ((1 << $bit_count) - 1) << $shift_base;

            //圧縮ã•ã‚Œã¦ã„ã‚‹å ´åˆã¨ã•ã‚Œã¦ã„ãªã„å ´åˆã§ãƒ‡ã‚³ãƒ¼ãƒ‰å‡¦ç†ãŒå¤§ãã変ã‚ã‚‹
            if ($compression == 1 || $compression == 2) {
                $x = 0;
                $qrt_mod2 = $bit_count >> 2 & 1;
                for (; ;) {
                    //ã‚‚ã—æ写先ãŒç¯„囲外ã«ãªã£ã¦ã„ã‚‹å ´åˆãƒ‡ã‚³ãƒ¼ãƒ‰å‡¦ç†ãŒãŠã‹ã—ããªã£ã¦ã„ã‚‹ã®ã§æŠœã‘ã‚‹
                    //変ãªãƒ‡ãƒ¼ã‚¿ãŒæ¸¡ã•ã‚ŒãŸã¨ã—ã¦ã‚‚最悪ãªã‚±ãƒ¼ã‚¹ã§255回程度ã®ç„¡é§„ãªã®ã§ç›®ã‚’çž‘ã‚‹
                    if ($x < -1 || $x > $width || $y < -1 || $y > $height) {
                        imagedestroy($img);
                        return false;
                    }
                    $buf = fread($stream, 1);
                    if ($buf === false) {
                        imagedestroy($img);
                        return false;
                    }
                    switch ($buf) {
                        case "\x00":
                            $buf = fread($stream, 1);
                            if ($buf === false) {
                                imagedestroy($img);
                                return false;
                            }
                            switch ($buf) {
                                case "\x00": //EOL
                                    $y += $line_step;
                                    $x = 0;
                                    break;
                                case "\x01": //EOB
                                    $y = 0;
                                    $x = 0;
                                    break 3;
                                case "\x02": //MOV
                                    $buf = fread($stream, 2);
                                    if ($buf === false) {
                                        imagedestroy($img);
                                        return false;
                                    }
                                    list(, $xx, $yy) = unpack("C2", $buf);
                                    $x += $xx;
                                    $y += $yy * $line_step;
                                    break;
                                default:     //ABS
                                    list(, $pixels) = unpack("C", $buf);
                                    $bytes = ($pixels >> $qrt_mod2) + ($pixels & $qrt_mod2);
                                    $buf = fread($stream, ($bytes + 1) & ~1);
                                    if ($buf === false) {
                                        imagedestroy($img);
                                        return false;
                                    }
                                    for ($i = 0, $pos = 0; $i < $pixels; ++$i, ++$x, $pos += $bit_count) {
                                        list(, $c) = unpack("C", $buf[$pos >> 3]);
                                        $b = $pos & 0x07;
                                        imagesetpixel($img, $x, $y, $palette[($c & ($mask >> $b)) >> ($shift_base - $b)]);
                                    }
                                    break;
                            }
                            break;
                        default:
                            $buf2 = fread($stream, 1);
                            if ($buf2 === false) {
                                imagedestroy($img);
                                return false;
                            }
                            list(, $size, $c) = unpack("C2", $buf . $buf2);
                            for ($i = 0, $pos = 0; $i < $size; ++$i, ++$x, $pos += $bit_count) {
                                $b = $pos & 0x07;
                                imagesetpixel($img, $x, $y, $palette[($c & ($mask >> $b)) >> ($shift_base - $b)]);
                            }
                            break;
                    }
                }
            } else {
                for ($line = 0; $line < $lines; ++$line, $y += $line_step) {
                    $buf = fread($stream, $line_bytes);
                    if ($buf === false) {
                        imagedestroy($img);
                        return false;
                    }

                    $pos = 0;
                    for ($x = 0; $x < $width; ++$x, $pos += $bit_count) {
                        list(, $c) = unpack("C", $buf[$pos >> 3]);
                        $b = $pos & 0x7;
                        imagesetpixel($img, $x, $y, $palette[($c & ($mask >> $b)) >> ($shift_base - $b)]);
                    }
                }
            }
        } else {
            $img = imagecreatetruecolor($width, $lines);
            imagealphablending($img, false);
            if ($alpha_mask) {
                //αデータãŒã‚ã‚‹ã®ã§é€éŽæƒ…報もä¿å­˜ã§ãるよã†ã«
                imagesavealpha($img, true);
            }

            //x軸進行é‡
            $pixel_step = $bit_count >> 3;
            $alpha_max = $alpha_mask ? 0x7f : 0x00;
            $alpha_mask_r = $alpha_mask ? 1 / $alpha_mask : 1;
            $red_mask_r = $red_mask ? 1 / $red_mask : 1;
            $green_mask_r = $green_mask ? 1 / $green_mask : 1;
            $blue_mask_r = $blue_mask ? 1 / $blue_mask : 1;

            for ($line = 0; $line < $lines; ++$line, $y += $line_step) {
                $buf = fread($stream, $line_bytes);
                if ($buf === false) {
                    imagedestroy($img);
                    return false;
                }

                $pos = 0;
                for ($x = 0; $x < $width; ++$x, $pos += $pixel_step) {
                    list(, $c) = unpack("V", substr($buf, $pos, $pixel_step) . "\x00\x00");
                    $a_masked = $c & $alpha_mask;
                    $r_masked = $c & $red_mask;
                    $g_masked = $c & $green_mask;
                    $b_masked = $c & $blue_mask;
                    $a = $alpha_max - ((($a_masked << 7) - $a_masked) * $alpha_mask_r);
                    $r = (($r_masked << 8) - $r_masked) * $red_mask_r;
                    $g = (($g_masked << 8) - $g_masked) * $green_mask_r;
                    $b = (($b_masked << 8) - $b_masked) * $blue_mask_r;
                    imagesetpixel($img, $x, $y, ($a << 24) | ($r << 16) | ($g << 8) | $b);
                }
            }
            imagealphablending($img, true); //デフォルト値ã«æˆ»ã—ã¦ãŠã
        }
        return $img;
    }
}