· 8 years ago · Jun 01, 2018, 11:04 AM
1<?php
2if(isset($_POST['id']))
3{
4 // conect to the db here
5
6
7 // get the variables, clean em
8 // width and height are max values
9 // twidth and theight are thumbnail max values
10 $address = htmlspecialchars(trim($_POST['address']));
11 $mls = trim($_POST['mls']);
12 $directions = trim(htmlentities($_POST['directions']));
13 $bed = $_POST['bed'];
14 $bath = $_POST['bath'];
15 $sqft = trim($_POST['sqft']);
16 $price =htmlentities($_POST['price']);
17 $type = $_POST['type'];
18 $year = $_POST['year'];
19 $agent = $_POST['id'];
20 $listingType = $_POST['listingType'];
21 $description = htmlspecialchars($_POST['description']);
22 $width = '640';
23 $height = '640';
24 $thumbName = "images/listings/$mls/thumb.jpg";
25 $twidth = '250';
26 $theight = '250';
27
28 // make sure they posted a mls number
29 if(empty($_POST['mls']))
30 {
31 die("<p>You didn't enter a mls number. Please go back and try again.</p>");
32 }
33
34 // if that mls listing folder exists quit
35 if(file_exists("images/listings/$mls"))
36 {
37 die ("<p>You are trying to add a listing that already exists please check the mls number or delete mls number $mls.</p>");
38 }
39
40
41 // create the directory and assign the filename path
42 $oldmask = umask(0);
43 mkdir("images/listings/$mls", 0777);
44 umask($oldmask);
45 $ext = '.jpg';
46 $fileName = "images/listings/$mls/";
47
48 // if there is a file put the name in an array
49 for ($i = 0; $i <10; $i++)
50 if(!empty($_FILES["upload$i"]['name']))
51 {
52 $pic[$i] = $_FILES["upload$i"]['name'];
53 }
54
55 // count the number of pictures to be uploaded
56 $totalPics = count($pic);
57
58 // loop this for each file to be uploaded
59 for($i = 0; $i<$totalPics; $i++)
60 // make sure its a jpeg
61 if (!eregi('^image/p?jpeg(;.*)?$', $_FILES["upload$i"]['type']))
62 {
63 // we already added the folder so if we kill the script take out the folder
64 rmdir("images/listings/$mls");
65 die ("<p>Sorry, one of the files you tried to upload was not jpeg. Please try again and make sure you use a file with a .jpg extension.</p>");
66 }
67 // if it is a jpeg upload it
68 else
69 {
70 if(is_uploaded_file($_FILES["upload$i"]['tmp_name']) and
71 copy($_FILES["upload$i"]['tmp_name'], "$fileName$i$ext"))
72 {
73 // find the dimensions
74 $simg = imagecreatefromjpeg("$fileName" ."$i". "$ext");
75 $currwidth = imagesx($simg);
76 $currheight = imagesy($simg);
77 // if its a portrait layout
78 if($currheight>$currwidth)
79 {
80 $zoom = $width/$currheight;
81 $newheight = $height;
82 $newwidth = $currwidth*$zoom;
83 }
84 // if its a landscape layout
85 else
86 {
87 $zoom = $width/$currwidth;
88 $newwidth = $width;
89 $newheight = $currheight*$zoom;
90 }
91 // build the new main image
92 $dimg = imagecreatetruecolor($newwidth, $newheight);
93 //imagetruecolortopalette($simg, false, 256);
94 $palsize = imagecolorstotal($simg);
95 for ($e = 0; $e<$palsize; $e++)
96 {
97 $colors = imagecolorsforindex($simg, $e);
98 imagecolorallocate($dimg, $colors['red'], $colors['green'], $colors['blue']);
99 }
100 imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);
101 imagejpeg($dimg, "$fileName" ."$i". "$ext", 90);
102 imagedestroy($simg);
103 imagedestroy($dimg);
104
105 $newheight = round($newheight);
106 $newwidth = round($newwidth);
107
108 // add the main images to the images table
109 $sql = "INSERT INTO images SET
110 upload=CURDATE(),
111 filename='$fileName$i$ext',
112 width='$newwidth',
113 height='$newheight',
114 agentid='$agent',
115 mls='$mls' ";
116
117 if(mysql_query($sql))
118 {
119 $msg1 = "Images uploaded and resized.";
120 }
121 else
122 {
123 $msg1 = "Error uploading and resizing images.";
124 }
125 }
126 }
127
128 //do it all again for the first image to make into a thumbnail
129 //make sure its a jpeg
130 if (!eregi('^image/p?jpeg(;.*)?$', $_FILES["upload0"]['type']))
131 {
132 die ("<p class=\"text\">Sorry, this isn't a jpeg image. Please try again and make sure you use a file with a .jpg extension.</p>");
133 }
134 // if it is a jpeg upload it
135 if(is_uploaded_file($_FILES['upload0']['tmp_name']) and
136 copy($_FILES['upload0']['tmp_name'], "$thumbName"))
137 {
138 $tsimg = imagecreatefromjpeg("$thumbName");
139 $tcurrwidth = imagesx($tsimg);
140 $tcurrheight = imagesy($tsimg);
141 if ($tcurrheight>$tcurrwidth)
142 {
143 $tzoom = $twidth/$tcurrheight;
144 $tnewheight = $theight;
145 $tnewwidth = $tcurrwidth*$tzoom;
146 }
147 else
148 {
149 $tzoom = $twidth/$tcurrwidth;
150 $tnewwidth = $twidth;
151 $tnewheight = $tcurrheight*$tzoom;
152 }
153 $tdimg = imagecreatetruecolor($tnewwidth, $tnewheight);
154 //imagetruecolortopalette($tsimg, false, 256);
155 $tpalsize = imagecolorstotal($tsimg);
156 for ($e = 0; $e<$palsize; $e++)
157 {
158 $colors = imagecolorsforindex($tsimg, $e);
159 imagecolorallocate($tdimg, $colors['red'], $colors['green'], $colors['blue']);
160 }
161 imagecopyresized($tdimg, $tsimg, 0, 0 , 0 , 0, $tnewwidth, $tnewheight, $tcurrwidth, $tcurrheight);
162 imagejpeg($tdimg, $thumbName, 95);
163 imagedestroy($tdimg);
164
165 $tnewheight = round($tnewheight);
166 $tnewwidth = round($tnewwidth);
167
168 }
169// add the listing information to the listing table
170$sql = "INSERT INTO listings SET
171 mls='$mls',
172 upload=CURDATE(),
173 address='$address',
174 directions='$directions',
175 bed='$bed',
176 bath='$bath',
177 sqft='$sqft',
178 price='$price',
179 type='$type',
180 year='$year',
181 agentid='$agent',
182 listingtype='$listingType',
183 description='$description',
184 filename='$thumbName',
185 width='$tnewwidth',
186 height='$tnewheight' ";
187
188 if(mysql_query($sql))
189 {
190 $msg3 = "Listing information for mls number: $mls stored in database.";
191 }
192 else
193 {
194 $msg3 = "ERROR: Listing did not store in database. ". mysql_error() ."<br>";
195 }
196}
197?>