php image thumbnail upload script

hello,
I need a easy tutorial....
I need a script that will allow me to upload an image, then create a thumbnail copy of it, so i then have 2 images, then both names will be inserted into my db.  I have found many scripts like this on the web, but they never seem to be complete
LVL 1
jblayneyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmc98dl1Commented:
http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20806629.html

you need to ensure you have the latest version of PHP and GD2 installed for this to work.

Duncan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jblayneyAuthor Commented:
tx duncan,
I do have the latest php and gd installed, that link you took me to, is not the complete code though, their re include files missing as well as the upload form, so u have anymore information..


justin
0
mmc98dl1Commented:
The include files are simply a login script, and an HTML footer and header file.  They arent of importance to this question.

The form looks like this:

<table class='border' width='75%' align='center'>      
<tr><th>Album Image upload</th></tr>
<tr>
            <td colspan='2' valign='top'>
                  
                  <form action="upload.php" method="post" enctype="multipart/form-data">
                  <table width='100%'>
                  <tr>
                        <th>Name:</th>
                        <th>Description:</th>
                        <th>Gallery:</th>
                        <th>File:</th>
                  </tr>
                  <tr>
                        <td><input type="text" name="name"></td>
                        <td><input type="text" name="description"></td>
                        <td><?php
                        $sql = "SELECT * FROM Album order by AlbumName";
                        dbDo(&$result,$sql);
                  
                        print "<select name=galleryId>\n";
                        while (odbc_fetch_row($result)){
                              print "<option value=".odbc_result($result,1).">".odbc_result($result,2)."\n";
                        }
                        print "</select>\n";

                  ?>      </td>
                        <td><input type="file" name="file" accept="image/jpeg,image/x-png"></td>
                  </tr>
                  <tr>
                        <td><input type="submit" name="action" value="upload"></td>
                  </tr>
                  </table>
                  </form>

            </td>
      </tr>
</table>

This will allow you to have a database of images that fit into albums.

Your Album table looks like this:
AlbumId int
AlbumName varchar
AlbumDescription varchar
DateCreated Datetime default Now()
AlbumFolder

The Image table:
ImageId int
ImageName varchar
ImageDescription varchar
UploadDate datetime default Now()
ImageFile
ThumbnailFile
AlbumId

I think from that you should be able to insert the images, then all you need is to do some SELECT's on it to get the info out.  The AlbumFolder field is the web path NOT the absolute path.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

jblayneyAuthor Commented:
ok tx again, ( i increased the points, maybe u can help me work through this)
I tried to make your script work with my db, the error i get is "No input file specified. " 

This is my code, I renamed the file imageUpload2.php, I changed the upload directory to "uploads/", Its not my server so i have no idea where my site and folder are, I have specified that uploads folder in my php.ini for uploading, and i have other simple upload scripts that work with it.

Also, what will my copied thumb be named ?

<?

     $uploadDir = "uploads/";
     $fileName = $_FILES['file']['tmp_name'];
     $userfileName = str_replace(" ","_",$_FILES['file']['name']);
    move_uploaded_file($fileName, $uploadDir. $userfileName);
     $source = imagecreatefromjpeg($uploadDir. $userfileName);
     $dest = imagecreate(100, 100);
    $system=explode(".",$userfileName);
    if (preg_match("/jpg|jpeg/",$system[1])){
        $src_img=imagecreatefromjpeg($uploadDir. $userfileName);
          echo("jpg");
    }
    if (preg_match("/png/",$system[1])){
        $src_img=imagecreatefrompng($uploadDir. $userfileName);
    }
     $original_x=imageSX($src_img);
    $original_y=imageSY($src_img);
    if ($original_x > $original_y) {
        $thumb_w=100;
        $thumb_h=$original_y*(100/$original_x);
    }
    if ($original_x < $original_y) {
        $thumb_w=$original_x*(100/$original_y);
        $thumb_h=100;
    }
    if ($original_x == $original_y) {
        $thumb_w=100;
        $thumb_h=100;
    }
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
     if (preg_match("/png/",$system[1])){
        imagepng($dst_img,$uploadDir."th_".$userfileName);
    } else {
        imagejpeg($dst_img,$uploadDir."th_".$userfileName);
    }
    imagedestroy($dst_img);
    imagedestroy($src_img);

