phpwordpressgdlibimagecreatefrompng

The image is not shown when merging two images in PHP GD library in wordpress


I got this code to work but after few weeks the code is not working ,i did try to see where the bug come from but i have no idea. I did create image with GD library and it is created successfully but when i want to merge it with another picture it show only black screen.Any idea of how to solve the issue please .Here is my code: Please note that the image is created and saved successfully,so there is no problem with create_image() function.

function create_image()
{
    $cer_id = get_the_ID();
    $cer_org_nummer = get_field('orgnummer_dqm');
    $cer_today = date('Y-m-d');
    $cer_org_name = get_field('foretags_namn_dqm');
    $cer_org_date = $cer_org_nummer . ' | ' . $cer_today;


    $im = @imagecreate(500, 450);
    $yellow = imagecolorallocate($im, 255, 255, 255);  // yellow
    imagecolortransparent($im, $yellow);

    $black = imagecolorallocate($im, 252, 195, 91);
    $string = "Digital Quality Managment";
    $font = FUNC_PLUGIN_D_DQM . '/font/Roboto-Regular.ttf';// black

    /*    imagestring($im, $font, 50, 300, $string, $black);*/
    /*    imagestring($im, $font, 3, 300, $cer_org_name, $black);*/
    /*    imagestring($im, $font, 0, 10, $cer_org_date, $black);*/
    imagettftext($im, 35, 0, 60, 270, $black, $font, 'CERTIFIED');

    imagettftext($im, 25, 0, 0, 320, $black, $font, $cer_org_date);

    imagepng($im, "wp-content/plugins/certificates/cer_images/image_$cer_id.png");
    imagedestroy($im);
}
      create_image();
    $image1 = "wp-content/plugins/certificates/cer_images/dqm_png_logo.png";
    $image2 = "wp-content/plugins/certificates/cer_images/image_$cer_id.png";

    list($width, $height) = getimagesize($image2);


    $image1 = imagecreatefromstring(file_get_contents($image1));

    $image2 = imagecreatefromstring(file_get_contents($image2));


    imagealphablending($image1, FALSE);
    imagesavealpha($image1, TRUE);
    imagecopymerge($image1, $image2, 110, 50, 0, 0, $width, $height, 100);


    header("Content-type: image/png");
    imagepng($image1);
    imagedestroy($image1);

Solution

  • please try the code below

    This code works for me..

    $pro_directory_path = get_attached_file( get_post_thumbnail_id($_REQUEST['apid']),'full' );
    
    $mainmetadata = wp_get_attachment_metadata( get_post_thumbnail_id( $_REQUEST['apid'] ) );
    
    $pro_img_name = $mainmetadata['file'];
    
    $pro_img_name_extra = '1'.$mainmetadata['file'];
    
    // $val_id is id of image(bigger image/background Image)
    $directory_path = get_attached_file( $val_id,'full' );
    
    $metadata = wp_get_attachment_metadata( $val_id );
    
    $name = $metadata['file'];
    
    $bgFile = $directory_path;
    
    // We want our final image to be 76x76 size
    //user $metadata to get ordiginal height width of image
    // $value contain height width of wall mentioned by admin
    $x = $metadata['width'];
    $y = $metadata['height'];
    
    $x_inch = $metadata['width']/96;
    $y_inch = $metadata['height']/96;
    
    $width_proption = $wall_w_inch/$x_inch;
    $height_proption = $wall_h_inch/$y_inch;
    
    $img_pos_x = ($x/2)-($resizedwidth/2);
    $img_pos_y = ($y/2)-($resizedheight);
    
    /* Code to generate custom wall images - STart */
    $inn_width = ($innerimg_width / $width_proption) * 12 ;
    $inn_height = ($innerimg_height / $height_proption) * 12 ;
    
    $resizedFilename = get_template_directory().'/woocommerce/temp_images/cust_'.$pro_img_name;
    
    $img_url_extra =  get_stylesheet_directory_uri().'/woocommerce/temp_images/cust_'.$pro_img_name;
    
    //function to resize Artist product image in desired size's (Variation sizes).
    $imgData = resize_image($pro_directory_path, $inn_width, $inn_height);
    imagepng($imgData, $resizedFilename);
    
    list($resizedwidth, $resizedheight) = getimagesize($resizedFilename);
    
    /* Code to generate custom wall images - ENDS */
    
    $imageFile = $resizedFilename;
    
    // dimensions of the final image
    $final_img = imagecreatetruecolor($x, $y);
    
    // Create our image resources from the files
    $image_1 = imagecreatefrompng($bgFile);
    $image_2 = imagecreatefrompng($imageFile);
    
    // Enable blend mode and save full alpha channel
    imagealphablending($final_img, true);
    imagesavealpha($final_img, true);
    
    // Copy our image onto our $final_img
    imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
    imagecopy($final_img, $image_2, $img_pos_x, $img_pos_y, 0, 0, $resizedwidth, $resizedheight);
    
    ob_start();
    $path =  __DIR__ .'/custom_wall_img/'.$name;
    imagepng($final_img,$path);
    $watermarkedImg = ob_get_contents(); // Capture the output
    ob_end_clean(); // Clear the output buffer