PHP - Thumbnails Look Pretty Bad
you can see it at the link below on my site. The generated thumbnails look terrible but I don't know why. Any help greatly appreciated. thank you . (code below of upload script and viewgallery script)
http://ealike.com/gallery/viewgallery.php?cid=1 upload script Code: [Select] <?php include("config.inc.php"); // initialization $result_final = ""; $counter = 0; // List of our known photo types $known_photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); // GD Function List $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); // Fetch the photo array sent by preupload.php $photos_uploaded = $_FILES['photo_filename']; // Fetch the photo caption array $photo_caption = $_POST['photo_caption']; while( $counter <= count($photos_uploaded) ) { if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) { $result_final .= "File ".($counter+1)." is not a photo<br />"; } else { mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); // Store the orignal file copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename); // Let's get the Thumbnail size $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) { $thumbnail_width = 100; $thumbnail_height = (int)(100 * $size[1] / $size[0]); } else { $thumbnail_width = (int)(100 * $size[0] / $size[1]); $thumbnail_height = 100; } // Build Thumbnail with GD 1.x.x, you can use the other described methods too $function_suffix = $gd_function_suffix[$filetype]; $function_to_read = "ImageCreateFrom".$function_suffix; $function_to_write = "Image".$function_suffix; // Read the source file $source_handle = $function_to_read ( $images_dir."/".$filename ); if($source_handle) { // Let's create an blank image for the thumbnail $destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height ); // Now we resize it ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] ); } // Let's save the thumbnail $function_to_write( $destination_handle, $images_dir."/tb_".$filename ); ImageDestroy($destination_handle ); // $result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />"; } } $counter++; } // Print Result echo <<<__HTML_END <html> <head> <title>Photos uploaded</title> </head> <body> $result_final </body> </html> __HTML_END; ?> DISPLAY THUMBNAILS AND IMAGES SCRIPT Code: [Select] <?php include("config.inc.php"); // initialization $result_array = array(); $counter = 0; $cid = (int)($_GET['cid']); $pid = (int)($_GET['pid']); // Category Listing if( empty($cid) && empty($pid) ) { $number_of_categories_in_row = 4; $result = mysql_query( "SELECT c.category_id,c.category_name,COUNT(photo_id) FROM gallery_category as c LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id GROUP BY c.category_id" ); while( $row = mysql_fetch_array( $result ) ) { $result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."</a> "."(".$row[2].")"; } mysql_free_result( $result ); $result_final = "<tr>\n"; foreach($result_array as $category_link) { if($counter == $number_of_categories_in_row) { $counter = 1; $result_final .= "\n</tr>\n<tr>\n"; } else $counter++; $result_final .= "\t<td>".$category_link."</td>\n"; } if($counter) { if($number_of_categories_in_row-$counter) $result_final .= "\t<td colspan='".($number_of_categories_in_row-$counter)."'> </td>\n"; $result_final .= "</tr>"; } } // Thumbnail Listing else if( $cid && empty( $pid ) ) { $number_of_thumbs_in_row = 5; $result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos WHERE photo_category='".addslashes($cid)."'" ); $nr = mysql_num_rows( $result ); if( empty( $nr ) ) { $result_final = "\t<tr><td>No Category found</td></tr>\n"; } else { while( $row = mysql_fetch_array( $result ) ) { $result_array[] = "<a href='viewgallery.php?cid=$cid&pid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>"; } mysql_free_result( $result ); $result_final = "<tr>\n"; foreach($result_array as $thumbnail_link) { if($counter == $number_of_thumbs_in_row) { $counter = 1; $result_final .= "\n</tr>\n<tr>\n"; } else $counter++; $result_final .= "\t<td>".$thumbnail_link."</td>\n"; } if($counter) { if($number_of_photos_in_row-$counter) $result_final .= "\t<td colspan='".($number_of_photos_in_row-$counter)."'> </td>\n"; $result_final .= "</tr>"; } } } // Full Size View of Photo else if( $pid ) { $result = mysql_query( "SELECT photo_caption,photo_filename FROM gallery_photos WHERE photo_id='".addslashes($pid)."'" ); list($photo_caption, $photo_filename) = mysql_fetch_array( $result ); $nr = mysql_num_rows( $result ); mysql_free_result( $result ); if( empty( $nr ) ) { $result_final = "\t<tr><td>No Photo found</td></tr>\n"; } else { $result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" ); list($category_name) = mysql_fetch_array( $result ); mysql_free_result( $result ); $result_final .= "<tr>\n\t<td> <a href='viewgallery.php'>Categories</a> > <a href='viewgallery.php?cid=$cid'>$category_name</a></td>\n</tr>\n"; $result_final .= "<tr>\n\t<td align='center'> <br /> <img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' /> <br /> $photo_caption </td> </tr>"; } } // Final Output echo <<<__HTML_END <html> <head> <title>Gallery View</title> </head> <body> <table width='100%' border='0' align='center' style='width: 100%;'> $result_final </table> </body> </html> __HTML_END; ?> Similar TutorialsI am getting Use of undefined constant id - assumed 'id' with this: $query = "SELECT * FROM products WHERE id = ".mysql_real_escape_string($_POST[id]).""; That seems pretty bull shit to me. Is this a bug in PHP? I would like to take the following xml file, and convert it to a nice array. Is there any functions to do this or any suggestions how to do this? xml file: Code: [Select] <?xml version="1.0" encoding="UTF-8"?> <users> <instructions> <phpfile>users.php</phpfile> </instructions> <users action="multiple"> <user> <name>root</name> <password>5f4dcc3b5aa765d61d8327deb882cf99</password> <permissions> <access>CREATE_USR</access> </permissions> </user> <users> </users> Desired array: Code: [Select] Array ( [instructions] => Array ( [phpfile] => users.php ) [users] => Array ( [0] => Array ( [name] => root [password] => 5f4dcc3b5aa765d61d8327deb882cf99 [permissions] => Array( [access] => CREATE_USR ) ) ) ) I'm in the process of building a network for investors and entrepreneurs. When a user logs in, they are directed to profile.php where they can have access to their profile information. If you go into the address bar and type in network.jasonbiondo.com/jasonbiondo you will not be able to view my profile, but if you type in http://network.jasonbiondo.com/profile. ... asonbiondo , you will be able to see my profile. I want to make it cleaner so that you can access profiles by going directly to network.jasonbiondo.com/jasonbiondo . I think my .htaccess file is causing the problem. I have listed my code below. .htaccess Code: [Select] Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.*)$ ./profile.php?u=$1 [NC] http://i.imgur.com/6olCy.png The picture (in the imgur link) pretty much describes what I'm trying to do. As it stands, it would take too long to simply do everything manually with html and css (both of which I'm pretty good at I think), moving all img src and dates from one div tag to another whenever I wanna add something. So yeah, what's the most efficient way to simply submit a picture, and all my other pictures will move? I know some of you guys will say to just learn php, but quite frankly, I don't have the time... and all the php books are ~1000pgs long which seems really daunting. I know, my problem's pretty basic and it'll probably be covered in the first 100pages or so, but still, like I said I don't have that much time for the moment. When I do, however, can you guys recommend me the quickest guide/video series/book to learn practical php? Not the most comprehensive, as the stuff I'm going to be doing's not that complex. One last thing, I've been hearing about CakePHP; what is it? I read that it's an "open source web application framework", but I still have little clue. I'm currently using dreamweaver for all my web stuff (no I do not use the WYSIWYG feature, I just grown accustom to using it). Should I "learn cakephp"? Please answer any one of those questions (particularly the first one). Many thanks and I'll be sure to reply! <?php if (!isset($_GET['register'])) { echo "register stuff here"; } elseif (!isset($_GET['login'])) { echo "login stuff here"; } elseif (!isset($_GET['forgotpassword'])) { echo "password recovery stuff here"; } else { echo "default page stuff here"; } ?> you get what i'm going for, but it kinda messes up this way with different contents on different places? help As a freelancer working alone, I've got to wear all the hats, doing all the front end and back end design and development. So perhaps I'm never really specializing in any one thing, and I tend to miss some good stuff, like MySQL GROUP BY. Doing counts of things using GROUP BY is very handy. I saw somebody doing that in a thread, and adapted it to two things that I was working on in the last week. Sweet!
What other fun MySQL stuff am I missing out on?
I found this online at http://php.net/manual/en/function.time.php: <?php function rel_time($from, $to = null) { $to = (($to === null) ? (time()) : ($to)); $to = ((is_int($to)) ? ($to) : (strtotime($to))); $from = ((is_int($from)) ? ($from) : (strtotime($from))); $units = array ( "year" => 31536000, // seconds in a year (12 months) "month" => 2628000, // seconds in a month (4 weeks) "week" => 604800, // seconds in a week (7 days) "day" => 86400, // seconds in a day (24 hours) "hour" => 3600, // seconds in an hour (60 minutes) "minute" => 60, // seconds in a minute (60 seconds) "second" => 1 // 1 second ); $diff = abs($from - $to); $suffix = (($from > $to) ? ("from now") : ("ago")); foreach($units as $unit => $mult) if($diff >= $mult) { $and = (($mult != 1) ? ("") : ("and ")); $output .= ", ".$and.intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : ("s")); $diff -= intval($diff / $mult) * $mult; } $output .= " ".$suffix; $output = substr($output, strlen(", ")); return $output; } ?> ======================= Basically, if you type <?php echo rel_time("March 12, 2011 7:00 PM"); ?>, it will show you the time ago relative to the current time. But there's two problems. 1) The time it sends out is about 5hrs early (the actual time is 7:00pm, I set the rel time to 7:00pm, but the time it displays reads '5 hours ago, 1 minute, and 3 seconds ago'. I'm not much of a coder, but judging by the looks of it, it would appear correct... but apparently not. 2) My second gripe is that the code shows too many increments of date. What I mean is, if if I set the date to over a year ago, it would display the years,months,weeks,days,minutes,seconds. That is way too much increments. I just want it to display seconds, if I submitted it seconds ago, just display minutes if it was displayed minutes ago, and display days if it was submitted days ago. You get the idea. Friends, family, fellow coders, and bored people- can you help a nooblet? Thanks! $username = $loggedInUser->username; // This is the logged in username $time = time(); $makedir = $username.'_'.$time; $var = getcwd(); $var = str_replace('\users', '\imageuploads', $var); $dirlocation = $var."\\".test_directory($username, $mysqli); function test_directory ($username, $mysqli) { $stmt = $mysqli->prepare("SELECT Temp_Directory FROM uc_users WHERE user_name LIKE ?"); $stmt->bind_param("s", $username); $stmt->execute(); $stmt->bind_result($Tempdir); while ($stmt->fetch()){ return $Tempdir; } } if((!empty(test_directory($username, $mysqli))) && is_dir($dirlocation)){ //echo "this is it"; $thedirectory = $dirlocation; } if(empty(test_directory($username, $mysqli))){ //echo "it's not a directory"; $newdir = $var."\\".$makedir; $query = mysqli_query($mysqli, "UPDATE uc_users SET Temp_Directory='$makedir' WHERE user_name='$username'"); if(!$query){ //echo mysqli_error($mysqli); } mkdir($newdir); //security chmod($newdir, 0644); $thedirectory = $newdir; } if(!is_dir($dirlocation) && (!empty(test_directory($username, $mysqli)))){ //echo "third one"; mkdir($dirlocation); chmod($dirlocation, 0644); $thedirectory = $dirlocation; } Ok, so what I'm doing here is testing to see whether a a record exists of the user having a folder in the MySQL database. Then, if it does, make sure that a folder exists at that location. If there is no folder, we create one for the user. If there is already a folder, we leave it alone. This is for image uploads, and $thedirectory, is where we upload images later on in the script. Hope that makes sense. The code seems to work. But how can I improve it and make it more robust? Or should I just leave it alone? Should I return FALSE from the function for better reliability over empty()? Does anybody know a good tutorial for creating thumbnails for these three file extensions "jpg, gif and png" in PHP?
I am only finding ones for "jpg".
Hello, I have a file upload site for my IRC channel, and I'm using timthumb to generate thumbnails. The problem is, I allow users to upload other than image files and they obviously show as blank now on my website. Any ideas how I could show custom made thumbnails for files such as .rar? i.e. user uploads a rar file, and my site instead trying to do this: Code: [Select] <div class='tiedosto'><a href='up/rss.rar' class='tiedosto screenshot' rel='up/rss.rar' ><img src='thumb.php?src=up/rss.rar&h=100&w=100&zc=1' alt=''></a></div>it would show this Code: [Select] <div class='tiedosto'><a href='up/rss.rar' class='tiedosto screenshot' rel='up/rss.rar' ><img src='thumb.php?src=thumb/rar.png&h=100&w=100&zc=1' alt=''></a></div>or just Code: [Select] <div class='tiedosto'><a href='up/rss.rar' class='tiedosto screenshot' rel='up/rss.rar' ><img src='thumb/rar.png'></a></div> Thanks. I am looking at the ImageMagick library to create thumbnails from about 800 pdf files. I'd like to use php because the information needs to be stored in a database. Is ImageMagick up to the task? Would you recommend an alternative library? Any other advice is appreciated. Thanks, -Brian Hi guys, I have a simple code below which allows to me to upload image to a file directory and save the location of my file in database the only thing i dont understand is how to 1-resize the images 2- create a thumbnail while uploading 3- allow only jpeg & JPG files to be uploaded with 200kb max also, could you tell me what is the best size i can store the images in? thanks in advance //image1 $nameone=$_FILES['myfileone']['name']; $tmp_name= $_FILES['myfileone']['tmp_name']; if ($nameone) { $locationone="images/$nameone"; move_uploaded_file($tmp_name, $locationone); $image1 = mysql_query ("UPDATE user SET image1='$locationone'"); } Hello, I'm very new to working with images and headers, i've never done it at all even. But i've been trying to make a gallery its got about 150 photos in it. And i know there are much better ways than me resizeing and saving 150 thumbs. So ive tried to research and implement a code to make this so i can recall the original images url from a database and this will be the end viewable picture. And use php to create a thumbnail from the original, and then make that a link so it moves onto a lightbox viewer. this is the code i've got so far, i'm having problems with the headers. I know that they must be put first but when i move the header to the start of file it still errors. I'd really appreciate any help that people would be willing to give <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-type" /> <title></title> <link rel="alternate" type="application/rss+xml" title="" href="http://www.loukafinefinish.com/rss.xml" /> <link rel="stylesheet" type="text/css" href="css/portfolio.css" /> <link rel="stylesheet" type="text/css" href="css/portfolio_image.css" /> <script type="text/javascript" src="js/cufon-yui.js"></script> <script type="text/javascript" src="js/arial.js"></script> <script type="text/javascript" src="js/cuf_run.js"></script> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/portfolio.js"></script> <script type="text/javascript" src="js/jquery.lightbox-0.5.js"></script> <link rel="stylesheet" type="text/css" href="css/jquery.lightbox-0.5.css" media="screen" /> <script type="text/javascript"> $(function() { $('.thumb a').lightBox(); }); </script> <style type="text/css"> /* Reset CSS */ html, body, h1, h2, h3, h4, h5, h6, p, ol, ul, li, pre, code, address, variable, form, fieldset, blockquote { padding: 0; margin: 0; font-size: 100%; font-weight: normal; } table { border-collapse: collapse; border-spacing: 0; } td, th, caption { font-weight: normal; text-align: left; } img, fieldset { border: 0; } ol { padding-left: 1.4em; list-style: decimal; } ul { padding-left: 1.4em; list-style:square; } q:before, q:after { content:''; } /* End of CSS Reset*/ </style> </head> <body> <div id="content-wrapper"> <div id="header"> <h1><a id="logo" href="index.html"><span>Carpentery</span></a></h1> </div> <div id="navigation"> <div class="menu_nav"> <ul> <li><a href="index.html">Home</a></li> <li><a href="services.htm</ul> <--header above etc--> <?php echo '<div class="container"> <ul class="thumb">'; require_once('include/connect.php'); $display = 5; // Number of results if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Determine number of pages $pages = $_GET['p']; } else { // Need to determine $q = "SELECT COUNT(id) FROM portfolio"; $r = @mysqli_query($dbc, $q); $row = @mysqli_fetch_array($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if($records > $display) { // More than 1 page $pages = ceil($records/$display); } else { $pages = 1; } } // End IF if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Query $q = "SELECT imageUrl FROM portfolio ORDER BY id LIMIT $start, $display"; $r = @mysqli_query($dbc, $q); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $filename[] = $row['imageUrl']; foreach ($filename as $url) { // The file $filename = $url; // Set a maximum height and width $width = 100; $height = 100; header('Content-type: image/jpeg'); // Header is here for my example, but i have tried // placing it at the top of my page // Get new dimensions list($width_orig, $height_orig) = getimagesize($filename); $ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } // Resample $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); echo '<li><a href=' . $row['imageUrl'] . '>' . imagejpeg($image_p, null, 100) . ' </a>'; } // End of FOR } // End while echo '</ul>'; // End of list of images mysqli_free_result($r); mysqli_close($dbc); if ($pages > 1) { // Make page links echo '<br /><p class="pagination">'; $current_page = ($start/$display) + 1; // What page script is on if($current_page != 1) { // Make previous button echo '<a href="portfolio.php?s=' . ($start - $display) . '&p=' . '">Previous</a> '; } // Make all the pages numbered for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="portfolio.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // END of FOR loop // If no last page, make net button if ($current_page != $pages) { echo '<a href="portfolio.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; } // End page links ?> <!--Footer etc--> </html> This is my overall page, but as the page had some html involved i thought it best to post some of it. I'm not convinced at all that my method for iterating through the database for the image urls allows me to then iterate this into the $filename for the image resize. If any one could help i'd appreciate it Thank you Hi all, Could someone help me add a thumbnail script to the below that works on scaling it down to 200px x 133px. I guess it is not that hard. <?php $destination='aircraft/'.$reg."1.jpg"; $temp_file = $_FILES['image']['tmp_name']; move_uploaded_file($temp_file,$destination); ?> Thanks This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=358438.0 Hi, You may noticed that i've posted before and closed them. It was too hard. I've been searching around for something else and i found it. But it's not quite perfect. What i want to do is make thumbnails smaller (i can do that on my own) but displaying the bigger picture above the thumbnails. And aligning the thumbnails next to each other. So how do i do that? The current code: <?php $result = mysql_query("SELECT reference FROM user_photos WHERE`profile_id`='".$row['id']."'"); while ($row2 = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<a href=\"".$_GET['username']."/pics/".$row2['reference']."\"> <img src=\"".$_GET['username']."/pics/thumbs/".$row2['reference']."\"></a><br/><br/>"; } } ?> The files are refering to the mysql in reference but are actually in a subfolder with the username on the server. I hope somebody wants to help me, Best regards, Martijn (the netherlands) Hi all. I was wondering if there was a way (other than retrieving db fields with a query) to show something like "customer_id" as "Customer ID". Is there a functionality of this nature in either MySQL or PHP? I'm aware of preg_replace and such but that would still require two queries; one for the field names and one for the data (right?) |