-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_resample.php
164 lines (132 loc) · 4.35 KB
/
image_resample.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
# output resampled image (better qual than resizing).
// if src_imagefile is already set then assume that this script is being
// included by another script if not then get details
session_start();
include("set_paths.php");
$filekey = $_SESSION['filekey'];
$filekey = $_SESSION['filekey'];
$basedir = $_SESSION['basedir'];
$src_imgfile = $filelist[$filekey];
$src_imgfile = "$basedir/$src_imgfile";
// Override session vars if Gets are provided
if ( isset($_GET['filekey']) ) {
$gallery = $_GET['gallery'];
if ( isset($_GET['dirkey']) ) $dirkey = $_GET['dirkey'];
$filekey = $_GET['filekey'];
#include( dirname(__FILE__)."/imagedata/$gallery");
include( "$imagedata/$gallery");
if ( $filekey == "default" ) {
$src_imgfile = $default_image;
}
else {
include_once("generate_filelist.php");
$filelist = generate_filelist($gallery, $dirkey);
$basedir = $dirs[$dirkey];
$src_imgfile = $filelist[$filekey];
$src_imgfile = "$basedir/$src_imgfile";
}
} // End if
if ( isset($_GET['size']) ) {
$size = $_GET['size'];
}
if ( isset($_GET['xsize']) ) {
$xsize = $_GET['xsize'];
unset($size);
}
if ( isset($_GET['ysize']) ) {
$ysize = $_GET['ysize'];
unset($size);
}
#$imagecache=dirname(__FILE__)."/imagecache";
if ( isset($xsize) || isset($ysize) ){
$cachefile="$imagecache/resample/".$xsize."x".$ysize."/$src_imgfile";
}
else $cachefile="$imagecache/resample/$size/$src_imgfile";
# Detect image format - PNG or Jpeg currently supported
$format = "jpeg" ; # assume jpeg by default.
if ( preg_match( "/\.png$/i", "$src_imgfile" ) ) {
$format="png";
}
$f_imageformat= "image$format";
$f_imagecreatefromformat= "imagecreatefrom$format";
$imageheader="content type: image/$format";
header($imageheader);
// see if the image has already been created in the cache
if ( file_exists("$cachefile") ) {
// If it does already exist then use it
$src_img = $f_imagecreatefromformat($cachefile);
$f_imageformat($src_img);
imagedestroy($src_img);
exit() ;
}
// otherwise create a new one
$src_img = $f_imagecreatefromformat($src_imgfile);
$srcsize = getimagesize($src_imgfile);
if ( isset($xsize) || isset($ysize) ){
list( $dest_x, $dest_y ) = get_fixed_dimensions($xsize,$ysize, $srcsize[0], $srcsize[1]);
#list( $dest_x, $dest_y ) = get_fixed_dimensions($xsize,$ysize);
}
elseif ( isset($size) ) {
list( $dest_x, $dest_y ) = get_auto_dimensions($size, $srcsize[0], $srcsize[1]);
}
else {
echo "\n<br/>ERROR: No image sizing defined<br/>\n";
exit;
}
$dest_img = imagecreatetruecolor($dest_x, $dest_y);
imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0,
$dest_x, $dest_y, $srcsize[0], $srcsize[1] );
# Output the new image to the user
$f_imageformat($dest_img);
# Save it to image cache
$cachefile_dir = dirname("$cachefile");
mkdir("$cachefile_dir", 0755, true );
$f_imageformat($dest_img, "$cachefile" );
#clean up memory
imagedestroy($src_img);
imagedestroy($dest_img);
function get_fixed_dimensions( $xsize, $ysize, $xsrcsize,$ysrcsize ){
if ( $xsize > 0 ){
// x axis size has been specified so set dest_x
$dest_x = $xsize;
if ( ! isset( $ysize ) || $ysize == 0 ) $dest_y = ($dest_x/$xsrcsize)*$ysrcsize ;
}
if ( $ysize > 0 ){
// y axis size has been specified so set dest_y
$dest_y = $ysize;
if ( ! isset( $xsize ) || $xsize == 0 ) $dest_x = ($dest_y/$ysrcsize)*$xsrcsize ;
}
$array = array($dest_x, $dest_y) ;
return $array;
}
function get_auto_dimensions($size,$xsrcsize,$ysrcsize) {
// Get the dimensions based on one size setting - so that portrait and
// landscape images are to the same scale
if ( $xsrcsize > $ysrcsize ){
// this must be a landscape format image
// Check that the original is bigger than the requested size
// we don't want to enlarge a small original photo.
if ( $size < $xsrcsize ) {
$dest_x = $size ;
$dest_y = ($dest_x/$xsrcsize)*$ysrcsize ;
} else {
$dest_x = $xsrcsize ;
$dest_y = $ysrcsize ;
}
} else {
// otherwise it is a portrait format
// Check that the original is bigger than the requested size
// we don't want to enlarge a small original photo.
if ( $size < $ysrcsize ) {
$dest_y = $size ;
$dest_x = ($dest_y/$ysrcsize)*$xsrcsize ;
} else {
$dest_x = $xsrcsize ;
$dest_y = $ysrcsize ;
}
}
$array = array($dest_x, $dest_y) ;
return $array;
}
?>