How to create a custom image QR code in the middle of php production

Source: Internet
Author: User
This article describes how to use php to create a QR code with a custom image in the middle. For more information, see section 1. First, you must generate the QR code as follows:

The code is as follows:


Class QRCode {
Public $ w;
Public $ h;
Public $ s;
Function _ construct ($ w1, $ h1, $ s1 ){
$ This-> w = $ w1;
$ This-> h = $ h1;
$ This-> s = $ s1;
$ This-> outimgase ();
}
Function qrcode (){
$ Post_data = array ();
$ Post_data ['cht'] = 'QR ';
$ Post_data ['chs '] = $ this-> w. "x". $ this-> h;
$ Post_data ['check'] = $ this-> s;
$ Post_data ['choe'] = "UTF-8 ";
$ Url = "http://chart.apis.google.com/chart ";
$ Data_Array = array ();
Foreach ($ post_data as $ key => $ value)
{
$ Data_Array [] = $ key. '='. $ value;
}
$ Data = implode ("&", $ data_Array );
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );
Return $ result;
}
Function outimgase (){
Echo $ this-> qrcode ();
}
}
Header ("Content-type: image/png ");
$ T = new QRCode (300,300, "tianxin ");


2. use a php file to draw the QR code with your target image. the code is as follows:

The code is as follows:


$ Surl = $ _ POST ["url"];
Function GrabImage ($ url, $ filename = ""){
If ($ url = ""): return false; endif;
If ($ filename = ""){
$ Ext = strrchr ($ url ,".");
If ($ ext! = ". Gif" & $ ext! = ". Jpg"): return false; endif;
$ Filename = date ("dMYHis"). $ ext;
}
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img );
$ Fp2 = @ fopen ($ filename, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
}
$ Source = GrabImage ("http: // localhost/QRCode. php", "Myqrcode.png ");
$ Water = GrabImage ($ surl, "t.png ");
Function getImageInfo ($ img ){
$ ImageInfo = getimagesize ($ img );
If ($ imageInfo! = False ){
$ ImageType = strtolower (substr (image_type_to_extension ($ imageInfo [2]), 1 ));
$ ImageSize = filesize ($ img );
$ Info = array (
"Width" => $ imageInfo [0],
"Height" => $ imageInfo [1],
"Type" => $ imageType,
"Size" => $ imageSize,
"Mime" => $ imageInfo ['Mime ']
);
Return $ info;
} Else {
Return false;
}
}
Function thumb ($ image, $ thumbname, $ type = '', $ maxWidth = 200, $ maxHeight = 50, $ interlace = true ){
// Obtain the source image information
$ Info = getImageInfo ($ image );
If ($ info! = False ){
$ SrcWidth = $ info ['width'];
$ SrcHeight = $ info ['height'];
$ Type = empty ($ type )? $ Info ['type']: $ type;
$ Type = strtolower ($ type );
$ Interlace = $ interlace? 1: 0;
Unset ($ info );
$ Scale = min ($ maxWidth/$ srcWidth, $ maxHeight/$ srcHeight); // calculates the scaling ratio.
If ($ scale> = 1 ){
// No longer scale out when the source image size is exceeded
$ Width = $ srcWidth;
$ Height = $ srcHeight;
} Else {
// Thumbnail size
$ Width = (int) ($ srcWidth * $ scale );
$ Height = (int) ($ srcHeight * $ scale );
}
// Load the source image
$ CreateFun = 'imagecreatefrom '. ($ type = 'jpg '? 'Jpeg ': $ type );
$ SrcImg = $ createFun ($ image );
// Create a thumbnail
If ($ type! = 'GIF' & function_exists ('imagecreatetruecolor '))
$ ThumbImg = imagecreatetruecolor ($ width, $ height );
Else
$ ThumbImg = imagecreate ($ width, $ height );
// Copy the image
If (function_exists ("ImageCopyResampled "))
Imagecopyresampled ($ thumbImg, $ srcImg, 0, 0, 0, $ width, $ height, $ srcWidth, $ srcHeight );
Else
Imagecopyresized ($ thumbImg, $ srcImg, 0, 0, 0, $ width, $ height, $ srcWidth, $ srcHeight );
If ('GIF' = $ type | 'PNG '= $ type ){
// Imagealphablending ($ thumbImg, false); // cancel the default mixed color mode.
// Imagesavealpha ($ thumbImg, true); // Set to save the complete alpha channel information
$ Background_color = imagecolorallocate ($ thumbImg, 0,255, 0); // assign a Green
Imagecolortransparent ($ thumbImg, $ background_color); // Set it to transparent. if this line is commented out, a green image is output.
}
// Sets the line scan for jpeg images
If ('jpg '= $ type | 'jpeg' = $ type)
Imageinterlace ($ thumbImg, $ interlace );

// Generate an image
$ ImageFun = 'image'. ($ type = 'jpg '? 'Jpeg ': $ type );
$ ImageFun ($ thumbImg, $ thumbname );
Imagedestroy ($ thumbImg );
Imagedestroy ($ srcImg );
Return $ thumbname;
}
Return false;
}
Function water ($ source, $ thumb, $ savename = "", $ alpha = 100 ){
// Check whether the file exists
If (! File_exists ($ source) |! File_exists ($ thumb ))
Return false;
// Image information
$ SInfo = getImageInfo ($ source );
$ Water = thumb ($ thumb, "wy.jpg", "jpg", $ sInfo ["width"]/4, $ sInfo ["height"]/4 );
$ WInfo = getImageInfo ($ water );
// If the image is smaller than the watermark image, no image is generated.
If ($ sInfo ["width"] <$ wInfo ["width"] | $ sInfo ['height'] <$ wInfo ['height'])
Return false;
// Create an image
$ SCreateFun = "imagecreatefrom". $ sInfo ['type'];
$ SImage = $ sCreateFun ($ source );
$ WCreateFun = "imagecreatefrom". $ wInfo ['type'];
$ WImage = $ wCreateFun ($ water );
// Set the mixed color mode of the image
Imagealphablending ($ wImage, true );
// Image position, right-aligned by default
// $ PosY = $ sInfo ["height"]-$ wInfo ["height"];
// $ PosX = $ sInfo ["width"]-$ wInfo ["width"];
$ PosY = ($ sInfo ["height"]-$ wInfo ["height"])/2;
$ PosX = ($ sInfo ["width"]-$ wInfo ["width"])/2;
// Generate a hybrid image
Imagecopymerge ($ sImage, $ wImage, $ posX, $ posY, 0, 0, $ wInfo ['width'], $ wInfo ['height'], $ alpha );
// Output image
$ ImageFun = 'image'. $ sInfo ['type'];
// If no save file name is provided, the original image name is used by default.
If (! $ Savename ){
$ Savename = $ source;
@ Unlink ($ source );
}
// Save the image
$ ImageFun ($ sImage, $ savename );
Imagedestroy ($ sImage );
}
Water ($ source, $ water );


In the above code, the GrabImage () function is used to convert the file that generates the QR code into an image. the next function is to process the scaling of the image and add the target image to the two places.

The code for index.html is as follows:

The code is as follows:




<BR> You can customize the two-dimensional code generator for an image in the middle. <BR>




In the middle, you can customize the two-dimensional code generator for the image.













Content to be generated by the QR code:



You want to add your own image address:











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.