TCPDF :设置通过 'foreach' 方法获取的多个图像之间的边距
TCPDF : Set margin between several images get by 'foreach' method
我正在使用 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
}
}
但相同的图像会重复出现
我建议您将数据源更改为:
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);
}
}
我正在使用 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
}
}
但相同的图像会重复出现
我建议您将数据源更改为:
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);
}
}