TCPDF:在几个图像之间设置边距,并通过' foreach'方法

时间:2015-09-12 09:07:38

标签: php foreach tcpdf

我正在使用TCPDF,我试图通过foreach方法将行和列设置为在pdf图像上进行排版。 这是我的代码:

$imagesValues = get_post_meta( $post->ID, 'image');
foreach ( $imagesValues as $imageItem ) { 
    foreach ( $imageItem as $imageID ) { 
        $imageURL = wp_get_attachment_url($imageID); // gets photo URL
        $pdf->Image($gallerieURL, $x, $y, $w, $h, 'JPG', '', '', false, 300, '', false, false, 0, false, false, false); 
    } 
}

修改

我做到了:

$x = 115;
$y = 35;
$w = 25;
$h = 50;
foreach ( $imagesValues as $imageItem) { 
  foreach ( $imageItem as $imageID) { 
    $imageURL = wp_get_attachment_url($imageID); // gets photo URL
    $x = 115;
    for ($i = 0; $i < 2; ++$i) {
      $pdf->Image( $imageURL, $x, $y, $w, $h, 'JPG', '', '', false, 300, '', false, false, 0, false, false, false);
      $x += 27; // new column
    }
    $y += 52; // new row
  }   
}

但是同样的图像重演了

1 个答案:

答案 0 :(得分:0)

我建议您将数据源更改为:

array (
  0 => array (
    'w'=>320,
    'h'=>240
  ),
  1 => array (
    'w'=>300,
    'h'=>250
  ) 
)

但是没关系,你只需要在这里做一点计算:

// Assuming that 115 is the width of the row
$x = 0;
// Assuming that 52 is the height of each
$y = 0;
$w = 25;
$h = 50;

// We start on 1 image, there are 4 images in each row
$imageCounter = 1;

foreach ( $imagesValues as $imageItem) { 
  foreach ( $imageItem as $imageID) { 
    // gets photo URL
    $imageURL = wp_get_attachment_url($imageID);
    // Calculate the column and row specifications
    switch ($imageCounter) {
      // First column
      case 1: 
        $x = 0;
        $imageCounter ++;
      break;
      case 2: 
        $x = 27;
        $imageCounter ++;
      break;
      case 3: 
        $x = 54;
        $imageCounter ++;
      break;
      case 4: 
        $x = 81;
        // Reset the image counter back to 1 for the new row
        $imageCounter = 1;
        // New row
        $y += 52;
      break;
    }
    $pdf->Image( $imageURL, $x, $y, $w, $h, 'JPG', '', '', false, 300, '', false, false, 0, false, false, false);
  }       
}