Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global $final_output_image;
- global $cf7_upload_fonts;
- // NOTE: Copy and paste url of the fonts from wordpress media library.
- $cf7_upload_fonts = 'http://localhost/codedropz/wp-content/uploads/2024/10/arial.ttf';
- add_action( 'dnd_cf7_mail_setup_data', 'cf7_combined_images', 100, 1 );
- add_filter( 'wpcf7_mail_components', 'cf7_dnd_attach_files', 10, 3 );
- add_filter('upload_mimes', function( $mimes ){
- $mimes['ttf'] = 'application/x-font-ttf';
- return $mimes;
- });
- function cf7_combined_images( $wpcf7 ) {
- global $final_output_image;
- $codedropz = CodeDropz_Drag_Drop_Upload_CF7::get_instance();
- $upload_dir = wp_upload_dir();
- // NOTE: Change this to your cf7 file upload name like: upload-file-a, upload-file-b
- $imageA = 'upload-file-a';
- $imageB = 'upload-file-b';
- if( $codedropz ) {
- $files = $codedropz->get_posted_data();
- // Image A
- if ( isset( $files[ $imageA ] ) ) {
- $image1 = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], reset( $files[ $imageA ] ) );
- }
- // Image B
- if ( isset( $files[ $imageB ] ) ) {
- $image2 = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], reset( $files[ $imageB ] ) );
- }
- // Save Fianal Image.
- if ( $image1 && $image2 ) {
- $nameA = pathinfo( wp_basename( $image1 ) );
- $nameB = pathinfo( wp_basename( $image2 ) );
- $final_name = wp_unique_filename( $upload_dir['path'], '/Final_'. $nameA['filename'] .'_'. $nameB['filename'] .'.jpg' );
- $final_output_image = $upload_dir['path'] .'/'. $final_name;
- mergeImagesWithText( $image1, $image2, $final_output_image );
- }
- // Remove dnd mail components.
- remove_filter( 'wpcf7_mail_components', array( $codedropz, 'dnd_cf7_mail_components' ), 500, 2 );
- }
- }
- // Attach files to email
- function cf7_dnd_attach_files( $components, $form, $instance ) {
- global $final_output_image;
- $template_name = $instance->get_current_template_name();
- $mail = $form->prop( $template_name );
- $upload_dir = wp_upload_dir();
- if ( isset( $mail['active'] ) && isset( $mail['attachments'] ) ) {
- if( false !== strpos( $mail['attachments'], '[before_and_after]' ) ) {
- $components['attachments'][] = $final_output_image;
- }
- }
- return $components;
- }
- function mergeImagesWithText($imageAPath, $imageBPath, $outputPath) {
- global $cf7_upload_fonts;
- $upload_dir = wp_upload_dir();
- // Get file extensions.
- $imageAext = pathinfo($imageAPath);
- $imageBext = pathinfo($imageBPath);
- // Load Images
- $imageA = ($imageAext['extension'] == 'png') ? imagecreatefrompng($imageAPath) : imagecreatefromjpeg($imageAPath);
- $imageB = ($imageBext['extension'] == 'png') ? imagecreatefrompng($imageBPath) : imagecreatefromjpeg($imageBPath);
- // Image A (Width & Height)
- $widthA = imagesx($imageA);
- $heightA = imagesy($imageA);
- // Image B (Width & Height)
- $widthB = imagesx($imageB);
- $heightB = imagesy($imageB);
- // Create a new image with combined height and added space
- $mergedImage = imagecreatetruecolor(max($widthA, $widthB), $heightA + $heightB + 160);
- // Fill background with white
- $white = imagecolorallocate($mergedImage, 255, 255, 255);
- imagefill($mergedImage, 0, 0, $white);
- // Copy images onto the merged image
- imagecopy($mergedImage, $imageA, 0, 70, 0, 0, $widthA, $heightA);
- imagecopy($mergedImage, $imageB, 0, $heightA + 140, 0, 0, $widthB, $heightB);
- // Define colors for text and backgrounds
- $textColorA = imagecolorallocate($mergedImage, 255, 255, 255); // White for text
- $textColorB = imagecolorallocate($mergedImage, 0, 0, 0); // Black for text
- $blueBg = imagecolorallocate($mergedImage, 0, 0, 255); // Blue for "Before"
- $yellowBg = imagecolorallocate($mergedImage, 255, 255, 0); // Yellow for "After"
- $fontSize = 12; // font size.
- $fontFile = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $cf7_upload_fonts);
- // Calculate text bounding box for centering
- $beforeText = 'Before';
- $afterText = 'After';
- $beforeBox = imagettfbbox($fontSize, 0, $fontFile, 'Before');
- $afterBox = imagettfbbox($fontSize, 0, $fontFile, 'After');
- $beforeTextWidth = $beforeBox[2] - $beforeBox[0];
- $afterTextWidth = $afterBox[2] - $afterBox[0];
- // Center text X position
- $centerXBefore = (imagesx($mergedImage) - $beforeTextWidth) / 2;
- $centerXAfter = (imagesx($mergedImage) - $afterTextWidth) / 2;
- // Define padding around text inside the rectangle
- $padding = 15; // Padding around the text
- // Draw background rectangles for the text with increased width and padding, moved up by 10px
- imagefilledrectangle($mergedImage, $centerXBefore - $padding - 10, 20, $centerXBefore + $beforeTextWidth + $padding + 10, 55, $blueBg);
- imagefilledrectangle($mergedImage, $centerXAfter - $padding - 10, $heightA + 90, $centerXAfter + $afterTextWidth + $padding + 10, $heightA + 125, $yellowBg);
- // Draw text on top of backgrounds with padding, moved up by 10px
- imagettftext($mergedImage, $fontSize, 0, $centerXBefore, 45, $textColorA, $fontFile, $beforeText);
- imagettftext($mergedImage, $fontSize, 0, $centerXAfter, $heightA + 115, $textColorB, $fontFile, $afterText);
- // Save the merged image
- imagejpeg($mergedImage, $outputPath);
- // Free memory
- imagedestroy($imageA);
- imagedestroy($imageB);
- imagedestroy($mergedImage);
- // Delete the original images
- if (file_exists($imageAPath)) {
- unlink($imageAPath);
- }
- if (file_exists($imageBPath)) {
- unlink($imageBPath);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement