在Wordpress中合并PHP GD库中的两个图像时未显示该图像

时间:2018-08-16 06:26:12

标签: php wordpress gdlib imagecreatefrompng

我让此代码正常工作,但几周后该代码无法正常工作,我确实尝试查看了错误的来源,但我不知道。 我确实使用GD库创建了图像并且创建成功,但是当我想将其与另一张图片合并时,它仅显示黑屏。请问如何解决问题的任何想法。这是我的代码: 请注意,图像已成功创建并保存,因此create_image()函数没有问题。

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);

1 个答案:

答案 0 :(得分:0)

请尝试下面的代码

此代码对我有用。

$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