$db_name = "mine";
$table_name = "image_uploads";
$connection = @mysql_connect("localhost", "user", "pass") or die("Problems connecting - Try again");

$db = @mysql_select_db($db_name, $connection) or die("Problems connecting - please try later");
$sql = "INSERT INTO $table_name
(imageID, imageName, imageClientID, imageBlurb, imageDate)
VALUES('', '$filename', '$id', '$uploadBlurb', 'date')";
 
$result =@mysql_query($sql, $connection) or die("Sorry, coudn't execute query - please try later");
echo ("<span>$realName was copied successfully<br><br></span>");
}
}
    
?>
0
mmc98dl1Commented:
Copied thumb will be called th_originalfilename.jpg

Bear in mind this script will only work for jpg or png.


     $uploadDir = "uploads/";


This definately needs to be the absolute path on your server  like the example I had in mine.

You can find this out by running phpinfo(); on your server, then look for the PATH_TRANSLATED entry - this will tell you where your scripts reside, then change it to reflect uploads/.

0
jblayneyAuthor Commented:
Hello Duncan,
The path-translated path is not as accuarate as yours, it doesnt go right down to the c-drive, but it is pretty long, this what the beginning of my code looks like now (below).. but now Im getting a weird error

Parse error: parse error in /u/j/jeremy/www.website.com/admin/uploadImage2.php on line 3

line 3 being the path
<?

 $uploadDir = "/u/j/jeremy/www.website.com/admin/uploads/";
     $fileName = $_FILES['file']['tmp_name'];
     $userfileName = str_replace(" ","_",$_FILES['file']['name']);
    move_uploaded_file($fileName, $uploadDir. $userfileName);
     $source = imagecreatefromjpeg($uploadDir. $userfileName);
     $dest = imagecreate(100, 100);
    $system=explode(".",$userfileName);
    if (preg_match("/jpg|jpeg/",$system[1])){
        $src_img=imagecreatefromjpeg($uploadDir. $userfileName);
0
jblayneyAuthor Commented:
Ok, everything works great... except one thing. i tried to upload a file that was exactly 100 kb and got this error

Fatal error: Allowed memory size of 8388608 bytes exhausted at (null):0 (tried to allocate 6264 bytes) in /u/j/jeremy/www.website.com/admin/upload.php on line 10

files that are bigger than 100 kb or smaller work, just this one gave me an error,, and thoughts on this...
0
jblayneyAuthor Commented:
after further testing... i have no idea why the 100 kb image didnt work, i opened it in photshop and resaved it as a 300 kb jpg and it still didnt work..

but i have not been able to get any images over 400 kb to create thumbnails, the original will upload, but no thumbnail..

The application i am buiding is because my client wants to upload 1-2 meg files and have them displayed on a page, which is why i need the thumbnails
0
jblayneyAuthor Commented:
tx, it kind of works, i seem to get errors if i try to upload photshop jpg's or jpgs over 1 meg,

Im going to post a new topic called "thumbnail upload server errors" hopefully you can help me.
0
mmc98dl1Commented:
I take it the 100 kb was a jpg?

and does it work ok for a sample of the clients images?

Note - my mistake - this will only work for JPG's I actually removed the PNG stuff from it.

That error (in my experience) comes up when the server has so many threads open it cant do any more.  

Make sure you still have the

    imagedestroy($dst_img);
    imagedestroy($src_img);

outside any if tags, to make sure whatever the outcome it always does this to clear the memory.

If it continues to happen - you might consider creating an image placeholder in memory of a similar size to the one being uploaded, but this is entirely impractical for 1Mb+ images!  it would be ok for a 100k image.

0
jblayneyAuthor Commented:
i posted a new topic for this named
thumbnail upload server errors

this script is entirely erratic, it accepts images and rejects them for no apparant reason, it accepts some 750 kb images and rejects others, It seems to reject any jpg image created by photoshop or fireworks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.