Jump to content

Archived

This topic is now archived and is closed to further replies.

spires

could someone have a quick check of my code?

Recommended Posts

Hi

Thanks for reading this post.

I am working on a image uploader. Which works fine.
But, if i upload a thin image (anything under $size which is 90) the pic goes all messed up.

I was woundering if iv'e done something wrong in my code.
I'm trying to make it, so if the image being loaded is to small or to big,
it will resize to 450 by 90

Thanks for your help

[code]
if(isset($_POST['banner_submit']))

{
    $size = 90; // the thumbnail height
$width = 450; // size of width


    $filedir = 'original/'; // the directory for the original image
    $thumbdir = 'banner/'; // the directory for the thumbnail image
$largedir = 'large/'; // the directory for the large image
    $prefix = ''; // the prefix to be added to the original name

    $maxfile = '1000000'; // max file size
    $mode = octdec('0666'); // octdec -- Octal to decimal.
// The mode parameter consists of three octal number components specifying access restrictions for the owner,
// the user group in which the owner is in, and to everybody else in this order. e.g (666)
     
    $userfile_name = $_FILES['image']['name'];
    $userfile_tmp = $_FILES['image']['tmp_name'];
    $userfile_size = $_FILES['image']['size'];
    $userfile_type = $_FILES['image']['type'];

     
  // if you have a the image and name then carry on
    if (isset($_FILES['image']['name']))
    {

//random digits to add to the file name
$random_digit=rand(00000000,99999999);
$ext = '.jpg';
$new_file_name=$random_digit.$ext;

// $prod_img = the image folder and the image and name
        $prod_img = $filedir.$new_file_name;

// $prod_img_thumb = the thumb folder, the prefix (if you want it) and the image and name
        $prod_img_thumb = $thumbdir.$prefix.$new_file_name;

//move_uploaded_file -- Moves an uploaded file to a new location.
//move the uploaded file to the image folder with a tempory name.
        move_uploaded_file($userfile_tmp, $prod_img);
// chmod -- Changes file mode,
// set the user interface for the image
        chmod ($prod_img, $mode);
        // getimagesize -- Get the size of an image
// find the size of the original image
        $sizes = getimagesize($prod_img);


        $aspect_ratio = $sizes[1]/$sizes[0];
// if its less than the size you want it, dont change
        if ($sizes[1] <= $size)
        {
            $new_width = $sizes[1];
            $new_height = $sizes[0];
// else, change image size
        }else{
            $new_height = $size;
$new_width = $width;
          //  $new_width = abs($new_height/$aspect_ratio);
// this code will only change the height, and make the width in ratio
        }


        $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
        $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
ImageCopyResampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $sizes[0], $sizes[1]) or die('Problem In resampling');
        ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving');
        imagedestroy($destimg);
    }




$date = date("Y-m-d > H:i:s");
$id = $_POST['id'];
$url = $_POST['url'];


$sql = mysql_query("INSERT INTO banner (id, new_file_name, date, url) VALUES ('$id', '$new_file_name', '$date', '$url')");

if ($sql) {
$uploaded = 'Files Uploaded';
}else{
$notuploaded = 'Your file did not upload';
}

}
[/code]

Share this post


Link to post
Share on other sites
some things I have noticed

dont use rand to get a unique id
what i do is
$new_file_name = date("YmdHis");

if I have mulitple uploads then I loop through the uploads, therefor I add the loop number at the end

also
You are assuming there jpgs
$file_ext = substr($_FILES['ufile']['name'], strrpos($_FILES['ufile']['name'], '.')+1);

I also notice a lot of functions are caps
PHP is case sensative, although caps work, they dont always

MyFunciton is diff to
Myfunction


Share this post


Link to post
Share on other sites
Hi, thanks for your comments.

I have changed the rand= as you suggested.

I would also like to make it so i can upload GIFS as well.
Could you let me know what parts of the code i have to change.

I placed in the code. but i'm not sure what else i'm suppose to do.
$file_ext = substr($_FILES['ufile']['name'], strrpos($_FILES['ufile']['name'], '.')+1);

[code]
if(isset($_POST['banner_submit']))

