Help with resizing mantaining aspect ratio with gd
Posted: Sat Nov 13, 2004 1:33 pm
Hi guys, I am new there... I hope you can help me with one question...
I upload an image from a form and change its name with a random number, what i would like to do now, is:
- Resize maintaining aspect ratio, saving resized image with that random name in /img
and original one in /hires_img
Can you help me doin it? I attached my original code, hope it can helps you to make you loose less time.
I tried some samples from image copy resize class but they didn't work...
In my server I can only use Gd library.
Thank you my friends.
France
Weirdan | France, please use
I upload an image from a form and change its name with a random number, what i would like to do now, is:
- Resize maintaining aspect ratio, saving resized image with that random name in /img
and original one in /hires_img
Can you help me doin it? I attached my original code, hope it can helps you to make you loose less time.
I tried some samples from image copy resize class but they didn't work...
In my server I can only use Gd library.
Thank you my friends.
France
Code: Select all
//**********************************************************************//
// $_FILES['filetoupload'] is the value of //
// file field from the form. <input type="file" name="filetoupload"> //
//**********************************************************************//
srand ((double) microtime( )*1000000);
$random_number = rand(100000,1000000 );
// this is the upload dir where files will go.
$upload_dir = "img/";
$size_bytes = 1048576; //bytes will be uploaded
$limit_file_type = "yes";
// specify file types.
$allowed_file_type = array('image/jpg'
);
$allowed_file_type = array(
'image/pjpeg',
'image/jpeg',
'image/jpg');
//check if the directory exists or not.
if (!is_dir("$upload_dir")) {
die ("The directory <b>($upload_dir)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("The directory <b>($upload_dir)</b> is NOT writable, Please Chmod (777)");
}
//Check first if a file has been selected
//is_uploaded_file('filename') returns true if
//a file was uploaded via HTTP POST. Returns false otherwise.
if (is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{//begin of is_uploaded_file
//Get the Size of the File
$size = $_FILES['filetoupload']['size'];
//Make sure that $size is less than 1MB (1000000 bytes)
if ($size > $size_bytes)
{
echo"<script language=Javascript>
alert ("Il file รจ troppo grande, deve essere massimo <b>$size_bytes</b> bytes")</script>";
exit();
}
//checks file type
if (($limit_file_type == "yes") && (!in_array($_FILES['filetoupload']['type'],$allowed_file_type)))
{
echo"<script language=Javascript>
alert ("Il file deve essere un''immagine JPG non progressiva, della dimensione massima di 10mb")</script>";
}
// $filename will hold the value of the file name submetted from the form.
$filename = $_FILES['filetoupload']['name'];
$filenamedefinitivo = "img_$random_number.jpg";
// Check if file is Already EXISTS.
if(file_exists($upload_dir.$filenamedefinitivo)){
$filenamedefinitivo = "img_$random_number.jpg";
}
//Move the File to the Directory of your choice
//move_uploaded_file('filename','destination') Moves afile to a new location.
$fronte ="fronte";
$retro ="retro";
if ((move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filenamedefinitivo)) &&
$immaginefronteretro == $fronte)
{
//make some actions
}
else{
//make some other actions
}Code: Select all
tags when posting php code[/color]