OBJUI

PHP合成图片

2024-04-25 16:57:52 78

/**
     * 生成带二维码海报
     */
    public static function createPoster($code)
    {
        try{
            $qrcode  = self::createQrcode($code);
            $qrcodeUrl = app()->getRootPath().'data/runtime/temp/'.uniqid().'.png';  
            file_put_contents($qrcodeUrl, file_get_contents($qrcode));  
    
    
            $bg_url = app()->getRootPath() . 'public/static/images/bg.jpg';
    
            // 创建第一张图片的资源  
            $backgroundImage  = imagecreatefromjpeg($bg_url);   //w279 h1108
            
            // 创建第二张图片的资源  
            $qrCodeImage  = imagecreatefromjpeg($qrcodeUrl);  
            
            // 获取海报背景的宽度和高度  
            $backgroundWidth = imagesx($backgroundImage);  
            $backgroundHeight = imagesy($backgroundImage);  
            
            // 获取二维码的宽度和高度  
            $qrCodeWidth = 180;  
            $qrCodeHeight = 180;  
            
            // 计算二维码的位置,使其靠页脚放置  
            $qrCodeX = ($backgroundWidth - $qrCodeWidth) / 2; // 根据需要调整位置  
            $qrCodeY = ($backgroundHeight - $qrCodeHeight) - 146; // 根据需要调整位置  
    
    
            // 创建缩放后的二维码图像资源  
            $scaleFactor = 180 / imagesy($qrCodeImage);
            // 计算缩放后的二维码尺寸  
            $scaledWidth = imagesx($qrCodeImage) * $scaleFactor;  
            $scaledHeight = imagesy($qrCodeImage) * $scaleFactor;  
    
            $scaledQRCodeImage = imagecreatetruecolor($scaledWidth, $scaledHeight);  
            imagecopyresampled(  
                $scaledQRCodeImage, // 目标图像资源  
                $qrCodeImage, // 源图像文件路径  
                0, // 目标图像的 x 坐标  
                0, // 目标图像的 y 坐标  
                0, // 源图像的 x 坐标  
                0, // 源图像的 y 坐标  
                $scaledWidth, // 目标图像的宽度  
                $scaledHeight, // 目标图像的高度  
                imagesx($qrCodeImage), // 源图像的宽度  
                imagesy($qrCodeImage) // 源图像的高度  
            );  
    
            
            // 将二维码复制到海报背景上  
            imagecopy($backgroundImage, $scaledQRCodeImage, $qrCodeX, $qrCodeY, 0, 0, $scaledWidth, $scaledHeight);  
            
            // 输出合成后的海报图像  
            $outputImagePath = app()->getRootPath().'data/runtime/temp/'.uniqid().'.jpg'; // 输出图像的保存路径  
            imagejpeg($backgroundImage, $outputImagePath); 

            //直接浏览器输出图片
            // header('Content-Type: image/png');  
            //imagepng($backgroundImage); 
            
            // 释放资源  
            imagedestroy($backgroundImage);  
            imagedestroy($qrCodeImage);   
    
            return $outputImagePath;
        }catch(\Exception $e){
            Log::channel('wx')->info($e->getMessage());
            return '';
        }
        
    }
function $initHighlight(block, cls) {
  try {
    if (cls.search(/\bno\-highlight\b/) != -1)
      return process(block, true, 0x0F) +
             ` class="${cls}"`;
  } catch (e) {
    /* handle exception */
  }
  for (var i = 0 / 2; i < classes.length; i++) {
    if (checkCondition(classes[i]) === undefined)
      console.log('undefined');
  }
}

export  $initHighlight;




更多精彩,请关注公众号

微信公众号

评论:
热门文章: