PHP uses Imagick extension to realize picture synthesis and fillet processing function example

  • 2021-12-21 04:22:03
  • OfStack

In this paper, an example of PHP using Imagick extension to achieve picture synthesis, fillet processing functions. Share it for your reference, as follows:

Requirements: Generate a specific 2D code for the user, pull the user's WeChat avatar and a specific background picture to synthesize a user-specific poster

Methods: The Imagick extension function of PHP was used to synthesize the pictures. Fillet the WeChat avatar and then compress the quality of the picture

1. Generate an exclusive 2D code according to the specific id of WeChat users


public static function getTicket($scene_id)
{
  $qrcode = '{"expire_seconds": 2592000, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": $scene_id }}}'; //2 Dimension code information 
  $access_token = self::getToken();  // WeChat official account token This one wants to get his own WeChat official account 
  $getticket_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token";
  $ticketinfo = self::request_by_curl($getticket_url,$qrcode);
  return $ticketinfo['ticket']; // Exclusive 2 Dimensional code ticken
}
public static function request_by_curl($remote_server, $post_string='')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: "));
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    $content = curl_exec($ch);
    curl_close($ch);
    $reArr=json_decode($content,true);
    return $reArr;
}

2. Composite posters


public function CompositeImage ($ticket, $wxnick, $userId)
{
  $Qrcode = new Imagick("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=$ticket");
  $Qrcode->setImageResolution(0.1,0.3);   // Set the resolution of the picture 
  $QrcodeWH = $Qrcode->getImageGeometry();  // Get the width and height of the source picture 
  if ($QrcodeWH['width']>200) {
   $QrcodeW['width'] = 200;
   $QrcodeH['height'] = $QrcodeW['width']/$QrcodeWH['width']*$QrcodeWH['height'];
  } else {
   $QrcodeW['width'] = $QrcodeWH['width'];
   $QrcodeH['height'] = $QrcodeWH['height'];
  }
   $Qrcode->thumbnailImage( $QrcodeW['width'], $QrcodeWH['height'], true ); // Zoom to the selected scale 
  //  Download the WeChat avatar in advance and then generate composite information 
   $curl  = curl_init($wxnick);
   $wxnickpath = "upload/wxnick/".$userId.".jpg";
   curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
   $imageData = curl_exec($curl);
   curl_close($curl);
   $tp = @fopen($wxnickpath, 'a');
   fwrite($tp, $imageData);
   fclose($tp);
   $weixin = new Imagick($wxnickpath);
   $weixin->setImageResolution(0.1,0.3);
   $weixin->roundCorners(360,360);   // Fillet treatment 
   $wxWH = $weixin->getImageGeometry();
   if ($wxWH['width']>200) {
 $wxW['width'] = 200;
 $wxH['height'] = $wxW['width']/$wxWH['width']*$wxWH['height'];
   } else {
 $wxW['width'] = $wxWH['width'];
 $wxH['height'] = $wxWH['height'];
   }
   $weixin->thumbnailImage( $wxW['width'], $wxWH['height'], true );// Equal scaling 
   // Create 1 A Imagick Object and get the background image to process  /data/wenda/htdocs/upload
 $poster = new Imagick( "/data/wenda/htdocs/upload/poster.png" );
 $posterWH = $poster->getImageGeometry();
 $posterW['width'] = $posterWH['width'];
 $posterH['height'] = $posterWH['height'];
 //  Create at thumbnail size 1 A colored picture 
 $canvas = new Imagick();
 $canvas->newImage( $posterW['width'], $posterH['height'], 'black', 'jpg' );
 //2 Dimension code   WeChat avatar   Background   Synthesis 
 $poster->compositeImage($Qrcode,Imagick::COMPOSITE_OVER,275,960);
 $poster->compositeImage($weixin,Imagick::COMPOSITE_OVER,275,402);
 $canvas->compositeImage( $poster, imagick::COMPOSITE_OVER, 0, 0);
 $canvas->setImageCompressionQuality(60); // Compression quality 
 $canvas->writeImage( "/upload/poster/$userId.jpg" ); // Generate a picture 
 return $canvas; // Returns picture stream information 
}
header( "Content-Type: image/jpg" );  // Output picture 
$posterimg = $this->CompositeImage($Fticket, $Fwnick, $userId);
echo $posterimg // Output picture 

For more readers interested in PHP related contents, please check the topics of this site: "PHP Extended Development Tutorial", "PHP Network Programming Skills Summary", "php curl Usage Summary", "PHP Array (Array) Operation Skills Complete Book", "PHP Graphics and Pictures Operation Skills Summary" and "php String (string) Usage Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: