We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Resize images while uploading

Hi i have this script to upload and replace the exciting image which is good. The one thing i can not figure out is i would like to resize any images that are uploaded, to like 60x60.
any ideas ?

<?php


    if (isset($_POST['submit'])) {

        $newext = '.png';
        $filename = $_FILES["file"]["name"];
        $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
        $file_ext = substr($filename, strripos($filename, '.')); // get file name
        $filesize = $_FILES["file"]["size"];
        $width = 20;
        $height = 20;
        $allowed_file_types = array('.jpg','.png','.gif');

        if (in_array($file_ext,$allowed_file_types)  &&  ($filesize < 2000)) {

            // rename file
            $newfilename = $_SESSION['ls_user'] . $newext;

            {       
                move_uploaded_file($_FILES["file"]["tmp_name"], "avatar/" . $newfilename);
                echo "File uploaded successfully.";         
            }

        } elseif (empty($file_basename)) {  
            // file selection error
            echo "Please select a file to upload.";     
        } elseif ($filesize > 2000) {   
            // file size error
            echo "The file you are trying to upload is too large.";     
        } else {
                    // file type error
            echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
            unlink($_FILES["file"]["tmp_name"]);        
        }

    }

    ?>

<p><form action="" method="post" enctype="multipart/form-data"></p>
    <input type="file" name="file" id="file">
    <input type="submit" name="submit" id="Submit" value="Submit"></form>

thank you

3
Contributors
6
Replies
3 Days
Discussion Span
6 Months Ago
Last Updated
19
Views
Question
Answered
TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@TKO

Hi i have this script to upload and replace the exciting image which is good. The one thing i can not figure out is i would like to resize any images that are uploaded, to like 60x60.any ideas ?

There's a lot of resized images with uploading scripts out there I'm not sure why you would choose this one and that you need someone to modify it?

Here is a link to an example that is closest to your:

http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html

LastMitch
Industrious Poster
4,212 posts since Mar 2012
Reputation Points: 134
Solved Threads: 336
Skill Endorsements: 45

Thanks for the idea
My sript uploads all images these other ones dont upload gif png ext.
Any ideas to modify this one

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@TKO

My sript uploads all images these other ones dont upload gif png ext.

Did you test out teh script and also put extension that don't upload

This your code of extension:

 $allowed_file_types = array('.jpg','.png','.gif');

This the code from the link:

if ($image) {
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
echo ' Unknown Image extension ';
$errors=1;
}
else{
$size=filesize($_FILES['file']['tmp_name']);

if ($size > MAX_SIZE*1024){
echo "You have exceeded the size limit";
$errors=1;
}

if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else if($extension=="gif"){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else {
$src = imagecreatefromgif($uploadedfile);
}

Any ideas to modify this one

No, I won't modify the current code you post.

It's up to you whether you pick this one because it's very simple.

I mean what so hard just changing extensions?

I already add gif?

All You just need to add a few more lines and you're done!

LastMitch
Industrious Poster
4,212 posts since Mar 2012
Reputation Points: 134
Solved Threads: 336
Skill Endorsements: 45

thanks for your input LastMitch the problem is most sciprts upload gifs but lose the animation.

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

thanks for your input LastMitch the problem is most sciprts upload gifs but lose the animation.

Uploading images don't lose quality. Resizing yes, like most resized scripts. There's really not much to prevent that unless you buy a software or a shareware scripts from a company.

LastMitch
Industrious Poster
4,212 posts since Mar 2012
Reputation Points: 134
Solved Threads: 336
Skill Endorsements: 45

thank you, i just set size when i called on the pics. seems to work for me.

$image1 = "<img src=avatar/$image0.png width=30 height=30 />";
TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by LastMitch

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1037 seconds using 2.7MB