Advertisement
CodeDropz

Combine Image a & Image b - Custom Code

Oct 31st, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.60 KB | None | 0 0
  1. global $final_output_image;
  2. global $cf7_upload_fonts;
  3.  
  4. // NOTE: Copy and paste url of the fonts from wordpress media library.
  5. $cf7_upload_fonts = 'http://localhost/codedropz/wp-content/uploads/2024/10/arial.ttf';
  6.  
  7. add_action( 'dnd_cf7_mail_setup_data', 'cf7_combined_images', 100, 1 );
  8. add_filter( 'wpcf7_mail_components', 'cf7_dnd_attach_files', 10, 3 );
  9.  
  10. add_filter('upload_mimes', function( $mimes ){
  11.     $mimes['ttf'] = 'application/x-font-ttf';
  12.     return $mimes;
  13. });
  14.  
  15. function cf7_combined_images( $wpcf7 ) {
  16.     global $final_output_image;
  17.    
  18.     $codedropz  = CodeDropz_Drag_Drop_Upload_CF7::get_instance();
  19.     $upload_dir = wp_upload_dir();
  20.    
  21.     // NOTE: Change this to your cf7 file upload name like: upload-file-a, upload-file-b
  22.     $imageA     = 'upload-file-a';
  23.     $imageB     = 'upload-file-b';
  24.    
  25.     if( $codedropz ) {
  26.         $files = $codedropz->get_posted_data();
  27.        
  28.         // Image A
  29.         if ( isset( $files[ $imageA ] ) ) {
  30.             $image1 = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], reset( $files[ $imageA ] ) );
  31.         }
  32.        
  33.         // Image B
  34.         if ( isset( $files[ $imageB ] ) ) {
  35.             $image2 = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], reset( $files[ $imageB ] ) );
  36.         }
  37.        
  38.         // Save Fianal Image.
  39.         if ( $image1 && $image2 ) {
  40.             $nameA = pathinfo( wp_basename( $image1 ) );
  41.             $nameB = pathinfo( wp_basename( $image2 ) );
  42.             $final_name = wp_unique_filename( $upload_dir['path'], '/Final_'. $nameA['filename'] .'_'. $nameB['filename'] .'.jpg' );
  43.             $final_output_image = $upload_dir['path'] .'/'. $final_name;
  44.             mergeImagesWithText( $image1, $image2, $final_output_image );
  45.         }
  46.        
  47.         // Remove dnd mail components.
  48.         remove_filter( 'wpcf7_mail_components', array( $codedropz, 'dnd_cf7_mail_components' ), 500, 2 );
  49.        
  50.     }
  51. }
  52.  
  53. // Attach files to email
  54. function cf7_dnd_attach_files( $components, $form, $instance ) {
  55.     global $final_output_image;
  56.    
  57.     $template_name = $instance->get_current_template_name();
  58.     $mail          = $form->prop( $template_name );
  59.     $upload_dir    = wp_upload_dir();
  60.    
  61.     if ( isset( $mail['active'] ) && isset( $mail['attachments'] ) ) {
  62.         if( false !== strpos( $mail['attachments'], '[before_and_after]' ) ) {
  63.             $components['attachments'][] = $final_output_image;
  64.         }
  65.     }
  66.    
  67.     return $components;
  68. }
  69.  
  70. function mergeImagesWithText($imageAPath, $imageBPath, $outputPath) {
  71.     global $cf7_upload_fonts;
  72.  
  73.     $upload_dir = wp_upload_dir();
  74.  
  75.     // Get file extensions.
  76.     $imageAext = pathinfo($imageAPath);
  77.     $imageBext = pathinfo($imageBPath);
  78.  
  79.     // Load Images
  80.     $imageA = ($imageAext['extension'] == 'png') ? imagecreatefrompng($imageAPath) : imagecreatefromjpeg($imageAPath);
  81.     $imageB = ($imageBext['extension'] == 'png') ? imagecreatefrompng($imageBPath) : imagecreatefromjpeg($imageBPath);
  82.  
  83.     // Image A (Width & Height)
  84.     $widthA = imagesx($imageA);
  85.     $heightA = imagesy($imageA);
  86.    
  87.     // Image B (Width & Height)
  88.     $widthB = imagesx($imageB);
  89.     $heightB = imagesy($imageB);
  90.  
  91.     // Create a new image with combined height and added space
  92.     $mergedImage = imagecreatetruecolor(max($widthA, $widthB), $heightA + $heightB + 160);
  93.  
  94.     // Fill background with white
  95.     $white = imagecolorallocate($mergedImage, 255, 255, 255);
  96.     imagefill($mergedImage, 0, 0, $white);
  97.  
  98.     // Copy images onto the merged image
  99.     imagecopy($mergedImage, $imageA, 0, 70, 0, 0, $widthA, $heightA);
  100.     imagecopy($mergedImage, $imageB, 0, $heightA + 140, 0, 0, $widthB, $heightB);
  101.  
  102.     // Define colors for text and backgrounds
  103.     $textColorA = imagecolorallocate($mergedImage, 255, 255, 255); // White for text
  104.     $textColorB = imagecolorallocate($mergedImage, 0, 0, 0); // Black for text
  105.    
  106.     $blueBg = imagecolorallocate($mergedImage, 0, 0, 255);  // Blue for "Before"
  107.     $yellowBg = imagecolorallocate($mergedImage, 255, 255, 0); // Yellow for "After"
  108.    
  109.     $fontSize = 12; // font size.
  110.     $fontFile = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $cf7_upload_fonts);
  111.  
  112.     // Calculate text bounding box for centering
  113.     $beforeText = 'Before';
  114.     $afterText = 'After';
  115.    
  116.     $beforeBox = imagettfbbox($fontSize, 0, $fontFile, 'Before');
  117.     $afterBox = imagettfbbox($fontSize, 0, $fontFile, 'After');
  118.  
  119.     $beforeTextWidth = $beforeBox[2] - $beforeBox[0];
  120.     $afterTextWidth = $afterBox[2] - $afterBox[0];
  121.  
  122.     // Center text X position
  123.     $centerXBefore = (imagesx($mergedImage) - $beforeTextWidth) / 2;
  124.     $centerXAfter = (imagesx($mergedImage) - $afterTextWidth) / 2;
  125.  
  126.     // Define padding around text inside the rectangle
  127.     $padding = 15;  // Padding around the text
  128.  
  129.     // Draw background rectangles for the text with increased width and padding, moved up by 10px
  130.     imagefilledrectangle($mergedImage, $centerXBefore - $padding - 10, 20, $centerXBefore + $beforeTextWidth + $padding + 10, 55, $blueBg);
  131.     imagefilledrectangle($mergedImage, $centerXAfter - $padding - 10, $heightA + 90, $centerXAfter + $afterTextWidth + $padding + 10, $heightA + 125, $yellowBg);
  132.  
  133.     // Draw text on top of backgrounds with padding, moved up by 10px
  134.     imagettftext($mergedImage, $fontSize, 0, $centerXBefore, 45, $textColorA, $fontFile, $beforeText);
  135.     imagettftext($mergedImage, $fontSize, 0, $centerXAfter, $heightA + 115, $textColorB, $fontFile, $afterText);
  136.  
  137.     // Save the merged image
  138.     imagejpeg($mergedImage, $outputPath);
  139.  
  140.     // Free memory
  141.     imagedestroy($imageA);
  142.     imagedestroy($imageB);
  143.     imagedestroy($mergedImage);
  144.  
  145.     // Delete the original images
  146.     if (file_exists($imageAPath)) {
  147.         unlink($imageAPath);
  148.     }
  149.    
  150.     if (file_exists($imageBPath)) {
  151.         unlink($imageBPath);
  152.     }
  153. }
  154.  
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement