commit | author | age
|
175a52
|
1 |
<?php |
JK |
2 |
class okladki { |
|
3 |
static function znajdz($KOD, $ISBN, $dir = 'covers') { |
|
4 |
if(strlen($KOD)<=8 && ctype_digit($KOD)) { |
|
5 |
validate::KOD($KOD); |
|
6 |
|
|
7 |
if(file_exists('./'.$dir.'/own/'.$KOD)) { |
|
8 |
return './'.$dir.'/own/'.$KOD; |
|
9 |
} |
|
10 |
} |
|
11 |
|
|
12 |
if(strlen($ISBN)==13) { |
|
13 |
validate::EAN($ISBN); |
|
14 |
|
|
15 |
if(substr($ISBN, 0, 3)=='978') { |
|
16 |
$ISBN10 = convert::ISBN13_to_ISBN10($ISBN); |
|
17 |
} |
|
18 |
else |
|
19 |
{ |
|
20 |
$ISBN10 = $ISBN; |
|
21 |
} |
|
22 |
|
|
23 |
if(file_exists('./'.$dir.'/own/'.$ISBN)) { |
|
24 |
return './'.$dir.'/own/'.$ISBN; |
|
25 |
} |
|
26 |
|
|
27 |
if(self::librarything($ISBN, $dir)!==FALSE) { |
|
28 |
return './'.$dir.'/'.$ISBN; |
|
29 |
} |
|
30 |
} |
|
31 |
|
|
32 |
return FALSE; |
|
33 |
} |
|
34 |
|
|
35 |
static function librarything($ISBN, $dir = 'covers') { |
532779
|
36 |
if(!function_exists('curl_init') || !config::$lt_api) { |
175a52
|
37 |
return FALSE; |
JK |
38 |
} |
|
39 |
|
|
40 |
// Okładkę już mamy |
|
41 |
if(file_exists('./'.$dir.'/'.$ISBN)) { |
|
42 |
if(filesize('./'.$dir.'/'.$ISBN)>0) { |
|
43 |
return NULL; |
|
44 |
} |
|
45 |
|
|
46 |
// Negatywne cache'owanie |
|
47 |
if(filesize('./'.$dir.'/'.$ISBN)==0 AND filemtime('./'.$dir.'/'.$ISBN)+(30*24*60*60) > time()) { |
|
48 |
return FALSE; |
|
49 |
} |
|
50 |
} |
|
51 |
|
532779
|
52 |
$get = 'http://www.librarything.com/devkey/'.config::$lt_api.'/'.($dir=='covers_big' ? 'large' : 'small').'/isbn/'.$ISBN10; |
175a52
|
53 |
$curl = curl_init($get); |
JK |
54 |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); |
|
55 |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); |
|
56 |
|
|
57 |
$img = @curl_exec($curl); |
|
58 |
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
59 |
$type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); |
|
60 |
|
|
61 |
if($code != 200 || substr($type, 0, 6) != 'image/' || $img == FALSE || strlen($img) < 100) { |
|
62 |
// Negatywne cache'owanie |
|
63 |
touch('./'.$dir.'/'.$ISBN); |
|
64 |
return FALSE; |
|
65 |
} |
|
66 |
else |
|
67 |
{ |
|
68 |
file_put_contents('./'.$dir.'/'.$ISBN, $img); |
|
69 |
return TRUE; |
|
70 |
} |
|
71 |
} |
|
72 |
|
|
73 |
static function przenies($SKOD, $SISBN, $KOD, $ISBN) { |
|
74 |
foreach(array('covers', 'covers_big') as $where) { |
|
75 |
$nowaokl = self::znajdz($KOD, $ISBN, $where); |
|
76 |
if(!$nowaokl) { |
|
77 |
$staraokl = self::znajdz($SKOD, $SISBN, $where); |
|
78 |
if(!$staraokl) { |
|
79 |
continue; |
|
80 |
} |
|
81 |
if($nowaokl == $staraokl) { |
|
82 |
continue; |
|
83 |
} |
|
84 |
|
|
85 |
if(!empty($ISBN)) { |
|
86 |
if(strpos($staraokl, '/own/')) { |
|
87 |
rename($staraokl, './'.$where.'/own/'.$ISBN); |
|
88 |
} |
|
89 |
else |
|
90 |
{ |
|
91 |
rename($staraokl, './'.$where.'/'.$ISBN); |
|
92 |
} |
|
93 |
} |
|
94 |
else |
|
95 |
{ |
|
96 |
rename($staraokl, './'.$where.'/own/'.$KOD); |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
if($KOD != $SKOD) { |
|
102 |
self::usun($SKOD, ''); |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
static function usun($KOD, $ISBN) { |
|
107 |
if($ISBN) { |
|
108 |
@unlink('./covers/'.$ISBN); |
|
109 |
@unlink('./covers_big/'.$ISBN); |
|
110 |
@unlink('./covers/own/'.$ISBN); |
|
111 |
@unlink('./covers_big/own/'.$ISBN); |
|
112 |
} |
|
113 |
else |
|
114 |
{ |
|
115 |
@unlink('./covers/own/'.$KOD); |
|
116 |
@unlink('./covers_big/own/'.$KOD); |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
static function upload($files, $KOD, $ISBN) { |
|
121 |
if($ISBN) { |
|
122 |
$DANE = $ISBN; |
|
123 |
} |
|
124 |
else |
|
125 |
{ |
|
126 |
$DANE = $KOD; |
|
127 |
} |
|
128 |
|
|
129 |
if(is_uploaded_file($files['tmp_name'])) { |
|
130 |
okladki::skaluj($files['tmp_name'], 500, 500, './covers_big/own/'.$DANE); |
|
131 |
okladki::skaluj($files['tmp_name'], 53, 80, './covers/own/'.$DANE); |
|
132 |
if(file_exists('./covers/'.$DANE)) { |
|
133 |
unlink('./covers/'.$DANE); |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
|
|
138 |
static function skaluj($file, $max_width, $max_height, $outfile) { |
|
139 |
list($width, $height, $type) = getimagesize($file); |
|
140 |
|
|
141 |
switch($type) { |
|
142 |
case IMAGETYPE_JPEG: |
|
143 |
$image = imagecreatefromjpeg($file); |
|
144 |
break; |
|
145 |
case IMAGETYPE_PNG: |
|
146 |
$image = imagecreatefrompng($file); |
|
147 |
break; |
|
148 |
case IMAGETYPE_GIF: |
|
149 |
$image = imagecreatefromgif($file); |
|
150 |
break; |
|
151 |
case IMAGETYPE_XBM: |
|
152 |
$image = imagecreatefromxpm($file); |
|
153 |
break; |
|
154 |
default: |
|
155 |
error::add('Nieznany format obrazka: '.$type.'!'); |
|
156 |
return FALSE; |
|
157 |
break; |
|
158 |
} |
|
159 |
|
|
160 |
if ($width > $max_width OR $height > $max_height) { |
|
161 |
if($width*$max_height > $height*$max_width) { |
|
162 |
$new_width = $max_width; |
|
163 |
$new_height = round( ($new_width / $width) * $height ); |
|
164 |
} |
|
165 |
else |
|
166 |
{ |
|
167 |
$new_height = $max_height; |
|
168 |
$new_width = round( ($new_height / $height) * $width ); |
|
169 |
} |
|
170 |
|
|
171 |
$new_image = imagecreatetruecolor($new_width, $new_height); |
|
172 |
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); |
|
173 |
return imagejpeg($new_image, $outfile, 100); |
|
174 |
} |
|
175 |
else |
|
176 |
{ |
|
177 |
return imagejpeg($image, $outfile, 100); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
?> |