**PLEASE NEED HELP CREATING THUMBNAIL**

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
becky155
Forum Newbie
Posts: 6
Joined: Mon Mar 24, 2008 10:21 pm

**PLEASE NEED HELP CREATING THUMBNAIL**

Post by becky155 »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi, I have been trying over and over again to create thumbnails and save them in a separate folder when the image is uploaded but I have not been successful. I am so, so frustrated! PLEASE, PLEASE if anyone can help me, I would really appreciate it. I have managed to write a program that successfully uploads the image to the server and saves a copy in my database but I am not sure what to do next.

My upload code is:

Code: Select all

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "Form"))
{
//This function separates the extension from the rest of the file name and returns it
function findexts2 ($filename2)
{
$filename2 = strtolower($filename2) ;
$exts2 = split("[/\\.]", $filename2) ;
 
$n2 = count($exts2)-1;
$exts2 = $exts2[$n2];
return $exts2;
}
function findexts2B ($filename2B)
{
$filename2B = strtolower($filename2B) ;
$exts2B = split("[/\\.]", $filename2B) ;
$n2B = count($exts2B)-1;
$exts2B = $exts2B[$n2B];
return $exts2B;
}
 
//This applies the function to our file
$ext2 = findexts2 ($_FILES['photo2']['name']);
$ext2B = findexts2B ($_FILES['photo2B']['name']);
 
//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
$ran3 = rand () ;
$ran3B = rand () ;
 
//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran4 = $ran3.".";
$ran4B = $ran3B.".";
 
//Upload to the server
$target = "/IMAGES/targetfile/";
$target2 = $target . $ran4.$ext2;
$targetB2 = "IMAGES/targetfile/";
$target2B = $targetB2 . $ran4B.$ext2B;
 
//This gets all the other information from the form
$pic2 = $ran4.$ext2;
$pic2B = $ran4B.$ext2B;
 
//This checks if the file is a php file
if ($pic2 =="text/php")
{
echo "No PHP files";
$ok=0;
}
if ($pic2B =="text/php")
{
echo "No PHP files";
$ok=0;
}
 
//Check file types
if (($_FILES["photo2"]["type"] == "image/gif")
|| ($_FILES["photo2"]["type"] == "image/jpeg")
|| ($_FILES["photo2"]["type"] == "image/bmp")
&& ($_FILES["photo2"]["size"] < 30000))
 
if (($_FILES["photo2B"]["type"] == "image/gif")
|| ($_FILES["photo2B"]["type"] == "image/jpeg")
|| ($_FILES["photo2B"]["type"] == "image/bmp")
&& ($_FILES["photo2B"]["size"] < 30000))
 
{
 
//Writes the photo to the server
if (move_uploaded_file($_FILES['photo2']['tmp_name'], $target2))
if (move_uploaded_file($_FILES['photo2B']['tmp_name'], $target2B))
{
$insertSQL = sprintf("INSERT INTO Database(Image, Image2) VALUES (%s, %s)",
GetSQLValueString($pic2, "text"),
GetSQLValueString($pic2B, "text"),
 
mysql_select_db($database_database_name, $data_name);
$Result1 = mysql_query($insertSQL, $database_name) or die(mysql_error());
 
$insertGoTo = "test.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
}
}
PLEASE, PLEASE, I NEED HELP :? . THANKS IN ADVANCE!


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by Jonah Bron »

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by pickle »

So you're just having trouble generating the thumbnail? Look here: http://www.phpgd.com/scripts.php?script=1 for a helpful class.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
becky155
Forum Newbie
Posts: 6
Joined: Mon Mar 24, 2008 10:21 pm

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by becky155 »

Hi,

Thank you all for the info. I have looked at the PHPit code before but I guess what's confusing me is where i put the code? Do i create a separate php file and then on the site refer the img src code to my php file per image? I am soooo confused and I know it's fairly easy, i just can't wrap my head around it. :banghead: I am thinking that I use my regular upload script and then in the areas on the site where i want to show the thumbnail, i would do something like: <img src=resample.php> ...am i on the right track? If I am on the right track will this method affect upload time if I have a lot of traffic at once?

Thank you all again. I don't know what's wrong with me!!!!! :(
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by pickle »

How much PHP coding have you done before? It looks like the code you posted before was copied from somewhere else (which is perfectly fine). Have you done much/any original PHP coding? Not to say it's bad if you haven't, but it will affect how it's explained.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
becky155
Forum Newbie
Posts: 6
Joined: Mon Mar 24, 2008 10:21 pm

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by becky155 »

I have done quite of coding for the site i am working on. I did copy the upload file but I had to revamp it and although I am new to PHP, I fairly understand coding. I just can't get this thumbnail thing . :cry:

Thanks for any help that you may be able to offer. :wink:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by pickle »

Ok.

So, correct me if I'm wrong, but you're at the spot where everything is hunky dorey except you don't have a thumbnail. It's much better to generate the thumbnail at the time of upload, than at the time of request. This will only generate the thumbnail once instead of every time someone wants the thumbnail.

One caveat - GD doesn't work on BMP files. If you necessarily have to use BMP files, look into ImageMagik. If this is for a public, web-based service, I'd recommend eliminating support for BMP files & support only JPG/GIF/PNG files.

Create a new file & paste in the Thumbnailer class code. The code on phpgd.com has some extra code before the class declaration which is used just for demonstration purposes. Remove all the code before the class declaration. include() that class file at the top of your main file.

Then, after the file has been successfully uploaded, create a new Thumbnailer object, open() the file that was just uploaded, and call the appropriate imageXXX() function, providing it with the filename you want for the thumbnail.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
becky155
Forum Newbie
Posts: 6
Joined: Mon Mar 24, 2008 10:21 pm

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by becky155 »

Ok...please forgive my ignorance. So in layman terms, I need to copy the code from the tutorial and save it and upload it. But before i upload it, get rid of the before the class declaration. :? this is where I start getting confused: After that create a new thumbnailer object, open the file that i just uploaded and call the image function... :? Okay...maybe i just not that familiar with the terminology...I am not sure what you meant by create a new thumbnailer object. :banghead:

Also, I would really like to create the thumbnail at the time of upload because based on what i have read and what you suggested, it would be better. So how would the code fall into place with my current upload code to create the thumbnail and upload it in a thumbs folder on my server?

Once again, please forgive my ignorance.

Thanks a lot!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by pickle »

Google "objects in PHP" - there are a few tutorials out there to help. It's actually pretty easy once you're shown. Objects are basically just containers for functions & variables - they REALLY help clean up code. Those tutorials should show you how to create a new object & how to call functions of that object.

As for where in your main file you go about making the thumbnail, it'd work best to put it after your last call to mysql_query() - where $Result1 is created. It's at that point where you know the image has been uploaded successfully & that it's been put in the database successfully.

I'd suggest you spend a couple hours playing around with really simple aspects of PHP programming - making forms, handling user input, etc - to get yourself acquainted with how it all works.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
becky155
Forum Newbie
Posts: 6
Joined: Mon Mar 24, 2008 10:21 pm

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by becky155 »

Thank you so much! I will play around with it and see what happens. :)
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by Inkyskin »

I have written a couple of small functions that will create thumbnails using GD. You can see them here: http://www.ben-griffiths.com/gd-thumbnail-class/
becky155
Forum Newbie
Posts: 6
Joined: Mon Mar 24, 2008 10:21 pm

Re: **PLEASE NEED HELP CREATING THUMBNAIL**

Post by becky155 »

Hi Ben,

Thanks for the code. I will check it out.
Post Reply