Skip to content

Instantly share code, notes, and snippets.

@time-wcrp
Last active August 16, 2016 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save time-wcrp/077022e7d29f761d8b97 to your computer and use it in GitHub Desktop.
Save time-wcrp/077022e7d29f761d8b97 to your computer and use it in GitHub Desktop.
The dummyimage download files to local
#!/usr/bin/env php
<?php
/*
The MIT License (MIT)
Copyright (c) 2015 iiitum
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Request a dummy image from <http://dummyimage.com>.
usage: php dummyimage.php COUNT WIDTH HEIGHT
*/
$path = __DIR__ . '/dummyimage';
# http://stackoverflow.com/questions/5614530/generating-a-random-hex-color-code-with-php
function color()
{
return strtolower(sprintf('#%06X', mt_rand(0, 0xFFFFFF)));
}
# http://www.jonasjohn.de/snippets/php/color-inverse.htm
function color_inverse($color)
{
$color = str_replace('#', '', $color);
if (strlen($color) != 6){ return '000000'; }
$rgb = '';
for ($x=0;$x<3;$x++){
$c = 255 - hexdec(substr($color,(2*$x),2));
$c = ($c < 0) ? 0 : dechex($c);
$rgb .= (strlen($c) < 2) ? '0'.$c : $c;
}
return strtolower('#'.$rgb);
}
function linkimage ($width, $height)
{
$url = 'http://dummyimage.com/';
$format = array('gif', 'jpg', 'png');
$format = $format[array_rand($format)];
$colors[0] = color();
$colors[1] = color_inverse($colors[0]);
$colors[0] = str_replace('#', '', $colors[0]);
$colors[1] = str_replace('#', '', $colors[1]);
return array(
'filename' => $width . 'x' . $height . '-' . $colors[0] . '-' . $colors[1] . '.' . $format,
'link' => $url . $width . 'x' . $height . '/' . $colors[0] . '/' . $colors[1] . '.' . $format
);
}
function create($count, $width, $height)
{
if (!is_dir($GLOBALS['path'])) {
mkdir($GLOBALS['path']);
}
for ($i = 1;$i <= $count;$i++) {
$linkfile = linkimage($width, $height);
@file_put_contents($GLOBALS['path'] . '/' . $linkfile['filename'], fopen($linkfile['link'], 'r'));
echo "[$i] ". json_encode($linkfile) . "\n";
}
}
$count = 20;
$width = 600;
$height = 600;
if (isset($argv[1])){
$count = $argv[1];
}
if (isset($argv[2])){
$width = $argv[2];
}
if (isset($argv[3])){
$height = $argv[3];
}
create($count, $width, $height);
@time-wcrp
Copy link
Author

How to use?

  1. run command curl 'https://gist.githubusercontent.com/iiitum/077022e7d29f761d8b97/raw/eb29b39c8f902a63191ff4bcdd7c77c961625db6/dummyimage.php' -o 'dummyimage.php'
  2. run command php dummyimage.php 20 800 800

Let's enjoy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment