1

我正在尝试使用 php filemtime 在相册中查找最新添加的图像(文件夹)并使用 Imagick 制作拇指。

我知道找到最新文件夹的那部分脚本确实有效,并且也适用于 Imagick 部分。把这两个放在一起就是问题所在。这里有一群更聪明的人 - 有人可以帮助并使其发挥作用吗?

脚本如下所示:

<?php

/* paths to the images */
$directory  = '../i/albums';
$fdirectory = opendir("$directory");
while (false !== ($file       = readdir($fdirectory))) {
    if (($file != ".") && ($file != ".." ) && ($file != "index.php~" ) && ($file != "index.php" )) {

        $files[$file] = filemtime("$directory/$file");
    }
}
closedir($fdirectory);
clearstatcache();
arsort($files);
$i            = 0;
while ((list($key1, $val) = each($files)) && ($i <= 0))
    $newdirectory = $directory . '/' . $key1 . '/thumbs/'; /* Problem is probably here */
/* Making thumbnail starts here */
/* The object used as a canvas */
$canvas       = new imagick('../images/wood_grain_3030646.jpg');
$canvas->adaptiveResizeImage(250, 250);
$canvas->setImageFormat("png");
/* Create an empty ImagickDraw object
  (Use the defaults for the polaroid) */
$bg           = new ImagickDraw();
/* Create a few random images */
/* Imagick seems not to like  $newdirectory here */
$images       = new Imagick(glob("" . $newdirectory . "{*.jpg,*.gif,*.png}", GLOB_BRACE));
/* Loop trough images, overlay on canvas and remove the image */
foreach ($images as $key => $imag) {
    /* Thumbnail to 100x width and set background to black.
      It looks like an drop-in shadow */
    $imag->thumbnailImage(100, null);
    $imag->setImageBackgroundColor(new ImagickPixel("black"));
    /* Use a random angle */
    $angle = mt_rand(1, 45);
    if (mt_rand(1, 2) % 2 === 0) {
        $angle  = $angle * -1;
    }
    /* Create the polaroid */
    $imag->polaroidImage($bg, $angle);
    /* Composite the polaroid over the canvas */
    /* Composite to a random location */
    $canvas->compositeImage($imag, Imagick::COMPOSITE_OVER, mt_rand(10, 150), mt_rand(10, 150));
    /* Round the corners. (yes, this is all that is needed.) */
    $canvas->roundCorners(10, 10);
    /* Clone the current object */
    $shadow = $canvas->clone();
    /* Set image background color to black
      (this is the color of the shadow) */
    $shadow->setImageBackgroundColor(new ImagickPixel('black'));
    /* Create the shadow */
    $shadow->shadowImage(80, 3, 5, 5);
    /* Imagick::shadowImage only creates the shadow.
      That is why the original image is composited over it */
    $shadow->compositeImage($canvas, Imagick::COMPOSITE_OVER, 0, 0);
    /* Free some resources */
    $imag->removeImage();
    /* Add some contrast */
    $shadow->contrastImage(20);

    $shadow->writeImages("../images/polaroids/polaroids_thumb.png", true);
}
/* Display the results */
header("Content-Type: image/png");
echo $shadow;
?>

我已经单独测试了 Imagick 部分,如果用于静态文件夹,它实际上可以工作。

4

0 回答 0