{
    $size = 74; // the thumbnail height
$width = 450; // size of width


    $filedir = 'original/'; // the directory for the original image
    $thumbdir = 'banner/'; // the directory for the thumbnail image
$largedir = 'large/'; // the directory for the large image
    $prefix = ''; // the prefix to be added to the original name

    $maxfile = '1000000'; // max file size
    $mode = octdec('0666'); // octdec -- Octal to decimal.
// The mode parameter consists of three octal number components specifying access restrictions for the owner,
// the user group in which the owner is in, and to everybody else in this order. e.g (666)
     
    $userfile_name = $_FILES['image']['name'];
    $userfile_tmp = $_FILES['image']['tmp_name'];
    $userfile_size = $_FILES['image']['size'];
    $userfile_type = $_FILES['image']['type'];
$file_ext = substr($_FILES['ufile']['name'], strrpos($_FILES['ufile']['name'], '.')+1);
     
  // if you have a the image and name then carry on
    if (isset($_FILES['image']['name']))
    {


$new_file_name = date("YmdHis").$userfile_name;

// $prod_img = the image folder and the image and name
        $prod_img = $filedir.$new_file_name;

// $prod_img_thumb = the thumb folder, the prefix (if you want it) and the image and name
        $prod_img_thumb = $thumbdir.$prefix.$new_file_name;

//move_uploaded_file -- Moves an uploaded file to a new location.
//move the uploaded file to the image folder with a tempory name.
        move_uploaded_file($userfile_tmp, $prod_img);
// chmod -- Changes file mode,
// set the user interface for the image
        chmod ($prod_img, $mode);
        // getimagesize -- Get the size of an image
// find the size of the original image
        $sizes = getimagesize($prod_img);


        $aspect_ratio = $sizes[1]/$sizes[0];
// if its less than the size you want it, dont change
        if ($sizes[1] <= $size)
        {
            $new_width = $sizes[1];
            $new_height = $sizes[0];
// else, change image size
        }else{
            $new_height = $size;
$new_width = $width;
          //  $new_width = abs($new_height/$aspect_ratio);
// this code will only change the height, and make the width in ratio
        }


        $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
        $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
ImageCopyResampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $sizes[0], $sizes[1]) or die('Problem In resampling');
        ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving');
        imagedestroy($destimg);
    }




$date = date("Y-m-d > H:i:s");
$id = $_POST['id'];
$url = $_POST['url'];


$sql = mysql_query("INSERT INTO banner (id, new_file_name, date, url) VALUES ('$id', '$new_file_name', '$date', '$url')");

if ($sql) {
$uploaded = 'Files Uploaded';
}else{
$notuploaded = 'Your file did not upload';
}

}
[/code]

Thanks for your help

Share this post


Link to post
Share on other sites
I have a script which allows jpgs, jpegs, (i know there the same, but not in php), gifs and pngs


[code]
//First I create an array of allowed file extensions
$allowed_ext = array("jpg","jpeg","gif", "png");
$num_allowed_exts = count($allowed_ext);
//also have it count the number of types in the array, so if i add one, or take one off, its less coding

//then I get the file ext, using <input type='file' name='ufile'

$file_ext = substr($_FILES['ufile']['name'], strrpos($_FILES['ufile']['name'], '.') +1);

//Now make the file_ext lower case, as JPG and jpg are different in php
$file_ext = strtolower($file_ext);

//checking if the file extension exsists
for($i = 0; $i < $num_allowed_exts; $i++){
if($file_ext == $allowed_ext[$i]){
$file_ext_ok = true;
break;
}else{
$file_ext_ok = false;
}
}

if($file_ext_ok == true){

//the new name, I am allowing members to upload images, so its only one image per member, unique ids
$new_file_name = $username.".".$file_ext;
//The dir for the file, keeping this seperate is for later
$dir = $_SERVER['DOCUMENT_ROOT']."/user_imgs/";

//Now we will upload the file  
if(move_uploaded_file($_FILES['ufile']['tmp_name'], $dir.$new_file_name)){

$image = $_FILES["ufile"]["name"];
$image_upload = true;
$img_name = $new_file_name;

list($width, $height) = getimagesize($dir.$new_file_name);

if($width > 300){

$ratio = $height / $width;
$new_width = 300;

$new_height = (300 * $ratio);



$image_p = imagecreatetruecolor($new_width, $new_height);

switch($file_ext){
case "jpg":
$image = imagecreatefromjpeg($dir.$new_file_name);
break;
case "jpeg":
$image = imagecreatefromjpeg($dir.$new_file_name);
break;
case "gif":
$image = imagecreatefromgif($dir.$new_file_name);
break;
case "png":
$image = imagecreatefrompng($dir.$new_file_name);
break;
//If you add another file ext, you MUST add something here
}
//People saying you should ALWAYS have a default on this, but what can the default be
imagecopyresampled($image_p, $image, 0,0,0,0,$new_width, $new_height, $width, $height);

$save_to = $dir.$new_file_name;
switch($file_ext){
case "jpg":
imagejpeg($image_p, $save_to, 75);
break;
case "jpeg":
imagejpeg($image_p, $save_to, 75);
break;
case "gif":
imagegif($image_p, $save_to, 75);
break;
case "png":
imagepng($image_p, $save_to, 75);
break;
//If you add another file extenstion, you MUST add something here
}
}
//This should be from the if(move_uploaded_image)
}else{
echo "There was an Error uploading image";
$image_upload = false;
}
//This should be from the checking file extension, I then loop through to tell the user what extension they can use
}else{
echo "I am afraid your profile image is an invalid image type, please use only the following extensions.<br />\n";
for($i = 0; $i < $num_allowed_exts; $i++){
echo "\"".$allowed_ext[$i]."\"<br />\n";
}
//This is for something else
$image_upload = false;
}

?>
[/code]

This is just a quick example
It is no way secure, you have to think, if someone have a Bitmap, and changes the file extension to jpg, it will pass the checking file extension, but it would fail when we do anything with it

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.