Reputation: 890
I am using Imagemagick's PHP extension to process some images. In my code, I am trying to increase the value of variable $j by 1; please note $j comes into play in the second loop iteration. I cannot figure out where to place $j++; within my code; it does not seem to increase as I put it in the end, defaulting to its initial '0' zero value.
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();
}
Upvotes: 0
Views: 49
Reputation: 277
In your code, from the second iteration and for all the subsequent ones, the line $j = 0;
is run so $j will stay at 0.
I suggest you init $j= 0 either before the loop or in the if($i==0){}
clause and increment it in the end of the else{} clause:
[...]
$width = (640 - $over->getImageWidth()) / 2;
$j = 0;
for ($i = 0; $i < $count; $i++) {
if ($i == 0) {
$base = new Imagick();
$base->newImage(640, 720, new ImagickPixel('transparent'));
} else {
$base = new Imagick();
$base->readImage(DOCROOT . '/composite-' . $j . '.png');
$j++;
}
$top = new Imagick();
$top->readImage($images[$i]);
$base->compositeImage($top, Imagick::COMPOSITE_DEFAULT, $width, $height);
$base->writeImage(DOCROOT . '/composite-' . $i . '.png');
$height += $top->getImageHeight();
}
Upvotes: 1
Reputation: 1651
initialize your variable j before the for loop by setting it to 0.
$j = 0
for ($i = 0; $i < $count; $i++) {
if (==> put your condition here) {
$j++;
}
....
}
In the for loop you can then formulate what ever condition you want j to increase (e.g. in every second iteration, not during the first iteration,only for the first 10 iterations,...)
Upvotes: 0