Question

I am using the file uploading class in codeigniter and can successfully upload images. My question is how to go about making sure the user uploads an image of only a fixed size, 600px by 360px, nothing more nothing less? I have tried using the upload data array to get the image height and width and am checking against these values TRUE or FALSE. If false, I pass an error string to the view, if true, I display nothing in the view, but I am not sure it is working right. Any one can help with how to go about doing this? Thank you in advance!

Was it helpful?

Solution 2

You can use GDI library tools to find out the image height and width, aswell as other information.

for example:

$imgInfo = getimagesize("/path/to/image.png");
if ($imgInfo[0] != 600) && $imgInfo[1] != 360){
   throw new Exception("Illegal image size");
}

http://www.php.net/manual/en/function.getimagesize.php

OTHER TIPS

You can use the Image Manipulating library in conjunction with File Uploading library.

Using File Uploading library, you can:

  • make sure that uploaded files have exact size that you want:

And you can use Image Manipulating library to resize uploaded image if needed, so comlete solution would look like:

$fileInfo = $this->upload->data();

if( $fileInfo['width'] == 600 && $fileInfo['height'] == 300 ) {
    // do something
} else {
    // initiialize library
    $this->image_lib->resize();
}

You can read more about these libraries here:

http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top