forked from gavmck/resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.php
82 lines (65 loc) · 2.31 KB
/
resize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
# ========================================================================#
#
# Author: Gavyn McKenzie
# Version: 1.0
# Date: 10-Oct-13
# Purpose: Take an array of images and convert to the cached correct sized version, return as JSON
# Requires : Requires PHP5, GD library, resize-class.php
#
# ========================================================================#
// @TODO Don't resize if cached
require_once __DIR__ . '/php/lib/resize-class.php';
// Return JSON
header('Content-Type: application/json');
// Potential image sizes
// Could be set evenly as a tolerance
// or to your common image display sizes
// Example: Round to nearest 100px
$sizes = array(
'100',
'150',
'200',
'240'
);
$cache = 'img/cache/';
// Get the closest value to the number in array (as long as it's not bigger than original)
function closest($search, $arr) {
$closest = null;
foreach($arr as $item) {
// distance from image width -> current closest entry is greater than distance from
if ($closest == null || abs($search - $closest) > abs($item - $search)) {
$closest = $item;
}
}
$closest = ($closest == null) ? $closest = $search : $closest;
return $closest;
}
$images = array();
if (isset($_GET['image'])) {
foreach ($_GET['image'] as $key => $imageArr) {
// Error suppression
if (isset($imageArr["src"]) && $imageArr["src"] != "" && $imageArr["src"] != "/") {
// get image width
$width = (int) $imageArr['width'];
$src = $imageArr['src'];
// Get the full path for the image
if (substr($src,0,7) == "http://") {
$image = $src;
} else {
$base_path = str_replace(basename(__FILE__),"",__FILE__);
$image = $base_path.str_replace("/",DIRECTORY_SEPARATOR,$src);
}
// get the optimum width from the available options
$width = closest($width, $sizes);
// Initialise image
$crispy = new resize($image,$width,$cache);
$newSrc = $crispy->resizeImage();
$images[] = array('og_src' => $src, 'src' => $newSrc);
} else {
$images[] = array('og_src' => $src, 'src' => '', 'fail' => true);
}
}
}
echo json_encode($images);
?>