- The Real Viking
- Medlem ●
- Karlstad
- 2013-08-02 15:43
Koden funkar fint om jag sätter $skip = 1, men inte när jag sätter $skip = 0, dvs när $skip = 0, skall databasen förminska bilden innan den sparas i databasen. När $skip = 1 förminskas bilden ej. Vad gör jag för fel?
<html> <head> <title>Test plattform</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="robots" content="noindex,nofollow" /> </head> <body> <?php $database = "PictureStorage"; $server = "192.168.0.8"; $db_user = "xxx"; $db_password = "xxxx"; function resizeImage ($picture,$max_height) { // get originalsize of image $im = imagecreatefromstring($picture); $width = imagesx($im); $height = imagesy($im); // Set thumbnail-height to x pixels $imgh = $max_height; // calculate thumbnail-height from given width to maintain aspect ratio $imgw = round($width / $height * $imgh); // create new image using thumbnail-size $resizedImage=imagecreatetruecolor($imgw,$imgh); // copy original image to thumbnail imagecopyresampled($resizedImage,$im,0,0,0,0,$imgw,$imgh,imagesx($im),imagesy($im)); //makes thumb $picture_array = array($resizedImage,$imgw, $imgh); return $picture_array; } if ($_POST['submit']) { if (isset($_FILES['file']) && $_FILES['file']['size'] > 0) { $picture_ID = 1; $skip = 1; echo "Reading file…<br>\n"; $fileName = $_FILES['file']['name']; $fileSize = $_FILES['file']['size']; $fileType = $_FILES['file']['type']; // Temporary file name stored on the server $tmpName = $_FILES['file']['tmp_name']; // Read the file $fp =fopen($tmpName, 'r'); $data1 = fread($fp, filesize($tmpName)); $data = addslashes($data1); $picture = $data; fclose ($fp); echo "Temporary file name: $tmpName<br>\n"; // imagejpeg($data1, null, 100); // Resize picture echo "Inserting picture in normal size into database<br>\n"; if ($skip == 0) { $picture_array = resizeImage ($data1, 480); $picture = $picture_array[0]; $imgw = $picture_array[1]; $imgh = $picture_array[2]; } else { $im = imagecreatefromstring($data1); $imgw = imagesx($im); $imgh = imagesy($im); } // imagejpeg($picture, null, 100); $link = mysqli_connect($server, $db_user, $db_password); if (mysqli_connect_errno()) { printf("<b>Connection failed:</b> %s<br>", mysqli_connect_error()); } else { mysqli_select_db($link, $database); $result = mysqli_query($link,"SET global max_allowed_packet=1073741824") or die( "<b>An error has occurred:</b> " .mysqli_error($link). ". <b>Error number:</b> " .mysqli_errno($link)); if ($picture_ID) { $query = ("UPDATE PictureStorage SET Data_normal = '$picture', Name_normal = '$fileName', Type_normal = '$fileType', Pixel_width_normal = '$imgw', Pixel_height_normal = '$imgh' WHERE Picture_ID = '$picture_ID' "); } else { $query = ("INSERT PictureStorage (Data_normal, Name_normal, Type_normal, Pixel_width_normal, Pixel_height_normal) VALUES ('$picture','$fileName','$fileType','$imgw','$imgh')"); } $result = mysqli_query($link,$query) or die( "<b>An error has occurred:</b> " .mysqli_error($link). ". <b>Error number:</b> " .mysqli_errno($link)); } mysqli_close($link); echo "Inserting picture in mini size into database<br>\n"; // Thumbnail size if ($skip == 0) { $picture_array = resizeImage ($data1, 120); $picture_mini = $picture_array[0]; $imgw_mini = $picture_array[1]; $imgh_mini = $picture_array[2]; } else { $im_mini = imagecreatefromstring($data1); $imgw_mini = imagesx($im_mini); $imgh_mini = imagesy($im_mini); } $link = mysqli_connect($server, $db_user, $db_password); if (mysqli_connect_errno()) { printf("<b>Connection failed:</b> %s<br>", mysqli_connect_error()); } else { mysqli_select_db($link, $database); $result = mysqli_query($link,"SET global max_allowed_packet=1073741824") or die( "<b>An error has occurred:</b> " .mysqli_error($link). ". <b>Error number:</b> " .mysqli_errno($link)); if ($picture_ID) { $query = ("UPDATE PictureStorage SET Data_mini = '$picture_mini', Name_mini = '$fileName', Type_mini = '$fileType', Pixel_width_mini = '$imgw_mini', Pixel_height_mini = '$imgh_mini' WHERE Picture_ID = '$picture_ID' "); } else { $query = ("INSERT PictureStorage (Data_mini, Name_mini, Type_mini, Pixel_width_mini, Pixel_height_mini) VALUES ('$picture_mini','$fileName','$fileType','$imgw_mini','$imgh_mini')"); } $result = mysqli_query($link,$query) or die( "<b>An error has occurred:</b> " .mysqli_error($link). ". <b>Error number:</b> " .mysqli_errno($link)); } mysqli_close($link); } else { echo "Could not read file…<br>\n"; } } else { $link = mysqli_connect($server, $db_user, $db_password); if (mysqli_connect_errno()) { printf("<b>Connection failed:</b> %s<br>", mysqli_connect_error()); } else { mysqli_select_db($link, $database); $query = ("SELECT * FROM PictureStorage"); $result = mysqli_query($link,$query) or die( "<b>An error has occured:</b> " .mysqli_error($link). ". <b>Error number:</b> " .mysqli_errno($link)); $numberofitems = 0; echo "<table border='1' cellspacing='0' cellpadding='2'>\n"; echo "<tr><th></th><th colspan='5'>Normal size</th><th></th><th colspan='5'>Mini size</th></tr>\n"; echo "<tr><th>ID</th><th>Picture</th><th>File name</th><th>Type</th><th>Width</th><th>Height</th><th>||</th><th>Picture</th><th>File name</th><th>Type</th><th>Width</th><th>Height</th></tr>\n"; while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { $ID = $row['Picture_ID']; $name = $row['Name_normal']; $type = $row['Type_normal']; $width = $row['Pixel_width_normal']; $height = $row['Pixel_height_normal']; $picture = $row['Data_normal']; $name_mini = $row['Name_mini']; $type_mini = $row['Type_mini']; $width_mini = $row['Pixel_width_mini']; $height_mini = $row['Pixel_height_mini']; $picture_mini = $row['Data_mini']; echo "<tr>"; echo "<td>" . $ID . "</td>"; echo "<td>"; echo "<img src='data:$type;base64, ". base64_encode($picture) . "' alt='En bild'>"; //echo "<img src='data:image/jpg; ". $picture . "' alt='En bild'>"; //imagejpeg($picture, NULL, 100); echo "</td>"; echo "<td>" . $name . "</td>"; echo "<td>" . $type . "</td>"; echo "<td>" . $width . "</td>"; echo "<td>" . $height . "</td>"; echo "<td>||</td>"; echo "<td>"; echo "<img src='data:$type;base64, ". base64_encode($picture_mini) . "' alt='En mini bild'>"; echo "</td>"; echo "<td>" . $name_mini . "</td>"; echo "<td>" . $type_mini . "</td>"; echo "<td>" . $width_mini . "</td>"; echo "<td>" . $height_mini . "</td>"; echo "</tr>\n"; $numberofitems++; } echo "<tr><th colspan=12>" . $numberofitems; if ($numberofitems > 1) { echo " items"; } else { echo " item"; } echo "</th></tr>\n"; echo "</table>\n"; } $html .= "<hr>"; $html .= "<form action='Test4.php' method='post' enctype='multipart/form-data'>"; $html .= "<label for = 'file'>Filename:</label>"; $html .= "<input type='file' name='file' id='file'><br>"; $html .= "<input type='submit' name='submit' value='Submit'>"; $html .= "</form>"; echo $html; } ?> </body> </html>
Senast redigerat 2013-08-02 15:55