0

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

2
Contributors
6
Replies
22
Views
4 Years
Discussion Span
Last Post by Squidge
0

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

0

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>";
}
0

$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.

Edited by LastMitch: grammer

1

@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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.