Forum Moderators: coopster

Message Too Old, No Replies

resize image to thumbnail

need help troubleshooting

         

MWpro

5:36 pm on Jul 24, 2004 (gmt 0)

10+ Year Member



I'm writing a script that lets the user upload a file and then have it resized as a thumbnail. So far I got the upload file part working, but cannot get the thumbnail part working. The script I am using for the thumbnails is from the php bag of scripts [webmasterworld.com]. Help figuring out what is wrong with this will be appreciated, thank you.


[2]
if (is_uploaded_file($_FILES['userfile']['tmp_name'])){
//Increment filename
$q = mysql_query("SELECT * FROM images");
$row = mysql_fetch_array($q);
$id = $row["id"];
$id++;
$filename = "image".$id.".jpg";

//Store image
copy($_FILES['userfile']['tmp_name'], "/usr/www/htdocs/path/img/gallery/$filename");

//Save $id into database
$q = "UPDATE images SET id='$id'";
mysql_query($q);

//Resize and store thumbnail
$new_thumb = "thumb".$id.".jpg";
$sourcefile = "$_FILES['userfile']['tmp_name']";
$picsize = getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize[1];

if ($source_x > $source_y){
$dest_x = 200;
$dest_y = 150;
} else {
$dest_x = 150;
$dest_y = 200;
}

$targetfile = "$new_thumb";
$jpegqual = 75;
$source_id = imagecreatefromjpeg("$sourcefile");
$target_id = imagecreatetruecolor($dest_x, $dest_y);
$target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);
imagejpeg($target_id,"$targetfile",$jpegqual);

echo "File uploaded successfully.\n";

} else {
echo "Sorry, your file could not be uploaded.";
}
}
[/2]

HughMungus

6:02 pm on Jul 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. You might want to indicate how it's failing (e.g., what error message).

2. Are you trying to resize GIF's? I've learned that many version of PHP don't allow GIF support because of licensing issues.

3. I just installed something similiar on my site. The script and tutorial are here (for comparison or if you want to try a different script):

[codewalkers.com...]

MWpro

6:11 pm on Jul 24, 2004 (gmt 0)

10+ Year Member



Basically when the user selects a file and presses submit, that file is uploaded onto the server, however the thumbnail is not. Because of this, I am assuming there is a problem with the thumbnail creation part of the script, but I cannot figure out what it is.

And I'm positive that I'm using all .jpg files to test it.

Birdman

8:08 pm on Jul 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

The Sql you are using looks incorrect and I think it's making the filename incorrect as well.

Have you created the /thumbs/ directory?

Permissions set correctly?

Place this line of code at the top of the script and then run the script.

error_reporting(E_ALL);

That will make PHP output all errors.

Birdman

henry0

12:35 am on Jul 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



MWpro
Birdman's script works fine (thanks Birdman)
I have been able to adapt/insert it in my CMS for multiple users and multiple img without too many problems

is your MySQL connection script delivering a few errors reports

my experience with the script is that you might also
try related and full path

are you testing locally or on a production server

let us know about "error_reporting"

regards

Henry

MWpro

2:42 am on Jul 25, 2004 (gmt 0)

10+ Year Member



try related and full path

Thanks, this was the problem. Solved by putting the whole path in both the source image and the new thumbnail image filename.

The Sql you are using looks incorrect and I think it's making the filename incorrect as well.

Even though the script is working now and the sql is working how it was intended, what is a more efficient way of doing the sql? I have a table with just one row and one column, just to hold the number of images that there that have been uploaded. What is a better way to select that number?

Birdman

12:46 pm on Jul 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is a modified version that simply count the files in the directory. No need for a db just to count the files. Hopefully, there are no errors ;) Enjoy!

$upload_dir = "/usr/www/htdocs/path/img/gallery/";

$file_count = count(scandir($upload_dir)) - 1;

$filename = $upload_dir."image".$file_count.".jpg";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $filename)){

//Resize and store thumbnail
$new_thumb = $upload_dir."thumb".$file_count.".jpg";

$picsize = getimagesize($filename);
$source_x = $picsize[0];
$source_y = $picsize[1];

if ($source_x > $source_y){
$dest_x = 200;
$dest_y = 150;
} else {
$dest_x = 150;
$dest_y = 200;
}

$jpegqual = 75;
$source_id = imagecreatefromjpeg($filename);
$target_id = imagecreatetruecolor($dest_x, $dest_y);
$target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);
imagejpeg($target_id,$new_thumb,$jpegqual);

echo "File uploaded successfully.\n";

} else {
echo "Sorry, your file could not be uploaded.";
}

MWpro

7:12 pm on Jul 25, 2004 (gmt 0)

10+ Year Member



Awesome; thanks so much : )