php make the middle with their own definition of the two dimensional code picture method

  • 2020-12-21 17:59:14
  • OfStack

1. First, you must generate 2-d code. The specific 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['chl'] = $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. Then use an php file to draw the 2d code and your target image in the following code:

< ?php
 
$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, "a"); 
fwrite($fp2,$img); 
fclose($fp2); 
return $filename; 
} 
$source = GrabImage("http://localhost/QRCode/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) { 
//  Get the original 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); //  Calculate the scale  
if ($scale >= 1) { 
//  Larger than the original size no longer thumbnails  
$width = $srcWidth; 
$height = $srcHeight; 
} else { 
//  Thumbnail size  
$width = (int) ($srcWidth * $scale); 
$height = (int) ($srcHeight * $scale); 
} 
//  Load the artwork  
$createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type); 
$srcImg = $createFun($image); 
// Create thumbnails  
if ($type != 'gif' && function_exists('imagecreatetruecolor')) 
$thumbImg = imagecreatetruecolor($width, $height); 
else 
$thumbImg = imagecreate($width, $height); 
//  Copy the picture  
if (function_exists("ImageCopyResampled")) 
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); 
else 
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); 
if ('gif' == $type || 'png' == $type) { 
//imagealphablending($thumbImg, false);// Cancel the default color mixing mode  
//imagesavealpha($thumbImg,true);// Set save intact  alpha  The channel information  
$background_color = imagecolorallocate($thumbImg, 0, 255, 0); //  assigned 1 A green  
imagecolortransparent($thumbImg, $background_color); //  Set to transparent, and if you comment out the line, output the green image  
} 
//  right jpeg Graph sets interlaced scan  
if ('jpg' == $type || 'jpeg' == $type) 
imageinterlace($thumbImg, $interlace); 

//  Generate images  
$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 if 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; 
// Set up the image  
$sCreateFun = "imagecreatefrom" . $sInfo['type']; 
$sImage = $sCreateFun($source); 
$wCreateFun = "imagecreatefrom" . $wInfo['type']; 
$wImage = $wCreateFun($water); 
// Set the color mixing mode of the image  
imagealphablending($wImage, true); 
// Image location , The default is right align at the bottom right corner  
// $posY = $sInfo["height"] - $wInfo["height"]; 
// $posX = $sInfo["width"] - $wInfo["width"]; 
$posY = ($sInfo["height"] - $wInfo["height"])/2; 
$posX = ($sInfo["width"] - $wInfo["width"])/2; 
// Generate mixed image  
imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha); 
// The output image  
$ImageFun = 'Image' . $sInfo['type']; 
// If the save file name is not given, the default is the original image name  
if (!$savename) { 
$savename = $source; 
@unlink($source); 
} 
// Save the image  
$ImageFun($sImage, $savename); 
imagedestroy($sImage); 
} 
water($source,$water); 

The GrabImage () function in the above code is used to convert the file that generates the 2d code into an image. The next function is to handle the zoom of the image to add the target image to the 2 bits.

3, come to an entry file index. html code as follows:
 
<html> 
<head> 
<title> 
 You can define your own image in the middle 2 Dimension code generator  
</title> 
</head> 
<body style="margin:0px; padding:0px; font-family: Song typeface ; font-size:12px;"> 
<form action="<span style="font-size:18px;"><strong><span style="color:#FF0000;"> Notice the submitted URL</span></strong></span>" method="post"> 
<div style="width:500px; height:200px; background-color:#CCCCCC; margin:auto; border-width:1px; border-color:#000000;" align="center"> 
<h1 style="margin:0px; padding:20px; font-family: Song typeface ; font-size:12px;"> You can define your own image in the middle 2 Dimension code generator </h1> 
<table width="500" border="0"> 
<tr> 
<td width="250" height="40" align="center" valign="middle">2 Contents of dimension code generation: </td> 
<td width="250" height="40" align="center" valign="middle"> 
<label> 
<input type="text" name="content" value=""> 
</label> 
</td> 
</tr> 
<tr> 
<td width="250" height="40" align="center" valign="middle"> I hope to add my own picture address: </td> 
<td width="250" height="40" align="center" valign="middle"> 
<label> 
<input type="text" name="url" value=""> 
</label> 
</td> 
</tr> 
<tr> 
<td height="40" colspan="2" align="center" valign="middle"> 
<label> 
<input type="submit" name="Submit" value=" Generate what I want 2 D code "> 
</label> 
</td> 
</tr> 
</table> 

</div> 
</body> 
</html> 

Related articles: