php using GD library background picture black rounded solve problems as well as pictures loss problems

if (!function_exists('gene_poster')) {

    function gene_poster($param)
    {
        $user = User::where(['id' => $param['user_id']])->find();
        $link = "http://".$_SERVER['SERVER_NAME'] . '/index/user/register/pid/' . $user['id'];
      /*  echo '<pre>';
        print_r($link);
        exit;*/
        $background = $param['background'];
        $nickname = $user['nickname'];
        $mobile = $user['mobile'];
        $avatar = $user['avatar'];

        $path = "../public/uploads/" . date('Ymd') . "/qrcode/";
        $poster_path = "../public/uploads/" . date('Ymd') . "/poster/";

        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }

        if (!is_dir($poster_path)) {
            mkdir($poster_path, 0777, true);
        }

        if (!$user['qrcode']) {
            /*1生成二维码存储*/
            $qr = new Endroid\QrCode\QrCode();
            $qr_code_img = $qr->setText($link)
                ->setSize(300)
                ->setPadding(10)
                ->setErrorCorrection('high')
                ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
                ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
                ->setLabel('')
                ->setLabelFontSize(16)
                ->setImageType(Endroid\QrCode\QrCode::IMAGE_TYPE_PNG);


            //$path = "";


            $qrcode_file_name = $path . date('YmdHis') . $param['user_id'] . ".png";
            $re = $qr_code_img->save($qrcode_file_name);
            User::where(['id' => $param['user_id']])->update(['qrcode' => $qrcode_file_name]);
        } else {
            $qrcode_file_name = $user['qrcode'];
        }

        $font_path = '../public/assets/fonts/captcha.ttf';
        $font_path = 'D:\WORK\project\kangaroocollege\public\assets\fonts\fangzhengheitijianti.TTF';
        /*echo '<pre>';
        / * 2 * Paint Save /
        Exit; * /
        print_r (is_file ($ font_path));

        Array config = $ ( 
            'text' => array(
                array(
                    'text' => $ the Nickname, 
                    'left' => 350, 
                    'Top' => 2050, 
                    'FontPath' => $ font_path, // font file 
                    'fontSize' => 50, // size 
                    ' fontColor '=>' 255,255,255 ', // font color 
                    ' angle '=> 0, 
                ), 

                Array ( 
                    ' text '=> $ Mobile, 
                    ' left '=> 350, 
                    ' Top '=> 2150 is, 
                    ' FontPath '=> $ font_path,// font file 
                    'fontSize' => 50, // Font Size 
                    'fontColor' =>'255,255,255', font color // 
                    'angle' => 0, 
                ),

                Array ( 
                    'text' => 'I am your exclusive customer service', 
                    'left' => 160, 
                    'Top' => 2250, 
                    'FontPath' => $ font_path, // font file 
                    'fontSize' => 60, / / font 
                    'fontColor' => '255,255,255' , // Font color 
                    'angle' => 0, 
                ), 
                Array ( 
                    'text' => 'available to me for advice', 
                    'left' => 160, 
                    'Top' => 2350,  
                    'FontPath' => $ font_path, // font file
                    'the fontSize' => 60,// size 
                    'fontColor' => '255,255,255' , // font color 
                    'angle' => 0,
                ) 
            ), 
            'Image' => Array ( 
                Array ( 
                   // 'URL' => $ Avatar, image resource path // 
                    'url' => border_radius ($ avatar), // Image resource path 
                    'left' => 160, 
                    'Top' => 2000, 
                    'stream' => 0, whether the character string resource // picture image stream 
                    'right' => 0, 
                    'bottom' => 0, 
                    'width' => 150, 
                    'height' => 150, 
                    'Opacity' => 100 
                ),
                    'left' => 900,
                array(
                    'url' => $qrcode_file_name,
                    'top' => 2000,
                    'right' => 0,
                    'stream' => 0,
                    'bottom' => 0,
                    'width' => 400,
                    'height' => 400,
                    'opacity' => 100
                ),

            ),
            'background' => $background,
        );
        $post_file_name = $poster_path . date('YmdHis') . $param['user_id'] . ".png";;
//echo createPoster($config,$filename);
        return createPoster($config, $post_file_name);
 * Generate posters
/ **
}

    }
 
        'text' => '',
 * @param string $ filename generation posters file name, do not pass this parameter file is not generated, the output images directly
 @Return BOOL * | String [type] [Description] 
 * / 
function createPoster ($ config = Array (), $ filename = "") 
{ 
// echo '<pre>'; 
// print_r ($ config); 
// print_r // ($ config [ 'background']); 
// Exit; 
    // if the newspaper to see what is wrong, you can adjust this comment header 
    // IF (empty ($ filename)) header ( "Content-of the type: Image / PNG "); 
    $ imageDefault = Array ( 
        'left' => 0, 
        'Top' => 0, 
        'right' => 0, 
        'bottom' => 0, 
        'width' => 100, 
        'height' => 100, 
        'Opacity' => 100 
    );
    $textDefault = array(
        'left' => 0,
        'top' => 0,
        'fontSize' => 32, // size 
        'fontColor' => '255,255,255' , // font color 
        'angle' => 0, 
    ); 
    $ background = $ config [ 'background']; // bottom posters have BACKGROUND 
    bACKGROUND method // 
    $ = backgroundInfo the getimagesize ($ background); 
    $ backgroundFun = 'imagecreatefrom' image_type_to_extension ($ backgroundInfo [2], to false);. 
    $ $ backgroundFun background = ($ background); 
    $ backgroundWidth = imagesx ($ background); bACKGROUND width // 
    $ backgroundHeight = imagesy ($ background) ; // bACKGROUND height 
    $ imageRes = imagecreatetruecolor (backgroundWidth $, $ backgroundHeight); 
    $ Color = imagecolorallocate ($ imageRes, 0, 0, 0); 
    imagefill($imageRes, 0, 0, $color);
    // imagecolortransparent ($ imageRes , $ color);// Color Transparent 
    imagecopyresampled ($ imageRes, $ background, 0, 0, 0, 0, imagesx ($ background), imagesy ($ background), imagesx ($ background), imagesy ($ background)); 
    // process the image 
    IF (empty ($ config [ 'image']!)) { 
        the foreach ($ config [ 'image'] AS Key $ => Val $) { 
            $ the array_merge Val = ($ imageDefault, $ Val); 
            $ info = the getimagesize ($ Val [ 'URL']); 
            $ function = 'imagecreatefrom' image_type_to_extension ($ info [2], to false);. 
            IF ($ Val [ 'Stream']) {/ / If you pass a string image stream 
                $ info = getimagesizefromstring (Val $ [ 'URL']); 
                $ function = 'imagecreatefromstring';
            }
            RES function = $ $ ($ Val [ 'URL']); 
            $resWidth = $info[0];
            $ resHeight info = $ [. 1]; 

            // Create sketchpad, scale the image to a specified size 
            $ canvas = imagecreatetruecolor ($ val [ 'width'], $ val [ 'height']); 
            ImageFill (Canvas $, 0, 0, $ Color); 

            / * echo '<pre>'; 
            print_r (. 1); 
            Exit; * / 
            / * --- png for processing scaled FIG transparent background start problem becomes black * --- / 
            IF (Key $ == 0) { 
                $ Color = imagecolorallocatealpha ($ Canvas, 255,255,255,127); 
                imagecolortransparent (Canvas $, $ Color); 
                ImageFill ($ Canvas, 0,0, $ Color) ; 
            } 
            / * --- for the scaling process of FIG png transparent background becomes black end of the problem --- * /


            // Key function parameters (width and height w target resource, source, target resource start coordinates x, y, start x coordinate origin resource, y, target resource, h, width and height of the source resource w, H) 
    if (! Empty ($ config [ 'text'])) {
            imagecopyresampled ($ Canvas, $ RES, 0, 0, 0, 0, $ Val [ 'width'], $ Val [ 'height'], $ resWidth, $ resHeight); 
            $ Val [ 'left'] = $ Val [ 'left'] <0 $ backgroundWidth - ABS ($ Val [ 'left']) - $ Val [ 'width']:? $ Val [ 'left']; 
            $ Val [ 'Top'] = $ Val [ 'Top '?] <0 $ backgroundHeight - ABS (Val $ [' Top ']) - Val $ [' height ']: Val $ [' Top ']; 

            // place the image 
            imagecopymerge ($ imageRes, $ canvas, $ val [ 'left'], $ val [ 'top'], $ val [ 'right'], $ val [ 'bottom'], $ val [ 'width'], $ val [ 'height'], $ val [ 'opacity ']); // left, top, right, bottom, width, height, transparency 
        } 
    } 
    // process the text] As $ key => $ choice) { 
            $ choices = array_merge ($ default text, $ Val);
        foreach ($config['text'] as $key => $val) {
            list($R, $G, $B) = explode(',', $val['fontColor']);
            $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
            $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
            $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];

//            echo '<pre>';
//            print_r($val['fontPath']);
//            print_r(is_file($val['fontPath']));
//            exit;

            imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'],Val $ [ 'Top'], $ fontColor, Val $ [ 'FontPath'], Val $ [ 'text']); 
    // This must be a
    }
        }

    imagealphablending ($ imageRes, to false); 

    imagesavealpha ($ imageRes, to true); 
    // generate picture 
    IF {(empty ($ filename)!) 
        $ imagepng RES = (imageRes $, $ filename,. 5); // save the local 
        imagedestroy ($ imageRes); 
        IF return false ($ RES!); 
        return $ filename; 
    } the else { 
        imagejpeg ($ imageRes); // display in the browser 
        imagedestroy ($ imageRes); 
    } 
} 

/ ** 
 * picture rounded corners 
 * / 
function border_radius ($ imgpath) 
{ 
    $ The pathinfo EXT = ($ imgpath); 
   / * echo '<pre>'; 
    print_r ($ EXT); 
    Exit; * / 
    $ src_img = null; 
    IF (isset ($ EXT [ 'Extension'])) { 
        Switch ($ EXT [ 'Extension ']) {
            Case 'JPG': 
                $ src_img = imagecreatefromjpeg ($ imgpath); 
                BREAK; 
            Case 'PNG': 
                $ src_img = imagecreatefrompng ($ imgpath); 
                BREAK; 

        } 
    } the else { 
        $ src_img = imagecreatefromjpeg ($ imgpath); 
    } 

    $ WH = the getimagesize ($ imgpath); 
    $ W = $ WH [0]; 
    $ H = $ WH [. 1]; 
    $ W = min ($ W, $ H); 
    $ H = $ W; 
    $ IMG = as imagecreatetruecolor ($ W, $ H); 
    // this must have a 
    imagealphablending ($ IMG, to false); 

    imagesavealpha($img, true);
    // a fully transparent color pickup, 127 is the last parameter completely transparent 
    $ bg = imagecolorallocatealpha ($ img, 255, 255, 255, 127) ; 
    ImageFill (IMG $, 0, 0, $ BG); 
    $ $ = R & lt W / 2; // radius 
    $ y_x = $ r; // center X coordinate 
    $ y_y = $ r; // center point Y coordinate 
    for ($ X = 0; $ X <$ W; $ X ++) { 
        for ($ Y = 0; $ Y <$ H; $ Y ++) { 
            $ RGBColor = imagecolorat ($ src_img, $ X, $ Y); 
            IF (((($ X - $ R & lt) * ($ X - $ R & lt) + ($ Y - $ R & lt) * ($ Y - $ R & lt)) <( R & lt * R & lt $ $))) { 
                imagesetpixel (IMG $, $ X, Y $, $ RGBColor); 
            } 
        } 
    } 

    . avatar_path $ = "../public/uploads/" DATE ( 'Ymd') "/ Avatar. / "; 


    IF (! is_dir ($ avatar_path)) { 
        mkdir ($ avatar_path, 0777, to true); 
    } 
// echo '<pre>';
//    print_r($img);
//    exit;
  //  file_put_contents($avatar_path.'avatar'.date('Ymd').".jpg",$img);
    $filename = $avatar_path.'avatar'.date('Ymd').".png";
    header("content-type:image/png");
    imagepng($img, $filename, 5); //保存到本地
  /*  imagepng($img);
    imagedestroy($img);
    exit;*/
    return $filename;
}

  

Guess you like

Origin www.cnblogs.com/Ychao/p/12124054.html