0

Hi I based this function from the one I've found on web and tried modifying it up for my PHP page for uploading of their icons but I want to limit the user from uploading an image which should only size 100x100px. Well I just call it using this:

uploadImage($id,$_FILES['upload']['name'],$_FILES['upload']['tmp_name']); 

This is the function I made:

 function uploadImage($new_name,$imagename,$tmp_name){ if($tmp_name!=null||$tmp_name!=""){ list($width, $height, $type, $attr) = getimagesize($tmp_name); if($width==100&&$height==100){ $image1 = $imagename; $extension = substr($image1, strrpos($image1, '.') + 1); $image = "$new_name.$extension"; $folder = "Images/"; if($image) { $filename = $folder.$image; $copied = copy($tmp_name, $filename); } else echo "image not uploaded."; } else echo "upload only 100x100px image!"; } } 

Now the problem is that even if I uploaded an image which exceeds the 100 x 100px dimensions it still proceeds without returning any errors and now I'm lost with it.

0

3 Answers 3

3

You probably need to re-factor your code a bit; have a function that checks whether the uploaded image is valid, and then one actually does the upload. Alternatively, you could create a class.

<?php class ImageUpload { public $tmpImage; public $maxWidth = 100; public $maxHeight = 100; public $errors = []; public function __construct($image) { $this->tmpImage = $image; } public function upload() { // Check image is valid; if not throw exception // Check image is within desired dimensions list($width, $height) = getimagesize($this->tmpImage); if ($width > $this->maxWidth || $height > $this->maxHeight) { throw new Exception(sprintf('Your image exceeded the maximum dimensions (%d&times;%d)', $this->maxWidth, $this->maxHeight)); } // Create filename // Do the upload logic, i.e. move_uploaded_file() } } 

You can then use this class as follows:

<?php $imageUpload = new ImageUpload($_FILES['upload']['tmp_name']); try { $imageUpload->upload(); } catch (Exception $e) { echo 'An error occurred: ' . $e->getMessage(); } 

This was written off the cuff, so they may be errors. But hopefully it demonstrates a better way to handle file uploads, and errors that may occur during upload.

Sign up to request clarification or add additional context in comments.

Comments

2

well, you can also resize the image after upload.

function createFixSizeImage( $pathToImages, $pathToFixSizeImages, $Width ) { // open the directory $dir = opendir( $pathToImages ); // loop through it, looking for any/all JPG files: while (false !== ($fname = readdir( $dir ))) { $image_info = getimagesize( "path/to/images/".$fname ); $image_width = $image_info[0]; $image_height = $image_info[1]; $image_type = $image_info[2]; switch ( $image_type ) { case IMAGETYPE_JPEG: // parse path for the extension $info = pathinfo($pathToImages . $fname); // continue only if this is a JPEG image if ( strtolower($info['extension']) == 'jpeg' ) { // load image and get image size $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img ); // give the size,u want $new_width = 100; $new_height = 100; // create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // save Fix Size Images into a file imagejpeg( $tmp_img, "{$pathToFixSizeImages}{$fname}" ); } break; case IMAGETYPE_PNG: // parse path for the extension $info = pathinfo($pathToImages . $fname); // continue only if this is a JPEG image if ( strtolower($info['extension']) == 'png' ) { // load image and get image size $img = imagecreatefrompng( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img ); $new_width = 100; $new_height = 100; // create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // save Fix Size Images into a file imagejpeg( $tmp_img, "{$pathToFixSizeImages}{$fname}" ); } break; case IMAGETYPE_BMP: echo "bmp"; break; default: break; } } } // close the directory closedir( $dir ); } createFixSizeImage("path","path/to/images/to/be/saved",100); 

Comments

1

Extending more or less unknown code and then debugging it, is like you wrote some code weeks ago and you don't understand it any longer.

In your case you are extending some existing code (you have not posted the original code, but your wrote that you did it that way) by adding the feature of checking the image size.

So that you do not need to edit much of the (unknown but) working code, create the new feature as a function of it's own:

/** * @param string $file * @param int $with * @param int $height * @return bool|null true/false if image has that exact size, null on error. */ function image_has_size($file, $width, $height) { $result = getimagesize($file); if ($count($result) < 2) { return null; } list($file_width, $file_height) = $result; return ($file_width == (int) $width) && ($file_height == (int) $height); } 

You now have the new functionality in a single function you can much more easily integrate into the original (hopefully otherwise working) code.

Usage:

$imageHasCorrectSize = image_has_size($tmp_name, 100, 100); 

So whenever you change code, do it like a surgeon, keeping the cuts as small as possible.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.