Forum Moderators: coopster

Message Too Old, No Replies

Uploading image then automatically create a thumbnail

Please help me.

         

dkin

7:37 am on Dec 16, 2004 (gmt 0)

10+ Year Member



I have been looking everywhere and trying everything to be able to get this done but nothing has worked thus far.

Does anyone know how I can upload an image and automatically creat a thumbnail.

If someone tells me this you will be my hero.

Thank you.

mincklerstraat

8:08 am on Dec 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



look here [be.php.net] for getting the image uploaded - then you'll want to use:
imagecreatefromjpeg() or imagecreatefrompng or imagecreatefromgif() depending on the filetype
imagecreatetruecolor() to create another image
imagecopyresized() to resize your image onto the new image
imagepng() or imagejpeg() (there is no imagegif() at the moment) to save your image as a file.

dkin

1:12 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



ok, I know all of those but, I cant understand how to use them.

My many attempts with the script that I am using now have failed, my script is posted in my other post, if someone knows how to use these functions please take a look at it.

Thank you for your reply

cheers

mincklerstraat

1:41 pm on Dec 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've taken a look at your post at [webmasterworld.com...] and it looks to me like you are having primarily file errors. Notice, where you have the error:
Warning: imagejpeg(): Unable to open '/home/eqoagui/public_html/img/thumb/thumbs/1.jpg' for writing

First thing to say, of course, is: does the directory /home/eqoagui/public_html/img/thumb/thumbs/ exist, and have you set its permissions properly (chmodding it to 777 should do it)? Maybe you chmodded a different directory previously, but this also needs to be chmodded 777 as well. Also, if 1.jpg in this directory already exists, it also needs to be chmodded 666 for this to work.

Another thing that comes to mind is the fact that this is a different path from some of the paths in errors reported in your previous postings. Maybe the paths created aren't corresponding to what's expected? Take a good look at the errors, if necessary echo out the paths at which you are trying to move or save things before you actually move or save them - this is a common source of errors in php.

dkin

1:53 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



ok, I have it uploading and creating the thumbnail perfectly, I have tested it many times and everything seems to be in order. But it is not saving the original as well. I need the original as well as the thumbnail. Here is a bit of the working code if someone can tell me where to insert the code to save the uploaded pic.

$new_thumb = "$new_pic";
$sourcefile = "$uploaddir$new_pic";
$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 = "$uploaddir$new_pic";
$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);
?>

Romeo

3:37 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Hi dkin,

your uploaded pic seems to be here:
$sourcefile = "$uploaddir$new_pic";
as in the beginning of your code.

Later, in the last part, you then seem to overwrite this file by assigning the same file name:
$targetfile = "$uploaddir$new_pic";

Try using another file name for writing, e.g.
$targetfile = "$uploaddir$new_pic" . "_thumb";

Regards,
R.

dkin

7:01 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



When I change the sourcefile variable the thumbnail is not created, it seems to upload the full sized image then scale it down to a thumbnail and save it again, any way I can duplicate it and save it prior to this happening?

mincklerstraat

10:22 am on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the spot where you define $sourcefile, try replacing this with the following:

$originalfile = "$uploaddir$new_pic";
$sourcefile = $uploaddir.'thumb'.$new_pic;

This will put the thumbs into a directory with 'thumb' added to the name of $uploaddir.