Skip to main content
update explanation to include citation and reference to php docs
Source Link

###incrementing a counter variable inside a foreach The foreach statement has two syntaxes"two syntaxes"1 and because the array indexes of $thumbWidths are numeric, the second one can be used to have the value of $i updated automatically (i.e. to have each numeric index assigned , starting at 0), reducing the initial assignment of the counter variable (i.e. $i = 0;) and updating the counter value at the end of each block (i.e. $i++;).

###incrementing a counter variable inside a foreach The foreach statement has two syntaxes and because the array indexes of $thumbWidths are numeric, the second one can be used to have the value of $i updated automatically, reducing the initial assignment of the counter variable (i.e. $i = 0;) and updating the counter value at the end of each block (i.e. $i++;).

###incrementing a counter variable inside a foreach The foreach statement has "two syntaxes"1 and because the array indexes of $thumbWidths are numeric, the second one can be used to have the value of $i updated automatically (i.e. to have each numeric index assigned , starting at 0), reducing the initial assignment of the counter variable (i.e. $i = 0;) and updating the counter value at the end of each block (i.e. $i++;).

add preface statement, example of troublesome filename
Source Link

The feedback below won't likely prevent time out errors, but hopefully will clean up the code.

###Description-code mismatch

You didn't specify which file system the files are coming from, but some systems allow single-quotes and semi-colons in file names (e.g. the MS NT file system - refer to this answer for a summary).

So it may seem unlikely but possibly that a filename like the following would be passed:

$imgUrl = "photo1';compressed.png";

See how well that works with the echo statement from your code in this playground example. In the browser console, there should be an error:

Uncaught SyntaxError: missing ) after argument list

###Description-code mismatch

You didn't specify which file system the files are coming from, but some systems allow single-quotes and semi-colons in file names (e.g. the MS NT file system - refer to this answer for a summary).

The feedback below won't likely prevent time out errors, but hopefully will clean up the code.

###Description-code mismatch

You didn't specify which file system the files are coming from, but some systems allow single-quotes and semi-colons in file names (e.g. the MS NT file system - refer to this answer for a summary).

So it may seem unlikely but possibly that a filename like the following would be passed:

$imgUrl = "photo1';compressed.png";

See how well that works with the echo statement from your code in this playground example. In the browser console, there should be an error:

Uncaught SyntaxError: missing ) after argument list

remove excess line
Source Link
foreach ($thumbWidths as $i => $thumbWidth) {
    $imagick = new imagick($imgUrl);
    $thumbHeight = $thumbHeights[$i];
    if ($crop) {
        $imagick->cropThumbnailImage($thumbWidth,$thumbHeight);
    }
    else {
        $imagick->resizeImage($thumbWidth,$thumbHeight,imagick::FILTER_LANCZOS, 1, true);
 
    }
    $imagick->setImageCompressionQuality($thumbQuality);
    $imagick->writeImage($thumbPaths[$i]);
    $imagick->clear();
    $imagick->destroy();
}
foreach ($thumbWidths as $i => $thumbWidth) {
    $imagick = new imagick($imgUrl);
    $thumbHeight = $thumbHeights[$i];
    if ($crop) {
        $imagick->cropThumbnailImage($thumbWidth,$thumbHeight);
    }
    else {
        $imagick->resizeImage($thumbWidth,$thumbHeight,imagick::FILTER_LANCZOS, 1, true);
 
    }
    $imagick->setImageCompressionQuality($thumbQuality);
    $imagick->writeImage($thumbPaths[$i]);
    $imagick->clear();
    $imagick->destroy();
}
foreach ($thumbWidths as $i => $thumbWidth) {
    $imagick = new imagick($imgUrl);
    $thumbHeight = $thumbHeights[$i];
    if ($crop) {
        $imagick->cropThumbnailImage($thumbWidth,$thumbHeight);
    }
    else {
        $imagick->resizeImage($thumbWidth,$thumbHeight,imagick::FILTER_LANCZOS, 1, true);
    }
    $imagick->setImageCompressionQuality($thumbQuality);
    $imagick->writeImage($thumbPaths[$i]);
    $imagick->clear();
    $imagick->destroy();
}
Source Link
Loading