Hello,

Thanks for checking this out.


Basically I have a form that uploads an image and stores it in a folder. I then run a thumbnail function on it and store a thumbnail version of it in a sub folder.

That all works but the bit I'm getting stuck on is trying to re size the original uploaded image. I want to do that because the original image must be 213 * 213. I can't figure it out. I need to re size the original image to 213 * 213 beofre I run the thumbnail function on it otherwise the thumbnail image will be wrong.

I have tried using imagecopyresampled, using the uploaded image as both the source image and destination image. When I try this I get the error:

imagecopyresampled(): supplied argument is not a valid Image resource

Is there a way I can just re size the Image and have it overwritten at the same time?


Any help is greatly appreciated.

Richard

Although I don't know about overwriting the image what I would do is to first submit the image to a temporary location. Then the script can read the file from that temporary location and create 2 new files each with there own sizes. Then you simply use the unlink function to delete the file at the temporary location.
Below is a sample from one of my old projects.

$imgfile='tmp/'.basename( $_FILES['uploadedfile']['name']);
//below line moves uploaded file but word 'uploadedfile' will
//need adjusting the the name of the browse field in the form.
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
$newwidth=640;
$newheight=480;								
$tmpc=imagecreatetruecolor($newwidth,$newheight);
				
//Below is the thumbnail being set out				
imagecopyresampled($tmpc,imagecreatefromjpeg($imgfile),0,0,0,0,$newwidth,
$newheight,$width,$height);

$newwidth=160;
$newheight=120;

$tmpb=imagecreatetruecolor($newwidth,$newheight);
				
imagecopyresampled($tmpb,imagecreatefromjpeg($imgfile),0,0,0,0,$newwidth,
$newheight,$width,$height);
								
//===== Now to export
$fname='test';
imagejpeg($tmpc, "../pics/".$fname.".jpg", 90);
imagejpeg($tmpb, "../thumbs/".$fname.".jpg", 90);
unlink($imgfile);

So above are the basics and replace the phrase 'uploadedfile' in the first couple of lines with the name of the browse field. Hope the code above helps explain what to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.