LoginSignup
0
2

More than 5 years have passed since last update.

ダミー画像提供サイトから複数枚の画像を一度に取得するスクリプト

Last updated at Posted at 2017-10-12

画像登録フォームなどを検証するときに、
いろんなダミー画像が欲しくて作りました。

ダミー画像取得サイトは、以下の2サイトで設定しており、

・lorempixel(http://lorempixel.com/)
・unsplash(https://unsplash.com/)

URLに指定のパラメータをつけて、アクセスすると、
指定枚数分の画像を取得し、zip形式に圧縮してダウンロードできる状態にします。

php の直書きプログラムですが、、、
参考になればと思い、投稿させていただきました。

設置手順

  1. DocumentRoot配下に「admin」というディレクトリを作成
  2. 「admin」ディレクトリ配下に「getDummyImage.php」の名称でファイルを作成、ソースはプログラムソースの内容
  3. 「admin」ディレクトリ配下に「download」ディレクトリを作成
  4. 「download」ディレクトリの権限を「777」に設定

動作環境

CentOS6.5
Apache2.2
PHP5.6
※ wget、zipコマンドが実行できること

プログラムソース

<html>
<head>
</head>
<body>



<?php

// パラメータ取得
$input = $_GET;

if (empty($input)) {
    print_r("<br/>");
    print_r("<b>■lorempixel サイトから画像を取得する場合</b>");
    print_r("<br/>");
    print_r("<br/>");
    print_r("<font color='#bc3434'>※ 画像にテキストの埋め込みができますが、画像数が少ないため同じ画像が取得される可能性が高いです。</font>");
    print_r("<br/>");
    print_r("<br/>");
    print_r("以下のルールでURLを設定してください。");
    print_r("<br/>");
    print_r("<br/>");
    print_r("<font color='#2a86d4'>?width=[幅]&height=[高さ]&gray=[グレー画像にしたい場合は「g」と入力]&category=[カテゴリ名]&id=[写真のID]&text=ダミーテキスト&total=[ダウンロードしたい画像数]</font>");
    print_r("<br/>");
    print_r("<br/>");
    print_r("<font color='#ec6d7f'>例:/admin/getDummyImage.php?site=lorempixel&width=150&height=150&category=cats&text=tci&total=20</font>");
    print_r("<br/>");
    print_r("<br/>");
    print_r("※ 英字または数値で入力してください。");
    print_r("<br/>");
    print_r("※ 幅と高さは必須です。");
    print_r("<br/>");
    print_r("※ カテゴリは、「abstract, animals, business, cats, city, food, nightlife, fashion, people, nature, sports, technics, transport」が指定可能です。");
    print_r("<br/>");
    print_r("※ カテゴリを指定しないと、写真のID、ダミーテキストは反映されません。");
    print_r("<br/>");
    print_r("※ ダミーテキストは、英字で入力する必要があります。");
    print_r("<br/>");
    print_r("※ ダウンロードしたい画像数が多いと同じ画像が取得される可能性があります。");
    print_r("<br/>");
    print_r("<hr/>");
    print_r("<br/>");
    print_r("<b>■unsplash サイトから画像を取得する場合</b>");
    print_r("<br/>");
    print_r("<br/>");
    print_r("<font color='#bc3434'>※ 画像数が多いので同じ画像が取得されることは少ないですが、画像にテキストの埋め込みができません。</font>");
    print_r("<br/>");
    print_r("<br/>");
    print_r("以下のルールでURLを設定してください。");
    print_r("<br/>");
    print_r("<br/>");
    print_r("<font color='#2a86d4'>?width=[幅]&height=[高さ]&gray=[グレー画像にしたい場合は「g」と入力]&total=[ダウンロードしたい画像数]</font>");
    print_r("<br/>");
    print_r("<br/>");
    print_r("<font color='#ec6d7f'>例:/admin/getDummyImage.php?site=unsplash&width=150&height=150&gray=g&total=20</font>");
    print_r("<br/>");
    print_r("<br/>");
    print_r("※ 英字または数値で入力してください。");
    print_r("<br/>");
    print_r("※ 幅と高さは必須です。");
    print_r("<br/>");
    print_r("※ 画像はランダムです。");
    print_r("<br/>");
    print_r("※ ダウンロードしたい画像数が多いと同じ画像が取得される可能性があります。");
    print_r("<br/>");
    print_r("<div style='margin-top:50px;'><small>©transcosmos inc. All rights reserved. Based by TCI-Framework.</small></div>");
    exit();
}

// デフォルト値指定
$format = array('width' => 100, 'height' => 100, 'total' => 1);

// 空値のパラメータを除去
$input = array_filter($input);
// デフォルト値をパラメータ値で置換
$input = array_merge($format, $input);

// カウント変数をセット
$cnt   = 1;

// ファイル格納場所
$time     = time();
$path     = __DIR__ . '/download/' . $time . '/';
$zippath  = __DIR__ . '/download/';
$zipname  = $time . '.zip';

// ディレクトリ作成
if (!file_exists($path))
{
  $ret = mkdir($path, 0777, true);
  chmod($path, 0777);
}

// URL条件指定
$condition = '';
if (!empty($input['gray'])) {
    $condition .= $input['gray'] . '/';
}

$condition .= $input['width'] . '/';
$condition .= $input['height'] . '/';


// サイト別条件指定
$site = $input['site'];
switch($site) {
    case 'lorempixel':
        $url = 'http://lorempixel.com';

        if (!empty($input['category'])) {
            $condition .= $input['category'] . '/';

            if (!empty($input['id'])) {
                $condition .= $input['id'] . '/';
            } else {
                $condition .= '0/';
            }
        }

        break;
    case 'unsplash':
        $url = 'http://unsplash.it';
        $condition .= '?random';
        //if (!empty($input['blur'])) {
        //    $condition .= '&blur=';
        //}
        //if (!empty($input['id'])) {
        //    $condition .= '&image=' . $input['id'];
        //}

        break;
    default:
        exit;
}


// ダミー画像取得
while ($cnt <= $input['total']) {

    $wgetCmd = 'wget -O %s%s.jpg %s/%s';

    $name = $cnt;

    if (!empty($input['category']) && !empty($input['text'])) {
        $conditionPlus = $condition . $input['text'] . '-' . $cnt . '/';
    } else {
        $conditionPlus = $condition;
    }

    $cmd = sprintf($wgetCmd, $path, $name, $url, $conditionPlus);
    passthru($cmd, $ret);

    if ($ret) {
        print_r("エラーが発生しました。<br/>");
        print_r("URLパラメータまたは『<?php echo $site ?>』サイトが動作していることを確認してください。");
        exit;
    }

    $cnt++;
}

// Zip 圧縮
$ret = exec("cd " . $zippath . ";zip -r " . $zipname . " " . $time);
//if (file_exists($zippath.$zipname)) {
//  header("Content-Type: application/octet-stream");
//  header("Content-Disposition: attachment; filename=$zipname");
//  ob_end_flush();
//  readfile($zippath.$zipname);
//} else {
//}

// Tmpディレクトリ削除
//if($dh = @opendir($path)) {
//
//    while (($obj = readdir($dh))) {
//        if($obj=='.' || $obj=='..') continue;
//        if (!@unlink($path.'/'.$obj)) rm($path.'/'.$obj);
//    }
//    @rmdir($path);
//}


?>

こちらからダウンロードしてください。<br/>
<a href="<?php echo (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] ?>/admin/download/<?php echo $time; ?>.zip" >Download</a>

</body>
</html>

ブラウザからアクセスすると、こんな感じに表示されるので、
URLパラメータをつけて実行してください。

スクリーンショット 2017-10-12 10.15.30.png

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2