图像上的PHP GD库图像

问题描述 投票:3回答:1

我正在研究一些允许用户为公司生成模拟礼品卡的代码。他们输入面额,名称和上传徽标,它将使用基本模板图像并将该信息放在其上。

直到徽标部分,我根据需要工作。

我使用imagecreatefrompng("template.png")创建了我的模板图像,并使用imagettftext()将文本放在所需的坐标中。

现在,当涉及到徽标时,我遇到了一些问题。

我首先尝试创建基本图像(模板+文本),然后使用imagecopy将徽标添加到新图像中,但我每次看起来都是基本图像,我从未看到徽标。

我试过硬编码图像的路径,但我仍然没有得到理想的结果。

  // If we have all of the post data
  if($denomination && $name && $pin && $defaultText){

    // Create our header to flush the image to browser
    header("Content-type: image/png");  
    header("Cache-Control: no-store, no-cache");  
    header('Content-Disposition: attachment; filename="GiftCard.png"');

    // Define our source image
    $image = imagecreatefrompng("card.png");

    // Preserve transparency
    imagealphablending($image, true);
    imagesavealpha($image, true);

    // Colors
    $backgroundColor = imagecolorallocate ($image, 255, 255, 255);
    $textColor = imagecolorallocate ($image, 255, 255,255);
    $font = 'arial.ttf';

    // Data
    $data = [
      'name' => [
        'x' => 30,
        'y' => 260,
        'size' => 12,
        'angle' => 0,
        'content' => $name,
      ],
      'pin' => [
        'x' => 175,
        'y' => 210,
        'size' => 18,
        'angle' => 0,
        'content' => $pin
      ],
      'denomination' => [
        'x' => 435,
        'y' => 45,
        'size' => 20,
        'angle' => 0,
        'content' => $denomination
      ],
      'defaultText' => [
        'x' => 30,
        'y' => 290,
        'size' => 12,
        'angle' => 0,
        'content' => $defaultText
      ]
    ];

    // Name
    imagettftext($image, $data['name']['size'], $data['name']['angle'], $data['name']['x'], $data['name']['y'], $textColor, $font, $data['name']['content']);
    // Pin
    imagettftext($image, $data['pin']['size'], $data['pin']['angle'], $data['pin']['x'], $data['pin']['y'], $textColor, $font, $data['pin']['content']);
    // Denomination
    imagettftext($image, $data['denomination']['size'], $data['denomination']['angle'], $data['denomination']['x'], $data['denomination']['y'], $textColor, $font, '$' . $data['denomination']['content']);
    // Default Text
    imagettftext($image, $data['defaultText']['size'], $data['defaultText']['angle'], $data['defaultText']['x'], $data['defaultText']['y'], $textColor, $font, $data['defaultText']['content']);

    // Create our base image with text
    $base = imagepng($image);

  // Do we need to place the logo?
  if($_FILES["logo"]["name"]){

      // Create our logo
      $logo = imagecreatefrompng($target_dir.$_FILES["logo"]["name"]);

      // Get current width/height of template
      list($top_width, $top_height) = getimagesize($base);

      // Get width/height of logo
      list($bottom_width, $bottom_height) = getimagesize($logo);

      // compute new width/height
      $new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
      $new_height = $top_height + $bottom_height;

      // create new image and merge
      $new = imagecreate($new_width, $new_height);
      imagecopy($new, $base, 0, 0, 0, 0, $top_width, $top_height);
      imagecopy($new, $logo, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);

      // Send image to browser
      $final = imagepng($new);

      echo $final;

      // Free up memory
      imagedestroy($final);  

    }else{

      echo $base;

      // Free up memory
      imagedestroy($base); 
    }

  }

什么东西都与我正在尝试的东西脱颖而出?简而言之,我试图将文本和图像添加到“模板”png图像上。想象一下,最好先将文本敲出来,然后将其用作徽标的新模板图像。

php image gd
1个回答
1
投票
// Create our base image with text
$base = imagepng($image);

imagepng返回一个布尔值并输出图像。所以此时您已将标题和工作PNG发送到浏览器。然后,您尝试在boolean上执行图像操作。这将生成警告消息。

你在这里重复错误:

    ...

    // Send image to browser
    $final = imagepng($new);

    echo $final;

    // Free up memory
    imagedestroy($final);  

} else {

    echo $base;

    // Free up memory
    imagedestroy($base); 
}

现在你要么输出两个PNG并回显一个布尔值,要么输出一个PNG并回显一个布尔值。无论如何,都会产生很多警告信息。

综上所述:

  1. 你不需要$base。而是使用$image
  2. 你不需要创建或回显$final,只需要调用imagepng($new);
  3. 不要在最后的echo $base;区块中使用else,请致电imagepng($image);

所有这些都应该从PHP错误日志文件中显而易见。

© www.soinside.com 2019 - 2024. All rights reserved.