Evening all.

I am goign through my Image upload script and found it looks messy, would you recommend an cleaner way of doing this:

    move_uploaded_file($_FILES['file']['tmp_name'], 'photos/' . $_FILES['file']['name']);
    try {
        $fileName = $_FILES['file']['name'];
        $resizeObj = new resize('photos/' . $fileName);

// *** Resize options: exact, portrait, landscape, auto, crop
        $resizeObj->resizeImage(225, 150, 'auto');
        $extension = strtolower(strrchr($fileName, '.')); // extract file extension
        $clean = str_replace($extension, '', $fileName); // replace file extension with nothing -> removes

        $resizeObj->saveImage('photos/' . $clean . '.png', 100);
        unlink('photos/' . $fileName);
    } catch (Exception $e) {
        echo 'Error ' . $e->getMessage() . "\n";
    }

I have tried parsing the $_FILES['file']['name'] directly into the resize class but this does do anything, hence i save the original -> modify the size -> strip the extension -> save as png -> delet original

Any input is greatly appreciated.

Squidge

Recommended Answers

All 6 Replies

It also appears my file validation is not working, it returns the file as not supported:

$excepted_EXT = array('image/gif' => 'gif',
    'image/jpeg' => 'jpeg',
    'image/jpg' => 'jpg',
    'image/png' => 'png');
$fileType = strtolower($_FILES['file']['type']);

if (in_array($fileType, $excepted_EXT) == TRUE) {
    echo "You got it right, $fileType is accepted";
} else {
    echo "WTF!!!!<br>";
    var_dump($excepted_EXT);
}

Even though the file is a jpg

Member Avatar for LastMitch

I am goign through my Image upload script and found it looks messy, would you recommend an cleaner way of doing this:

It's not much you can add or modify.

Take a look at this (I think you might like this one):

http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

The first posted code works well, was hoping there maybe away round having to move_upploaded_file before running through the resize.

Thats a nice link thanks LastMitch, this is the one i used:

http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/

Resizes the file to many variations.

Could you look over the above file validation? Think i added is as you were posting :)

Sorted:

$mime = array('image/gif','image/jpeg','image/jpg','image/png');
//$fileType = strtolower($_FILES['file']['type']);

if (in_array($_FILES['file']['type'], $mime)) {
    echo "You got it right, $fileType is accepted";
} else {
    echo "mime type is not allowed<br>";
}
Member Avatar for LastMitch

$excepted_EXT function is not going to work. You created an array.

Instead of this

$excepted_EXT = array('image/gif' => 'gif','image/jpeg' => 'jpeg','image/jpg' => 'jpg','image/png' => 'png');
$fileType = strtolower($_FILES['file']['type']);

take out image/ and put type instead.

$excepted_EXT = array('type' => 'gif','type' => 'jpeg','type' => 'jpg','type' => 'png');
$fileType = strtolower($_FILES['file']['type']);
if (in_array($fileType, $excepted_EXT) == TRUE) {
echo "You got it right, $fileType is accepted";
} else {
echo "WTF!!!!<br>";
var_dump($excepted_EXT);
}

Then try to run it. If it doesn't work then used a switch statement

The link that you provided the code is written in OOP so you need to create a function something like this one from the link:

private function openImage($file)  
{  
// *** Get extension  
$extension = strtolower(strrchr($file, '.'));  

switch($extension)  
{  
    case '.jpg':  
    case '.jpeg':  
        $img = @imagecreatefromjpeg($file);  
        break;  
    case '.gif':  
        $img = @imagecreatefromgif($file);  
        break;  
    case '.png':  
        $img = @imagecreatefrompng($file);  
        break;  
    default:  
        $img = false;  
        break;  
}  
return $img;  
}

to identify the extension and then you go to resize. Resizing images code are bit hard to modify especially if it's in OOP. Then it's even differcult.

@LastMitch,

I am validating the mime type before progressing to the resizing OOP Class.

You are correct in the code you supplied, this is in the class functionalilty. I had created a multi array instead of one that i can validate the ['type'] from $_FILES.

So i cahnged the first line to:

$var = array('mimetype1','mimetype2');

And this parsed.

Thank you for your assistance and input. It is greatly received.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.