-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapperPicture.php
125 lines (110 loc) · 3.7 KB
/
MapperPicture.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
<?php
/**
* Description of MapperPicture
*
* @author machar
*/
namespace Picture;
use Nette\Image;
class MapperPicture extends \Nette\Object {
/**
*
* @var collection / nazev databaze v mongu
*/
private $collection;
public function __construct(\Mongo $mongo, $db, $table) {
$this->collection = $mongo->{$db}->{$table};
}
/**
*
* @param \MongoId $mongoId
* @param integer $width
* @param integer $height
* @return Picture $picture nebo NULL
*/
public function get(\MongoId $mongoId, $width = null, $height = null) {
$width = (int) $width;
$height = (int) $height;
$obj = $this->collection->findOne(array("_id" => $mongoId));
if ($obj) {
$picture = unserialize($obj["picture"]);
$resolution = ($width == 0 OR $height == 0) ? "default" : $width . "_" . $height;
if ($resolution == "default") {
return $picture;
} else {
if (!isset($obj[$resolution])) {
$img = Image::fromString(base64_decode($picture->getSource()));
if ($img->getWidth() >= $img->getHeight()) {
$img->resize(null, $height);
$img->crop(($img->getWidth() - $width) / 2, 0, $width, $height);
} else {
$img->resize($width, null);
$img->crop(0, ($img->getHeight() - $height) / 2, $width, $height);
}
$img->sharpen();
$img->antialias(true);
$picture->setWidth($width);
$picture->setHeight($height);
$picture->setSource(base64_encode((string) $img));
$this->collection->update(array("_id"=> $mongoId),array('$set' => array($resolution => $picture->getSource())));
} else {
$picture->setWidth($width);
$picture->setHeight($height);
$picture->setSource($obj[$resolution]);
}
}
return $picture;
}
return null;
}
/**
*
* @param \MongoId $mongoId
*/
public function delete(\MongoId $mongoId) {
$this->collection->remove(array("_id" => $mongoId));
}
/**
*
* @param type $haystack
* @param type $needle
* @param type $desc / 1 => true
* @return array \MongoId
*/
public function find($haystack, $needle, $desc) {
$list = array();
$cursor = $this->collection->find(array( $haystack => $needle ));
foreach ($cursor as $ob) {
$list[(string)$ob["_id"]] = $ob["_id"];
}
if ($desc==1) {
rsort($list);
} else {
ksort($list);
}
return $list;
}
/**
*
* @param Picture $picture
* @param ArrayHash $options / doplnujici sloupky
* @return \Picture\Picture
*/
public function save(Picture $picture, $options) {
$list = array();
$list["picture"] = null;
$list["timestamp"] = time();
$picture->setCreateDate(new \Nette\DateTime);
if ($options instanceof \Nette\ArrayHash) {
foreach ($options as $key => $option) {
$list[$key] = $option;
}
}
$this->collection->insert($list, array("secure" => true));
$id = $this->collection->findOne(array(), array("_id" => $list['_id']));
$picture->setIdPicture((string) $id["_id"]);
$this->collection->update(array("_id" => $list["_id"]), array('$set' => array("picture" => serialize($picture))));
return $picture;
}
}
?>