Results 1 to 7 of 7

Thread: GD Image resizing and saving thumb

  1. #1
    Join Date
    Aug 2007
    Location
    Harrisburg, PA
    Posts
    131
    Thanks
    6
    Thanked 9 Times in 9 Posts

    Default GD Image resizing and saving thumb

    ok im not sure what is going on but this isn't working for me. My one lab instructor gave me this script to play around with so I could learn how to create thumb nails dynamically but after I made some changes things aren't working. When I try to upload the image it tells me I haven't provided an image everytime can someone please take a look and see what they think. heres the code
    Code:
    <?php
    $target_path = "../gallery_images/large"; // Where the file is going to be placed. Permissions on this folder must be set to 777
    $this_file = basename($_SERVER['PHP_SELF']); // Sets $this_file to the name of this php file
    include('../php/db_connect.php'); // Includes file to connect to database
    
    // Uploads image to the server and adds entry to database
    if($_POST){	
    	$error_list = array();
    	
    	// Get base image name and description
    	$large = basename($_FILES['large']['large_tmp']);
    
    	$desc = $_POST['desc'];
    	
    	if(empty($large)){
    		$error_list[] = 'Image';
    	}
    	if(empty($desc) OR ($desc == 'Description')){
    		$error_list[] = 'Description';
    	}
    	else{
    		// Replace spaces with '_' and make imagename lowercase
    		$large = ereg_replace(' ','_',"$large");
    		$large = strtolower($large);
    		
    		$target_path = $target_path . $large; // Add the original imagename to our target path.
    		
    		$sql = "SELECT large FROM jchamb_gallery ";
    		$sql .= "WHERE large='$large' ";
    		
    		if (mysql_query($sql)) {
    			$result = mysql_query($sql);
    			$row = mysql_fetch_assoc($result);
    			$image_check = $row['large'];
    		}
    			else{
    				$_FILES['large']['large_tmp']; // Gets temorary image name from the server
    				// Moves the temporary image to its new location
    				if(move_uploaded_file($_FILES['large']['large_tmp'], $target_path)){
    					
    					//////////////////////////// IMAGE SCALE ////////////////////////////////
    					$image_path = "../gallery_images/large/$large"; // Gets temorary image name from the server
    					$dest_path = "../gallery_images/thumb/"; // Sets path to thumbnail images
    
    					// Sets maximum width and height of thumbnail
    					$max_width = 200;
    					$max_height = 150;
    
    					$size = GetImageSize($image_path);
    					$width = $size[0];
    					$height = $size[1];
    					$x_ratio = $max_width / $width;
    					$y_ratio = $max_height / $height;
    
    					if(($width <= $max_width) && ($height <= $max_height)){
    					  $new_width = $width;
    					  $new_height = $height;
    					}
    					elseif(($x_ratio * $height) < $max_height){
    					  $new_height = ceil($x_ratio * $height);
    					  $new_width = $max_width;
    					}
    					else{
    					  $new_width = ceil($y_ratio * $width);
    					  $new_height = $max_height;
    					}
    
    					$src = imagecreatefromjpeg($image_path);
    					$dst = imagecreatetruecolor($new_width,$new_height);
    					imagecopyresized($dst, $src, 0, 0, 0, 0, $new_width,$new_height,$width,$height);
    					$thumb = imagejpeg($dst,"$dest_path"."$large", 90);
    					imagedestroy($src);
    					imagedestroy($dst);
    					//////////////////////////// END IMAGE SCALE ////////////////////////////////
    					$status = "<p>The image ". $image. " has been uploaded!</p>";
    					
    					// Inserts image name into table 'images'
    					$sql = "INSERT INTO jchamb_gallery SET large='$large', thumb='$thumb' desc='$desc'";
    					$result = mysql_query($sql);
    				}
    				else{
    					$status = "<p>There was an error uploading the image, please try again.</p>";
    				}
    			}
    		}
    	}
    	if(count($error_list) > 0) {
    		$status = "<p>You did not provide a(n):</p>\n";
    		foreach($error_list as $error){
    		    $status .= "\t<p><strong>$error</strong></p>\n";
    		}
    	}
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    	<title>Upload Image</title>
    </head>
    <body>
    <div id="wrapper">
    <?php echo $status; ?>
    <form enctype="multipart/form-data" action="<?php echo $this_file; ?>" method="post">
    	<ul id="input">
    		<li><input type="text" name="desc" value="Description" onfocus="if (value=='Description') value='';" onblur="if(value=='') value='Description';" /></li>
    		<li><input type="file" name="large" id="large" /></li>
    	</ul>
    	<p><input type="submit" value="Upload Image" /></p>
    </form>
    
    </div>
    </body>
    </html>
    Thanks, a lot

    Jake

  2. #2
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Well, it seems that some parts of your code are quite messy...

    First, $_FILES['large']['large_tmp'] is invalid, what you probably mean is $_FILES['large']['tmp_name']

    Second, you are concatenating a path without a trailing slash and a filename:
    PHP Code:
    $target_path "../gallery_images/large";
    // ...
    $large basename(/* ... */);
    // ...
    $target_path $target_path $large// This might now look like ../gallery_images/largetmp034823.jpg 
    Yet later you refer to the file as: (why though, you already have $target_path)
    PHP Code:
    $image_path "../gallery_images/large/$large"
    Third, what's the point of the following query? Checking for existence?
    Code:
    SELECT large FROM jchamb_gallery WHERE large='$large'
    You could use COUNT(*) instead.

  3. #3
    Join Date
    Aug 2007
    Location
    Harrisburg, PA
    Posts
    131
    Thanks
    6
    Thanked 9 Times in 9 Posts

    Default

    I fixed what you mentioned about forgetting the slash in the path and the $_FILES['large']['tmp_name']. i wasn't aware that it had to be called 'tmp_name'. but now it doesn't say anything after i hit upload and the image isnt uploaded or created

    Like I said before my lab instructor at my school gave the script to me to so i could mess with it and learn how to create thumbs dynamically

    I took a few things out and really have no clue what that one query is for or if I really need it for anything, and I don't know why it has that second path. I probably made it a lot sloppier when I started messing with it I'm really new at php.

    Do you have suggestions on what might get rid of some the unnessary things, clean up the code and actually make it work.

    thanks for taking the time to look

    Jake

  4. #4
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    The script is pretty messed up and needs to be rewritten. It's quite pointless to hold a name of a temporary file ($large), a boolean value ($thumb) and [the only meaningful] description in the database.

    There are plenty of image upload scripts on Google http://www.google.ee/search?q=image+upload+php

  5. #5
    Join Date
    Aug 2007
    Location
    Harrisburg, PA
    Posts
    131
    Thanks
    6
    Thanked 9 Times in 9 Posts

    Default

    I have to store in a database for the class I am. I have to make a dynamic image gallery using php and mysql. They showed us one way but I wanted to try something new for my actual project. I've rewritten the code but now i'm getting an error that i don't understand

    Warning: move_uploaded_file(../gallery_images/large/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/2008/03/jchamberlain/public_html/wpr/final_project/admin/gallery2.php on line 21

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phplhkr25' to '../gallery_images/large/' in /home/2008/03/jchamberlain/public_html/wpr/final_project/admin/gallery2.php on line 21

    heres my code
    Code:
    <?php
    $this_file = basename($_SERVER['PHP_SELF']);
    
    	if(empty($_POST)){
    	$status = ' ';	
    	}
    	else {
    		$desc = $_POST['desc'];
    		$destination = '../gallery_images/large/';
    		$large = $_FILES['large']['name'];
    		$large_tmp = $_FILES['large']['tmp_name'];
    		$error_list = array();
    		
    		if(empty($large)){
    			$error_list[] = 'Image';
    		}
    		if(empty($desc) OR ($desc == 'Description')){
    			$error_list[] = 'Description';
    		}
    		else {
    				if(move_uploaded_file($large_tmp, $destination)){
    					
    					
    					$image_path = "../gallery_images/large/$large"; // Gets temorary image name from the server
    					$dest_path = "../gallery_images/thumb/"; // Sets path to thumbnail images
    
    					
    					$max_width = 200;
    					$max_height = 150;
    
    					$size = GetImageSize($image_path);
    					$width = $size[0];
    					$height = $size[1];
    					$x_ratio = $max_width / $width;
    					$y_ratio = $max_height / $height;
    
    					if(($width <= $max_width) && ($height <= $max_height)){
    					  $new_width = $width;
    					  $new_height = $height;
    					}
    					elseif(($x_ratio * $height) < $max_height){
    					  $new_height = ceil($x_ratio * $height);
    					  $new_width = $max_width;
    					}
    					else{
    					  $new_width = ceil($y_ratio * $width);
    					  $new_height = $max_height;
    					}
    
    					$src = imagecreatefromjpeg($image_path);
    					$dst = imagecreatetruecolor($new_width,$new_height);
    					imagecopyresized($dst, $src, 0, 0, 0, 0, $new_width,$new_height,$width,$height);
    					imagejpeg($dst,"$dest_path"."$large", 90);
    					$thumb = "$dest_path"."$large";
    					imagedestroy($src);
    					imagedestroy($dst);
    					
    					$status = "<p>The image \". $image. \" has been uploaded!</p>";
    					
    					
    					$sql = "INSERT INTO jchamb_gallery SET large='$large', thumb='$thumb' desc='$desc'";
    					$result = mysql_query($sql);
    					
    				}
    				
    				else{
    					$status = "<p>There was an error uploading the image, please try again.</p>";
    				}
    			}
    		}
    	if(count($error_list) > 0) {
    		$status = "<p>You did not provide a(n):</p>\n";
    		foreach($error_list as $error){
    		    $status .= "\t<p><strong>$error</strong></p>\n";
    		}
    	}	
    		
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    	<div id="wrapper">
    	  <?php echo $status; ?>
          <form enctype="multipart/form-data" action="<?php echo $this_file; ?>" method="post">
              <ul>
                  <li><input name="desc" type="text" onfocus="if (value=='Description') value='';" onblur="if(value=='') value='Description';" value="Description" size="55" /></li>
                  <li><input name="large" type="file" size="55"  /></li>
              </ul>
              <p><input type="submit" value="Upload Image" /></p>
          </form>
        
        </div>
    </body>
    </html>
    This is really stating to drive me crazy.

    thanks for the help though
    Last edited by punstc; 06-24-2008 at 10:29 PM.

  6. #6
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Well, the following works:
    PHP Code:
    <?php
    $this_file 
    basename($_SERVER['PHP_SELF']);
    $error_list = array();

        if(empty(
    $_POST)){
            
    $status ' ';
        }
        else {
            
    $desc $_POST['desc'];
            
    $large basename($_FILES['large']['name']);
            
    $image_path "../gallery_images/large/$large"// Gets temorary image name from the server
            
    $thumb_path "../gallery_images/thumb/$large"// Sets path to the thumbnail image
            
            
            
    if(empty($large)){
                
    $error_list[] = 'Image';
            }
            if(empty(
    $desc) OR ($desc == 'Description')){
                
    $error_list[] = 'Description';
            }
            else {
                    if(
    move_uploaded_file($_FILES['large']['tmp_name'], $image_path)){
                        
    $max_width 200;
                        
    $max_height 150;

                        
    $size GetImageSize($image_path);
                        
    $width $size[0];
                        
    $height $size[1];
                        
    $x_ratio $max_width $width;
                        
    $y_ratio $max_height $height;

                        if((
    $width <= $max_width) && ($height <= $max_height)){
                          
    $new_width $width;
                          
    $new_height $height;
                        }
                        elseif((
    $x_ratio $height) < $max_height){
                          
    $new_height ceil($x_ratio $height);
                          
    $new_width $max_width;
                        }
                        else{
                          
    $new_width ceil($y_ratio $width);
                          
    $new_height $max_height;
                        }

                        
    $src imagecreatefromjpeg($image_path);
                        
    $dst imagecreatetruecolor($new_width,$new_height);
                        
    imagecopyresized($dst$src0000$new_width,$new_height,$width,$height);
                        
    imagejpeg($dst$thumb_path90);
                        
    imagedestroy($src);
                        
    imagedestroy($dst);
                        
                        
    $status '<p>The image "' $large '" has been uploaded!</p>';
                        
                        
                        
    $sql "INSERT INTO jchamb_gallery SET large='$large', thumb='$thumb_path' desc='$desc'";
                        
    $result mysql_query($sql);
                        
                    }
                    
                    else{
                        
    $status "<p>There was an error uploading the image, please try again.</p>";
                    }
                }
            }
        if(
    count($error_list) > 0) {
            
    $status "<p>You did not provide a(n):</p>\n";
            foreach(
    $error_list as $error){
                
    $status .= "\t<p><strong>$error</strong></p>\n";
            }
        }    
            
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>

    <body>
        <div id="wrapper">
          <?php echo $status?>
          <form enctype="multipart/form-data" action="<?php echo $this_file?>" method="post">
              <ul>
                  <li><input name="desc" type="text" onfocus="if (value=='Description') value='';" onblur="if(value=='') value='Description';" value="Description" size="55" /></li>
                  <li><input name="large" type="file" size="55"  /></li>
              </ul>
              <p><input type="submit" value="Upload Image" /></p>
          </form>
        
        </div>
    </body>
    </html>
    I'm still not sure what do you want to store in the database, currently it holds the [base] name of the image, path to the thumbnail (why?, could be just constructed from the image name) and, well, the description.

  7. #7
    Join Date
    Jul 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    There already exist services for performing on-demand image manipulation. You may stop hosting CPU-intensive thumbnailing scripts on your server.

    SteadyOffload (http://steadyoffload.com/thumbnail-problem) can do basic image processing (resizing, cropping, flipping, rotation, etc.) remotely. You just use a couple of custom HTML attributes and the manipulation is conducted on a remote cache server:

    <img srcx="image.jpg" xmanip="RescaleWidth 150" xjpegquality="80" />

    Much easier than all the hassle with GD, ImageMagick, or whatever else. Also note that you treat the thumbnail as the same file, but with added on-the-fly manipulations.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •