我使用Imagemagick的PHP扩展来处理一些图像。在我的代码中,我试图将变量$ j的值增加1;请注意$ j在第二次循环迭代中发挥作用。我无法弄清楚放置$ j ++的位置;在我的代码中;它似乎没有增加,因为我把它放在最后,默认为最初的'0'零值。简单的循环迭代难度
first iteration we do not need $j;
second iteration $j is assigned 0;
third iteration $j needs to be increased by 1 or $j = 1
... loop continues
$dst = "tem891";
$over = new Imagick();
$over->readImage(DOCROOT . '/' . $dst . '/middle.png');
$images = glob(DOCROOT . '/' . $dst . "/line-*.png");
sort($images, SORT_NATURAL | SORT_FLAG_CASE);
$count = count($images);
$height = (720 - $over->getImageHeight())/2;
$width = (640 - $over->getImageWidth())/2;
$j = '';
for ($i = 0; $i < $count; $i++) {
if ($i == 0) {
$base = new Imagick();
$base->newImage(640, 720, new ImagickPixel('transparent'));
} else {
$j = 0;
$base = new Imagick();
$base->readImage(DOCROOT . '/composite-' . $j . '.png');
}
$top = new Imagick();
$top->readImage($images[$i]);
$base->compositeImage($top, Imagick::COMPOSITE_DEFAULT, $width, $height);
$base->writeImage(DOCROOT . '/composite-' . $i . '.png');
$height += $top->getImageHeight();
}
删除$ J = 0;并且在for循环之前做$ j = 0的$ j =''的instad。然后在$ base-> readImage行之后添加$ j ++; – sinaza