php logo付きQRコードの作成

4294 ワード

<?php





/**

    php     

**/







class MyQrcode{



    const SIZE = 150;

    const LEVEL = "L";

    const MARGIN = 4;





    /**

        logo   logo     



                 

    **/

    public static function factoryCreateQrcode($type, $string, $filename = false, $logo = null,$level = self::LEVEL, $size = self::SIZE,  $margin = self::MARGIN){

        $type = strtolower($type);

        if(!in_array($type, array("google", "php", "logo"))){

            return false;

        }

        $method = "createQrcodeFrom".ucfirst($type);

        return call_user_func_array(

            'self::'.$method,

            array($string, $filename, $logo, $level, $size, $margin, $logo)

            );



    }





  /**

     * google api      【QRcode      4296            ,             】

     * @param string $chl         ,     、  、     、  。        ,      UTF-8 URL-encoded.           2K   ,   POST  

     * @param int $widhtHeight           

     * @param string $EC_level       ,QR         ,       、   、   、  。

     *                 L-  :        7%   

     *                 M-       15%   

     *                 Q-       25%   

     *                 H-       30%   

     * @param int $margin               

       @return   

     */

    public static function createQrcodeFromGoogle($string, $filename = false, $logo = null, $level = self::LEVEL, $size = self::SIZE,  $margin = self::MARGIN){

        return 'http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$level.'|'.$margin.'&chl='.urlencode($string);

    }

    /**

            

    **/

    public static function createQrcodeFromPhp($string, $filename = false, $logo = null, $level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN)    {

        //    php QR 

        $dir = dirname(__FILE__).DIRECTORY_SEPARATOR;

        require_once $dir."phpqrcode".DIRECTORY_SEPARATOR."phpqrcode.php";

        $filename = $dir.$filename;



        QRcode::png($string, $filename, $level, $size, $margin);

        return $filename;

    }



    public static function createQrcodeFromLogo($string, $filename = false, $logo = null, $level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN)   {

        $qrfile = self::createQrcodeFromPhp($string, $filename,$logo, $level, $size, $margin);

        return self::creatQrcodeWithLogo($logo, $qrfile);

    }



    /**

        @param $logo logo  

        @param $qrfile qrfile    

        @return                

    **/

    public static function creatQrcodeWithLogo($logo, $qrfile, $size=6)
    {

        if(!is_file($qrfile) || !is_file($logo)){

            return false;

        }

        $QR = imagecreatefrompng($qrfile);//   QR 

        if ($logo !== FALSE) {

          $logo = imagecreatefromstring(file_get_contents($logo));

          $QR_width = imagesx($QR);

          $QR_height = imagesy($QR);

          $logo_width = imagesx($logo);

          $logo_height = imagesy($logo);

          $logo_qr_width = $QR_width/$size;

          $scale = $logo_width/$logo_qr_width;

          $logo_qr_height = $logo_height/$scale;

          $from_width = ($QR_width-$logo_qr_width)/2;

          imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);

        }

        //                    

        if(is_file($qrfile) && strpos($qrfile, "http") === false){

            imagepng($QR, $qrfile);

        } else{

            header('Content-type: image/png');

            imagepng($QR);

            imagedestroy($QR);

        }

    }



}



/*

$type = "php";

$string = "beck.bi";

$filename = "test1.png";

MyQrcode::factoryCreateQrcode($type, $string, $filename );

*/



/*

$type = "google";

$string = "beck";

var_dump(MyQrcode::factoryCreateQrcode($type, $string));

*/



$type = "logo";

$string = "beck.bi";

$filename = "test2.png";

$logo = "1.jpg";

MyQrcode::factoryCreateQrcode($type, $string, $filename, $logo);





?>