tags:

views:

134

answers:

5

Hey! How do i resize/downscale the images that gets uploaded with my upload script down to 350x100 if they are over 350x100?

My script:

$allowed_filetypes = array('.png','.PNG');
$filename = $_FILES['strUpload']['name']; 
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 
if(in_array($ext,$allowed_filetypes)) 
{
    list($width, $height, $type, $attr) = getimagesize($_FILES['strUpload']['tmp_name']);
    if ($width > 350 || $height > 100) 
    {
        echo "That file dimensions are not allowed. Only 350x100 is allowed";
        exit();
    } 

    if ($_FILES['strUpload']['size'] > 2097152 )
    {
        echo "ERROR: Large File Size. Only less than 2mb accepted";
        exit();
    }

    $imagename = uniqid('ff') . ".png";
    move_uploaded_file ( $_FILES['strUpload']['tmp_name'], $imagename );

    print ( "<script type=\"text/javascript\">" );
    if(file_exists($imagename) && $_FILES['strUpload']['name'] != '')
    {
        print ( "self.opener.SetImageFile(\"" . $imagename . "\");" );
        echo "\n";
        print ( "self.opener.setInputFile(\"" . $imagename . "\");" );
    }
    echo "\n";
    print ( "window.close();" );
    echo "\n";
    print ( "</script>" );

    $open = new dbconnect();
    $open->callDB("localhost","pema2201_william","lindberg","pema2201_siggen");

    $ip = $_SERVER['REMOTE_ADDR'];
    $dattum = date('Y-m-d H:i:s', time());

    mysql_query("INSERT INTO piclist (ip,pic,datum) VALUES('$ip','$imagename','$dattum')") or die(mysql_error());

} 
else 
{
    echo "WRONG FILE TYPE ONLY PNG ALLOWED"
}
A: 

Use imagecopyresized - there's a good example of how to use it on the PHP manual page.

Rob Knight
+1  A: 

PHP has several image handling libraries. The GD library has shipped since PHP 4.3 so I suggest using that. Just read the docs to find what you need.

DisgruntledGoat
A: 

Fun with PHP and Images

Its a nice tutorial, should solve your problem.

Arpit Tambi
A: 

Have a look at this question someone else asked a few days ago.

That not only explains how it's done, but also how it's done in an efficient way. (ImageMagick should be used over the GD library)

Hope that helps.

André Hoffmann
A: 

The general gist is to create a new "canvas" of the desired dimensions for the image to go in.

Take your uploaded image and copy it onto the new canvas giving setting a source width x height (take all of the source image) and destination width x height (use all of the destination canvas), offsets are available to shift the image around a bit if you need to.

Then finally save it where you need it to go, or insert it into a database field, (this will replace your move_uploaded_file call).

Question Mark