3 if(! class_exists("Photo")) {
11 public function __construct($data) {
13 $this->image = @imagecreatefromstring($data);
14 if($this->image !== FALSE) {
15 $this->width = imagesx($this->image);
16 $this->height = imagesy($this->image);
21 public function __destruct() {
23 imagedestroy($this->image);
26 public function is_valid() {
30 public function getWidth() {
34 public function getHeight() {
38 public function getImage() {
42 public function scaleImage($max) {
44 $width = $this->width;
45 $height = $this->height;
47 $dest_width = $dest_height = 0;
49 if((! $width)|| (! $height))
52 if($width > $max && $height > $max) {
53 if($width > $height) {
55 $dest_height = intval(( $height * $max ) / $width);
58 $dest_width = intval(( $width * $max ) / $height);
65 $dest_height = intval(( $height * $max ) / $width);
68 if( $height > $max ) {
69 $dest_width = intval(( $width * $max ) / $height);
74 $dest_height = $height;
80 $dest = imagecreatetruecolor( $dest_width, $dest_height );
81 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
83 imagedestroy($this->image);
85 $this->width = imagesx($this->image);
86 $this->height = imagesy($this->image);
90 public function rotate($degrees) {
91 $this->image = imagerotate($this->image,$degrees,0);
92 $this->width = imagesx($this->image);
93 $this->height = imagesy($this->image);
98 public function scaleImageUp($min) {
100 $width = $this->width;
101 $height = $this->height;
103 $dest_width = $dest_height = 0;
105 if((! $width)|| (! $height))
108 if($width < $min && $height < $min) {
109 if($width > $height) {
111 $dest_height = intval(( $height * $min ) / $width);
114 $dest_width = intval(( $width * $min ) / $height);
119 if( $width < $min ) {
121 $dest_height = intval(( $height * $min ) / $width);
124 if( $height < $min ) {
125 $dest_width = intval(( $width * $min ) / $height);
129 $dest_width = $width;
130 $dest_height = $height;
136 $dest = imagecreatetruecolor( $dest_width, $dest_height );
137 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
139 imagedestroy($this->image);
140 $this->image = $dest;
141 $this->width = imagesx($this->image);
142 $this->height = imagesy($this->image);
148 public function scaleImageSquare($dim) {
150 $dest = imagecreatetruecolor( $dim, $dim );
151 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
153 imagedestroy($this->image);
154 $this->image = $dest;
155 $this->width = imagesx($this->image);
156 $this->height = imagesy($this->image);
160 public function cropImage($max,$x,$y,$w,$h) {
161 $dest = imagecreatetruecolor( $max, $max );
162 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
164 imagedestroy($this->image);
165 $this->image = $dest;
166 $this->width = imagesx($this->image);
167 $this->height = imagesy($this->image);
170 public function saveImage($path) {
171 $quality = get_config('system','jpeg_quality');
172 if((! $quality) || ($quality > 100))
173 $quality = JPEG_QUALITY;
174 imagejpeg($this->image,$path,$quality);
177 public function imageString() {
180 $quality = get_config('system','jpeg_quality');
181 if((! $quality) || ($quality > 100))
182 $quality = JPEG_QUALITY;
184 imagejpeg($this->image,NULL,$quality);
185 $s = ob_get_contents();
192 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
194 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
198 $guid = $r[0]['guid'];
202 $r = q("INSERT INTO `photo`
203 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
204 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
209 dbesc(datetime_convert()),
210 dbesc(datetime_convert()),
211 dbesc(basename($filename)),
213 intval($this->height),
214 intval($this->width),
215 dbesc($this->imageString()),
233 function import_profile_photo($photo,$uid,$cid) {
237 $photo_failure = false;
239 $filename = basename($photo);
240 $img_str = fetch_url($photo,true);
241 $img = new Photo($img_str);
242 if($img->is_valid()) {
244 $img->scaleImageSquare(175);
246 $hash = photo_new_resource();
248 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
251 $photo_failure = true;
253 $img->scaleImage(80);
255 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
258 $photo_failure = true;
260 $img->scaleImage(48);
262 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
265 $photo_failure = true;
269 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
270 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
271 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.jpg';
274 $photo_failure = true;
277 $photo = $a->get_baseurl() . '/images/person-175.jpg';
278 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
279 $micro = $a->get_baseurl() . '/images/person-48.jpg';
282 return(array($photo,$thumb,$micro));