TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Changing imagesize while uploading. (http://www.talkphp.com/advanced-php-programming/2569-changing-imagesize-while-uploading.html)

lesP 04-02-2008 12:29 PM

Changing imagesize while uploading.
 
Hi Guys!

I have done a uploading script, but I want to do, so when I upload an image the image should be stored in 3 different sizes. That means 3 different pictures. What is the easiest way to do that? Here is my upload-script:

PHP Code:

$filename 'billeder/' basename($_FILES['file']['name']);


        if (
move_uploaded_file($_FILES['file']['tmp_name'], $filename)) {
            echo 
"File is valid, and was successfully uploaded.";
        } else {
            echo 
'DEBUG: Fra: ' $_FILES['file']['tmp_name'] . ' til ' $filename '<br>';
            echo 
"Possible file upload attack!";
        }    

        
$sql "INSERT INTO smykker (smykkenavn, beskrivelse, pris, varenr, billede, kategori, underkategori, leveringstid)
            VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')"
;
        
$sql sprintf($sql,
            
mysql_real_escape_string($_POST['smykkenavn']),
            
mysql_real_escape_string($_POST['beskrivelse']),
            
mysql_real_escape_string($_POST['pris']),
            
mysql_real_escape_string($_POST['varenr']),
            
mysql_real_escape_string($filename),
            
mysql_real_escape_string($_POST['kategori']),
            
mysql_real_escape_string($_POST['underkategori']),
            
mysql_real_escape_string($_POST['leveringstid'])
            );

        
$res mysql_query($sql) or die(mysql_error());
        echo 
"Done<br>Klik <a href='upload.php'>her</a> for at gå tilbage."


Jim 04-09-2008 06:57 AM

with imagecopyresampled() you can do that quite easily. And in your code, please check the file for mimetype and extention! :)

Orc 04-10-2008 01:50 PM

Quote:

Originally Posted by Jim (Post 13331)
with imagecopyresampled() you can do that quite easily. And in your code, please check the file for mimetype and extention! :)

...or imagecopyresized incase he doesn't want it to change the pixels.

sketchMedia 04-10-2008 07:00 PM

easy really, here the script i use:
PHP Code:

function resizeImage($originalImage,$toWidth,$toHeight,$dest_img)
{
    list(
$width$height) = getimagesize($originalImage);
    
$xscale=$width/$toWidth;
    
$yscale=$height/$toHeight;

    if (
$yscale>$xscale)
   {
        
$new_width round($width * (1/$yscale));
        
$new_height round($height * (1/$yscale));
    }
    else 
    {
        
$new_width round($width * (1/$xscale));
        
$new_height round($height * (1/$xscale));
    }
    
$imageResized imagecreatetruecolor($new_width$new_height);
    
$imageTmp imagecreatefromjpeg ($originalImage);
    
imagecopyresampled($imageResized$imageTmp0000$new_width$new_height$width$height);

    
imagejpeg($imageResized$dest_img);
}

resizeImage('test1.jpg'400400'test_res2222222.jpg'); 

I have used this in the past, should be easy for you to modify, it takes into account image orientation (i.e. landscape/portrait) and scales accoringly. It olny accepts jpegs atm but like i say you its easy to customize it with the correct image format function. the php manual is useful here:

PHP: Image - Manual


All times are GMT. The time now is 10:06 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0