<?php
/**
* WiseLoop PHP Web Media Grabber Sample<br/>
* Title: Image<br/>Grabber
*/
require_once dirname(__FILE__) . "/../php-web-media-grabber/bin/wlWmg.php";
require_once dirname(__FILE__) . "/demo-utils.php";
$arrays = array($_POST, $_GET);
$url = wlExtArray::searchArraysByKey($arrays, 'url', null);
$download = wlExtArray::searchArraysByKey($arrays, 'download', '');
$size_active = wlExtArray::searchArraysByKey($arrays, 'size_active', '');
$size = intval(wlExtArray::searchArraysByKey($arrays, 'size', ''));
$size_operator = wlExtArray::searchArraysByKey($arrays, 'size_operator', '');
$limit_active = wlExtArray::searchArraysByKey($arrays, 'limit_active', '');
$limit = intval(wlExtArray::searchArraysByKey($arrays, 'limit', ''));
$tagslice_active = wlExtArray::searchArraysByKey($arrays, 'tagslice_active', '');
$tagslice = wlExtArray::searchArraysByKey($arrays, 'tagslice', '');
$ctype_active = wlExtArray::searchArraysByKey($arrays, 'ctype_active', '');
$image_extensions_active = wlExtArray::searchArraysByKey($arrays, 'image_extensions_active', '');
$image_extensions = wlExtArray::searchArraysByKey($arrays, 'image_extensions', '');
$image_width_active = wlExtArray::searchArraysByKey($arrays, 'image_width_active', '');
$image_width = intval(wlExtArray::searchArraysByKey($arrays, 'image_width', ''));
$image_width_operator = wlExtArray::searchArraysByKey($arrays, 'image_width_operator', '');
$image_height_active = wlExtArray::searchArraysByKey($arrays, 'image_height_active', '');
$image_height = intval(wlExtArray::searchArraysByKey($arrays, 'image_height', ''));
$image_height_operator = wlExtArray::searchArraysByKey($arrays, 'image_height_operator', '');
$image_format_active = wlExtArray::searchArraysByKey($arrays, 'image_format_active', '');
$image_format = wlExtArray::searchArraysByKey($arrays, 'image_format', '');
$image_followdirectimagelinks = wlExtArray::searchArraysByKey($arrays, 'image_followdirectimagelinks', '');
$image_followsubpageslinks = wlExtArray::searchArraysByKey($arrays, 'image_followsubpageslinks', '');
$subpage_limit_active = wlExtArray::searchArraysByKey($arrays, 'subpage_limit_active', '');
$subpage_limit = intval(wlExtArray::searchArraysByKey($arrays, 'subpage_limit', ''));
$subpage_tagslice_active = wlExtArray::searchArraysByKey($arrays, 'subpage_tagslice_active', '');
$subpage_tagslice = wlExtArray::searchArraysByKey($arrays, 'subpage_tagslice', '');
//testing if the url is coming from the approved sites (wiseloop or darkroomdesign)
//this test is meant to save bandwidth and disk space only on wiseloop.com demonstration page
//please remove this from your real production project
if ($download && isset($allowDownloadFrom)) {
//$download = false;
foreach ($allowDownloadFrom as $approvedSite) {
if (stripos($url, $approvedSite)) {
$download = true;
break;
}
}
}
?>
<script type="text/javascript">
function advancedFollowSubpagesChanged() {
var checked = document.getElementById('image_followsubpageslinks').checked;
var el = document.getElementById('advancedFollowSubpagesSettings');
if(checked) {
el.style.display = 'block';
}else {
el.style.display = 'none';
}
}
</script>
<div class="row container">
<form action="" id="form" method="POST" enctype="multipart/form-data" class="form form-horizontal" role="form">
<div class="well well-sm">
<label class="control-label" for="url">Full target URL path: </label>
<input class="form-control" type="text" name="url" id="url" placeholder="http://" value="<?php echo isset($url) ? $url : 'http://www.pachd.com/free-images/flower-images.html'; ?>"/>
<small class="text-info">include http:// also</small>
</div>
<div class="well well-sm">
<h4>Image Grabber Filters</h4>
<div class="row">
<div class="checkbox col-sm-2">
<label>
<input type="checkbox" id="image_extensions_active" name="image_extensions_active" <?php echo $image_extensions_active ? 'checked' : ''; ?>/>
Image Extensions
</label>
</div>
<div class="col-sm-10">
<small class="text-info">if nothing is selected or the filter is inactive (unchecked), all extensions will be searched</small>
</div>
</div>
<select class="form-control" name="image_extensions[]" multiple>
<?php echo options(wlWmgImageGrabber::getImageFileExtensions(), $image_extensions); ?>
</select>
<small class="text-info">Ctrl+Click to select multiple values</small><br/>
<div class="row">
<div class="col-sm-2">
<div class="checkbox">
<label>
<input type="checkbox" id="image_width_active" name="image_width_active" <?php echo $image_width_active ? 'checked' : ''; ?>/>
Image Width
</label>
</div>
</div>
<div class="col-sm-1">
<select class="form-control" name="image_width_operator" id="image_width_operator">
<?php echo options(wlWmgFilter::getOperators(true), $image_width_operator, true); ?>
</select>
</div>
<div class="col-sm-2">
<div class="input-group">
<input class="form-control" type="text" name="image_width" id="image_width" value="<?php echo $image_width ? $image_width : '0'; ?>"/>
<span class="input-group-addon">pixels</span>
</div>
</div>
<div class="col-sm-7">
<small class="text-info">if checked, only images with specified width will be extracted</small>
</div>
</div>
<br/>
<div class="row">
<div class="col-sm-2">
<div class="checkbox">
<label>
<input type="checkbox" id="image_height_active" name="image_height_active" <?php echo $image_height_active ? 'checked' : ''; ?>/>
Image Height
</label>
</div>
</div>
<div class="col-sm-1">
<select class="form-control" name="image_height_operator" id="image_height_operator">
<?php echo options(wlWmgFilter::getOperators(true), $image_height_operator, true); ?>
</select>
</div>
<div class="col-sm-2">
<div class="input-group">
<input class="form-control" type="text" name="image_height" id="image_height" value="<?php echo $image_height ? $image_height : '0'; ?>"/>
<span class="input-group-addon">pixels</span>
</div>
</div>
<div class="col-sm-7">
<small class="text-info">if checked, only images with specified height will be extracted</small>
</div>
</div>
<br/>
<div class="row">
<div class="input-append">
<div class="col-sm-2">
<div class="checkbox">
<label>
<input type="checkbox" id="image_format_active" name="image_format_active" <?php echo $image_format_active ? 'checked' : ''; ?>/>
Image Format
</label>
</div>
</div>
<div class="col-sm-3">
<select class="form-control" name="image_format" id="image_format">
<?php echo options(wlWmgImageGrabber::getImageFormats(true), $image_format, true); ?>
</select>
</div>
<div class="col-sm-7">
<small class="text-info">if checked, only images having specified format will be extracted</small>
</div>
</div>
</div>
<br/>
<div class="row">
<div class="checkbox col-sm-2">
<label>
<input type="checkbox" id="image_followdirectimagelinks" name="image_followdirectimagelinks" <?php echo $image_followdirectimagelinks ? 'checked' : ''; ?>/>
Follow direct image a href links
</label>
</div>
<div class="col-sm-10">
<small class="text-info">if checked, the grabbing engine will follow a href direct image links that hide behind another images (direct link to the picture: the full size image file is linked directly under the thumb without embedding it into a html subpage) - very useful on galleries that displays only the thumbs, and the full size image is behind the thumb's link</small>
</div>
</div>
<br/>
<div class="row">
<div class="checkbox col-sm-2">
<label>
<input onchange="advancedFollowSubpagesChanged();" type="checkbox" id="image_followsubpageslinks" name="image_followsubpageslinks" <?php echo $image_followsubpageslinks ? 'checked' : ''; ?>/>
Follow subpages a href links
</label>
</div>
<div class="col-sm-10">
<small class="text-info">
if checked the grabbing engine will follow a href subpages links in order to search them for another images (link to a html subpage that embeds the full size picture among other html tag elements) - very useful on galleries that displays only the thumbs, and the full size image is embedded inside the thumb's link subpage<br/>
<strong>By checking this, can lead to a very long processing time, so use it wisely; it is advisable to use the advanced settings below.</strong>
</small>
<div id="advancedFollowSubpagesSettings" class="well well-sm" style="display:<?php echo $image_followsubpageslinks ? 'block' : 'none'; ?>">
<h5>Advanced settings for subpages link navigation</h5>
<p>
<input type="checkbox" id="subpage_limit_active" name="subpage_limit_active" <?php echo $subpage_limit_active ? 'checked' : ''; ?>/>
<label for="subpage_limit_active">Scan only first</label>
<input type="text" style="width:50px;" name="subpage_limit" id="subpage_limit" value="<?php echo $subpage_limit ? $subpage_limit : '0'; ?>"/> subpage images
<span class="info"> - If active (checked) only first N (this number) images will be checked to find the full size image inside the subpage</span>
</p>
<p>
<input type="checkbox" id="subpage_tagslice_active" name="subpage_tagslice_active" <?php echo $subpage_tagslice_active ? 'checked' : ''; ?>/>
<label for="subpage_tagslice_active">Search for full size image only inside the subpage tag that contains</label>
<input type="text" style="width:300px;" name="subpage_tagslice" id="subpage_tagslice" value="<?php echo $subpage_tagslice ? htmlentities($subpage_tagslice) : ''; ?>"/>
<span class="info">
- If active (checked) the processor will test only the media founded inside the contents of the subpage tag specified here;
an incomplete tag (tag slice) can be specified also, the tag will auto complete depending on the contextual HTML content
</span>
</p>
</div>
</div>
</div>
<br/>
<div class="row">
<div class="checkbox col-sm-2">
<label>
<input type="checkbox" id="ctype_active" name="ctype_active" <?php echo $ctype_active ? 'checked' : ''; ?>/>
Check Content-Type header
</label>
</div>
<div class="col-sm-10">
<small class="text-info">
if checked, the grabber engine will check also the content-type of the grabbed media to make sure it is an image;
this checking will add some additional processing as headers for each possible media will be downloaded in order to perform image validation.
If unchecked, the grabber engine will use only the provided file extensions and therefore the grabbing process will be faster but can bring non-image files also
</small>
</div>
</div>
</div>
<?php require_once 'widget-grabbing-parameters.php'; ?>
<div>
<input type="submit" value="Grab Images" class="btn btn-primary"/>
</div>
</form>
</div>
<?php
if (!$image_extensions_active) {
$image_extensions = null;
}
//the magic starts here ...
//creating the image grabber object by giving some necessary parameters:
//$url: url,
//$image_extensions: the image extensions to grab (null means all)
$imageMediaGrabber = new wlWmgImageGrabber($url, $image_extensions, $ctype_active);
//set download switch: if the images should be downloaded to localhost
$imageMediaGrabber->setDoDownload($download);
//limit filter: setting the media limit count to be grabbed
if ($limit_active) {
$imageMediaGrabber->setLimit($limit);
}
//the processor will grab only the media founded inside the contents of the tag specified here
if ($tagslice_active) {
$imageMediaGrabber->setGrabOnlyFromTagSlice($tagslice);
}
//$image_followdirectimagelinks: if the grabbing engine should follow a href direct image links that hide behind another images (direct link to the picture: the full size image file is linked directly under the thumb without embedding it into a html subpage)
//(very usefull when grabbing a gallery page that displays only the thumbs as starting page)
if ($image_followdirectimagelinks) {
$imageMediaGrabber->setFollowDirectImageLinks(true);
}
//$image_followdirectimagelinks: if the grabbing engine should follow a href subpages links in order to search them for another images (link to a html subpage that embeds the full size picture among other html tag elements)
//(very useful when grabbing a gallery page that displays only the thumbs as starting page)
if ($image_followsubpageslinks) {
$imageMediaGrabber->setFollowSubpagesLinks(true);
if ($subpage_limit_active) {
$imageMediaGrabber->setFollowedSubpageImgLimitCount($subpage_limit);
}
if ($subpage_tagslice_active) {
$imageMediaGrabber->setFollowedSubpageGrabOnlyFromTagSlice($subpage_tagslice);
}
}
//the width (pixels) filter
if ($image_width_active) {
//filter creation
$filterWidth = new wlWmgFilter(
$image_width,
'width',
$image_width_operator
);
//adding filter to the grabber
$imageMediaGrabber->addFilter($filterWidth);
}
//the height (pixels) filter
if ($image_height_active) {
//filter creation
$filterHeight = new wlWmgFilter(
$image_height,
'height',
$image_height_operator
);
//adding filter to the grabber
$imageMediaGrabber->addFilter($filterHeight);
}
//the format (portrait / landscape) filter
if($image_format_active) {
//filter creation
$filterFormat = new wlWmgFilter(
$image_format,
'format'
);
//adding filter to the grabber
$imageMediaGrabber->addFilter($filterFormat);
}
//the content-length (size) filter
if ($size_active) {
//filter creation
$sizeFilter = new wlWmgFilter(
$size,
wlWmgFilter::TYPE_CONTENT_LENGTH,
$size_operator
);
//adding filter to the grabber
$imageMediaGrabber->addFilter($sizeFilter);
}
//grabbing
if ($url) {
$imageMediaGrabber->grab();
//displaying the grabbed media
echo '<h3>Grabbed media (' . $imageMediaGrabber->countValidMedia() . ' items found)</h3>';
echo '<pre>' . print_r($imageMediaGrabber->getValidMediaTable(true), true) . '</pre>';
//displaying founded invalid media or unsuccessful grabbing tries (due to various server rejections, or broken links)
if ($imageMediaGrabber->hasInvalidMedia()) {
echo '<h3>Invalid media (' . $imageMediaGrabber->countInvalidMedia() . ' items found)</h3>';
echo '<pre>' . print_r($imageMediaGrabber->getInvalidMediaTable(true), true) . '</pre>';
}
}
?>