PHP implementation of the way to generate promotional posters

Source: Internet
Author: User
Tags imagejpeg

Often there is the need to generate promotional posters, including the specified QR code, to share someone else to scan the code to determine the user referral relationship.
Careful analysis, the necessary elements of the promotion poster is the poster background and QR code, both of which are easy to generate, but to combine the two together into a picture two-dimensional and can be saved to the local easy to share out, this is the difficulty, in the H5 can use canvas to complete similar functions, But in the small program inside a lot of limitations. Then we generate the poster directly in the background, the foreground calls directly.

Pre-Preparation:
1. Poster background, background map General storage server, program local read;
2. Promote two-dimensional code, can be two-dimensional code image link, can also be a string image stream, if you generate two-dimensional code, see: Using Phpqrcode to generate two-dimensional code.
Here's how:
/**

  • Generate promotional Posters
  • @param the array parameter, including pictures and text
  • @param string $filename generate poster file name, do not pass this parameter does not produce the file, output the picture directly
  • @return [Type] [description]
    */
    function Createposter ($config =array (), $filename = "") {
    If you want to read the newspaper what is wrong, you can first note the header
    if (empty ($filename)) header ("Content-type:image/png");
    $imageDefault = Array (
    ' Left ' =>0,
    ' Top ' =>0,
    ' Right ' =>0,
    ' Bottom ' =>0,
    ' Width ' =>100,
    ' Height ' =>100,
    ' Opacity ' =>100
    );
    $textDefault = Array (
    ' Text ' = ',
    ' Left ' =>0,
    ' Top ' =>0,
    ' FontSize ' =>32,//font size
    ' FontColor ' = ' 255,255,255 ',//Font Color
    ' Angle ' =>0,
    );
    $background = $config [' Background '];//poster at the bottom of the background
    Background method
    $backgroundInfo = getimagesize ($background);
    $backgroundFun = ' Imagecreatefrom '. Image_type_to_extension ($backgroundInfo [2], false);
    $background = $backgroundFun ($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));
    Processed the picture
    if (!empty ($config [' image ')]) {
    foreach ($config [' image '] as $key = + $val) {
    $val = Array_merge ($imageDefault, $val);
    $info = getimagesize ($val [' url ']);
    $function = ' Imagecreatefrom '. Image_type_to_extension ($info [2], false);
    if ($val [' stream ']) {//If a string image stream is passed
    $info = getimagesizefromstring ($val [' url ']);
    $function = ' imagecreatefromstring ';
    }
    $res = $function ($val [' url ']);
    $resWidth = $info [0];
    $resHeight = $info [1];
    Build artboards, zoom pictures to specified dimensions
    $canvas =imagecreatetruecolor ($val [' width '], $val [' height '];
    Imagefill ($canvas, 0, 0, $color);
    Key functions, parameters (target resource, source, start coordinate of target resource x, y, start coordinate of source resource x, y, Width of target resource w,h, Width height of source resource w,h)
    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 '];
    Placing images
    Imagecopymerge ($imageRes, $canvas, $val [' left '], $val [' top '], $val [' right '], $val [' Bottom '], $val [' width '], $val [' Height '], $val [' opacity ']);//left, top, right, bottom, width, height, transparency
    }
    }
    Working with text
    if (!empty ($config [' text '])) {
    foreach ($config [' text '] as $key = + $val) {
    $val = Array_merge ($textDefault, $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 '];
    Imagettftext ($imageRes, $val [' fontSize '], $val [' angle '], $val [' left '], $val [' top '], $fontColor, $val [' Fontpath '],$ val[' text ');
    }
    }
    Create a picture
    if (!empty ($filename)) {
    $res = imagejpeg ($imageRes, $filename, 90); Save to Local
    Imagedestroy ($imageRes);
    if (! $res) return false;
    return $filename;
    }else{
    Imagejpeg ($imageRes); Display on the browser
    Imagedestroy ($imageRes);
    }
    }

Use example one: Generate a poster with a two-dimensional code
2. Add logo to generated QR code (create picture file)
function scerweima1 ($url = ") {
Require_once ' phpqrcode.php ';
$value = $url; Two-dimensional code content
$errorCorrectionLevel = ' H '; Fault tolerance level
$matrixPointSize = 6; Generate picture size
Generate two-dimensional code images
$filename = ' qrcode/'. Microtime (). PNG ';
QRCode::p ng ($value, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
$logo = ' qrcode/logo.jpg '; Ready logo image
$QR = $filename; Original two-dimensional code diagram that has been generated
if (file_exists ($logo)) {
$QR = imagecreatefromstring (file_get_contents ($QR)); Target Image connection resource.
$logo = imagecreatefromstring (file_get_contents ($logo)); Source Image Connection resource.
$QR _width = Imagesx ($QR); Two-dimensional code image width
$QR _height = Imagesy ($QR); Two-dimensional code image height
$logo _width = Imagesx ($logo); Logo image width
$logo _height = Imagesy ($logo); Logo image Height
$logo _qr_width = $QR _WIDTH/4; The width of the logo after the combination (1/5 of the QR code)
$scale = $logo _width/$logo _qr_width; The width of the logo (width/width of the combination)
$logo _qr_height = $logo _height/$scale; The height of the logo after the combination
$from _width = ($QR _width-$logo _qr_width)/2; The coordinate point of the upper left corner of the logo after combining
Regroup and resize pictures
/*

    • Imagecopyresampled () copies a square area of an image (source image) into another image
      */
      Imagecopyresampled ($QR, $logo, $from _width, $from _width, 0, 0, $logo _qr_width, $logo _qr_height, $logo _width, $logo _ height);
      }
      Output picture
      Imagepng ($QR, ' qrcode.png ');
      Imagedestroy ($QR);
      Imagedestroy ($logo);
      Return ' }
      Invoke View Results
      echo scerweima1 (' https://www.baidu.com ');

Use example two: Generate posters with images, nicknames and QR codes
$config = Array (
' Text ' =>array (
Array
' Text ' = ' nickname ',
' Left ' =>182,
' Top ' =>105,
' Fontpath ' = ' qrcode/simhei.ttf ',//font file
' FontSize ' =>18,//font size
' FontColor ' = ' 255,0,0 ',//Font Color
' Angle ' =>0,
)
),
' Image ' =>array (
Array
' url ' = ' qrcode/qrcode.png ',//Picture resource Path
' Left ' =>130,
' Top ' =>-140,
' Stream ' =>0,//Picture resource is a string image stream
' Right ' =>0,
' Bottom ' =>0,
' Width ' =>150,
' Height ' =>150,
' Opacity ' =>100
),
Array
' url ' = = ' https://wx.qlogo.cn/mmopen/vi_32/ Dyaiogq83eofd96opk97rxwm179g9ijytigqxod8jh9icff6cia6sj0fxeillmlf0dvviaf3snibxtrfavo3c8ria2w/0 ',
' Left ' =>120,
' Top ' =>70,
' Right ' =>0,
' Stream ' =>0,
' Bottom ' =>0,
' Width ' =>55,
' Height ' =>55,
' Opacity ' =>100
),
),
' Background ' = ' qrcode/bjim.jpg ',
);
$filename = ' qrcode/'. Time (). JPG ';
Echo Createposter ($config, $filename);
echo Createposter ($config);

PHP implementation of the way to generate promotional posters

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.