簡體   English   中英

移動並替換超過特定分辨率的所有子目錄中的每個圖像

[英]Move and replace each image in all subdirectories that are over a certain resolution

我需要每天運行一個cron作業來移動寬度或高度超過1000px的所有圖像,然后使用默認圖像替換移動的圖像,該圖像位於運行此腳本的同一目錄中。

/gwart.co.uk/mediawiki/images/too_large.jpg //file used to replace old image

我已經能夠使用以下方式過濾所有圖像:

find -type f -regex "^.*\.\(png\|jpg\|jpeg\)$" -printf "%p, " -exec identify -format "%w, %h\n" {} \; | awk -F ',' '$2 > 1000 || $3 > 1000'

哪個會輸出

./b1/Eldar_Guardian.png, 892, 1767
./b2/Farseer_with_Guardians1.png, 894, 1308

哪個顯示文件,但事實是沒有丟棄awk我不知道如何實際移動這些圖像,然后將原始文件名存儲為too_large.jpgcp

我的目標是為此使用Bash,但是如果有人願意,也可以選擇Php

另一個煩人的要求是原始文件類型需要持久存在,其中也存在相關的too_large文件。

  • 移動高度或寬度超過1000像素的圖像,無論哪個匹配。
  • cp too_large [.jpg / .png / .jpg]移動圖像位置

假設你有詳細信息,那么這樣的事情怎么樣 -

find -type f -regex "^.*\.\(png\|jpg\|jpeg\)$" -printf "%p, " -exec identify -format "%w, %h\n" {} \; |
  awk -F ',' '$2 > 1000 || $3 > 1000 {print $1}' |
  while read -r file
  do mv "$file" "some/other/place/$newName"
     cp /gwart.co.uk/mediawiki/images/too_large.jpg "$file"
  done

原始find垂直和水平返回文件名和像素數。

./b1/Eldar_Guardian.png, 892, 1767
./b2/Farseer_with_Guardians1.png, 894, 1308

你的awk已經選擇了超大文件; 我剛剛添加了一個步驟,只在找到文件名時打印文件名。

  awk -F ',' '$2 > 1000 || $3 > 1000 {print $1}' | # print of X or Y >1k

所以循環只需要讀取和處理文件名。

如果你想用PHP做這個偽代碼應該適用於小調整。 您將需要確保您位於正確的目錄(chdir)中,因為該示例假定您正在檢查的文件位於運行PHP的目錄中。

$imagelist = glob('*.{png,jpg,jpeg}',GLOB_BRACE);
foreach ($imagelist as $image) {

    $image_info = getimagesize($image);
    if ($image_info[0] > 1000 OR
        $image_info[1] > 1000) {

        rename($image,'movebigfilelocation');
        copy('/gwart.co.uk/mediawiki/images/too_large.jpg',$image);

    }
}

如果你有PHP和Imagick ,你也可以縮小圖像。 片段:

$maxXres = 1000;

$mimg = new \Imagick($img['tmp_name']);
$xresolution = $mimg->getImageWidth();

if ($xresolution > $this->maxXres){
   $mimg->thumbnailImage($this->maxXres, 0);
}

$fn = "/where/ever/downscaled.jpg";
$mimg->writeImage($fn);

然后用$ fn覆蓋原始文件。 該示例僅考慮x軸,但如何檢查y側是相同的,只需使用getImageHeight。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM