]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
bd7cf856031813fda557fbd2f0cb278a37d8eeb3
[friendica.git] / mod / wall_upload.php
1 <?php
2
3 require_once('Photo.php');
4
5 function wall_upload_post(&$a) {
6
7         if(! local_user()) {
8                 echo ( "Permission denied." . EOL );
9                 killme();
10         }
11
12         if(! x($_FILES,'userfile'))
13                 killme();
14
15         $src      = $_FILES['userfile']['tmp_name'];
16         $filename = basename($_FILES['userfile']['name']);
17         $filesize = intval($_FILES['userfile']['size']);
18
19         $imagedata = @file_get_contents($src);
20         $ph = new Photo($imagedata);
21
22         if(! ($image = $ph->getImage())) {
23                 echo ("Unable to process image." . EOL);
24                 @unlink($src);
25                 killme();
26         }
27
28         @unlink($src);
29
30         $width = $ph->getWidth();
31         $height = $ph->getHeight();
32
33         $hash = hash('md5',uniqid(mt_rand(),true));
34         
35         $str_image = $ph->imageString();
36         $smallest = 0;
37
38         $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`, 
39                 `height`, `width`, `data`, `scale` )
40                 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 0 )",
41                 intval($_SESSION['uid']),
42                 dbesc($hash),
43                 datetime_convert(),
44                 datetime_convert(),
45                 dbesc(basename($filename)),
46                 intval($height),
47                 intval($width),
48                 dbesc($str_image));
49         if(! $r) {
50                 echo ("Image upload failed." . EOL);
51                 killme();
52         }
53
54         if($width > 640 || $height > 640) {
55                 $ph->scaleImage(640);
56
57                 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`, 
58                         `height`, `width`, `data`, `scale` )
59                         VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 1 )",
60                         intval($_SESSION['uid']),
61                         dbesc($hash),
62                         datetime_convert(),
63                         datetime_convert(),
64                         dbesc(basename($filename)),
65                         intval($ph->getHeight()),
66                         intval($ph->getWidth()),
67                         dbesc($ph->imageString())
68                 );
69                 if($r) 
70                         $smallest = 1;
71         }
72
73         if($width > 320 || $height > 320) {
74                 $ph->scaleImage(320);
75
76                 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`, 
77                         `height`, `width`, `data`, `scale` )
78                         VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 2 )",
79                         intval($_SESSION['uid']),
80                         dbesc($hash),
81                         datetime_convert(),
82                         datetime_convert(),
83                         dbesc(basename($filename)),
84                         intval($ph->getHeight()),
85                         intval($ph->getWidth()),
86                         dbesc($ph->imageString())
87                 );
88                 if($r)
89                         $smallest = 2;
90         }
91
92         $basename = basename($filename);
93         echo  "<br /><br /><img src=\"".$a->get_baseurl(). "/photo/{$hash}-{$smallest}.jpg\" alt=\"$basename\" /><br /><br />";
94
95         killme();
96         return; // NOTREACHED
97 }