-1

Possible Duplicate:
Calculating image size ratio for resizing

So I have a simple upload script that resizes the uploaded picture, I would like to force the width size, and let the height get automatically proportioned.

Here is my upload script:

if (isset($_POST['upload'])) {
        $dirupload = "../images/";
        $dirupload = $dirupload . basename( $_FILES['image-upload']['name']);
        $useless = $_POST['useless'];
        $imageupload = ($_FILES['image-upload']['name']);
        $uploadedfileupload = $_FILES['image-upload']['tmp_name'];
        $uploadedfiletypeupload = $_FILES['image-upload']['type']; 

        $query_add="SELECT * FROM produits ORDER BY position";
        $result_add=mysql_query($query_add);
        $num_add=mysql_num_rows($result_add);
        $add_add=$num_add+1;

        if (!($uploadedfiletypeupload =="image/pjpeg" OR $uploadedfiletypeupload =="image/jpeg" OR $uploadedfiletypeupload =="image/jpg")){
            echo "L'image doit être en .jpg ou .jpeg";
        }else if (move_uploaded_file($_FILES['image-upload']['tmp_name'], $dirupload)){     
            $sql="INSERT INTO produits (position) VALUES ('$add_add')";
            if (!mysql_query($sql,$con)){
                die('Error: ' . mysql_error());
            }       


            $newnameupload = "../images/produit" . mysql_insert_id() . ".jpg";
            rename ("../images/$imageupload","$newnameupload");    
            $orig_imageupload = imagecreatefromjpeg("../image/$newnameupload");
            $sm_imageupload = imagecreatetruecolor(274,219);
            imagecopyresampled($sm_imageupload,$orig_imageupload,0,0,0,0,274,219,imagesx($orig_imageupload),imagesy($orig_imageupload));
            imagejpeg($sm_imageupload, $newnameupload, 100);

        }else{
            echo "Un probleme est survenue";
        }
    }

Any ideas on trying to get this to work ? (yes I do have the height set for now in the script)

Thanks in advance !

Community
  • 1
  • 1
  • 1
    What's wrong with your script? – Tchoupi Aug 29 '12 at 12:57
  • Nothing is wrong with the script, I would just like too see how only set the width and let the height get automatically proportioned. – user1619359 Aug 29 '12 at 13:00
  • There are many image control libraries for PHP, you can look for one and use it. It will resize with proportions or crop to fill blank spaces and so on. – Valdas Aug 29 '12 at 13:00

2 Answers2

1

I think, the best way is to use some image processing library. Because it is kinda crop-and-resize option. Sometimes image can't be proportionally reduced to the required size without cropping.

Take a look at:

Both of them have built-in functionality that you need and are rather friendly and flexible.

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
1

Calculate the width to height factor like

$factor = $height /$width;

then

$newHeight = $newWidth * $factor;
Dirk McQuickly
  • 2,099
  • 1
  • 17
  • 19