singapore: the smallest big galery
home » forum » Mods » Limiting allowable size of uploaded image

You are not logged in.

  • Index
  •  » Mods
  •  » Limiting allowable size of uploaded image

#1 2008-07-14 21:56:52

wokandrice
Member

Limiting allowable size of uploaded image

Has anyone modified singapore to limit the maximum image size (pixels not bytes) that a user can upload to singapore?

In order to save space on my server I want to limit the size of images that users can upload to 640 x 460 pixels (or 460 x 640).

I have a tiny bit of php knowledge and could write a program to limit uploaded image size in a really simple image uploading program. However I don't understand the structure of singapore well enough in order to be able to integrate such a feature into singapore.

Any help appreciated.

Thanks

Offline

 

#2 2008-07-15 02:49:54

DC
Administrator

Re: Limiting allowable size of uploaded image

Well actually I did a mod that would prevent small images from being uploaded, why you ask well because I had clients uploading images that were no biger then a thumb or some odd size that created problems for the gd resizing engine so I made it impossible to upload images that were not a set size ...

I dont see any real problem with just making my idea do the oposite so you get what you need.

In includes/admin.class.php

look for the
function addImage()

We will be adding all the code into that func


Now try the following, you may need to play with this a bit as my version is highly modified so know way my line numbers are that of a virgin script.

First add this line
$tempname = $_FILES['sgFileName']['tmp_name'];

after this line
$image = basename($_REQUEST["sgFileName"]);


Then try adding my mod code block


$my_username = ucwords($this->user->username);
      $imgchk = @getimagesize($tempname);
      $w = $imgchk[0];
     
      if($w > '640'){
      return $this->pushError($this->translator->_g("Sorry $my_username your image is to big, images must be no larger then 640x460!", $image));
      }else


right before you see the line with
//make sure image is valid

Again you may need to play with this a bit ...
but it should get you going in the direction you seek.

DC


To code or not to code that is the question?

Did my response help you out? Consider donating by buying me a slice, Whats this? Read More!
http://www.clickcraft.net/slice.php

Offline

 

#3 2008-07-15 22:47:51

wokandrice
Member

Re: Limiting allowable size of uploaded image

Thanks for that. I've had a go with that and it didn't have any effect. It doesn't seem to be assigning an image to $tempname. I've added the following echo statements and I get the following output when trying to upload an image called nextdoor.jpg. When it attempts to check the size of $tempname there is nothing there for it to check.

$tempname = $_FILES['sgFileName']['tmp_name'];
      echo "image ",$image,"<br>";     
      echo "tempname ",$tempname;

image nextdoor.jpg
tempname

Offline

 

#4 2008-07-16 21:47:35

wokandrice
Member

Re: Limiting allowable size of uploaded image

I've fixed this now. Here is the full code of the function addImage if anyone also wants to make the mod.

function addImage()
  {
    if($_REQUEST["sgLocationChoice"] == "remote") {
      $image = $_REQUEST["sgImageURL"];
      $path = $image;
    } elseif($_REQUEST["sgLocationChoice"] == "single") {
      //set filename as requested and strip off any clandestine path info
      if($_REQUEST["sgNameChoice"] == "same") $image = basename($_FILES["sgImageFile"]["name"]);
      else $image = basename($_REQUEST["sgFileName"]);         
      //make sure image is right size
    $imgChk = GetImageSize($_FILES["sgImageFile"]["tmp_name"]);
    $w = $imgChk[0];
    $h = $imgChk[1];     
      if($w > '640' or $h > '640'){
      return $this->pushError($this->translator->_g("Sorry $my_username your image is to big. In order to save space images must be no larger then 640x460 pixels! Please resize your image and try again."));
      }else
      //make sure image is valid
      if(!preg_match("/\.(".$this->config->recognised_extensions.")$/i", $image)) {
        $imgInfo = GetImageSize($_FILES["sgImageFile"]["tmp_name"]);

        switch($imgInfo[2]) {
          case 1 : $image .= '.gif'; break;
          case 2 : $image .= '.jpg'; break;
          case 3 : $image .= '.png'; break;
          case 6 : $image .= '.bmp'; break;
          case 7 :
          case 8 : $image .= '.tif'; break;
          default :
            return $this->pushError($this->translator->_g("Uploaded image '%s' has unrecognised extension and image type could not be determined from file contents.", $image));
        }
      }
     
      $path = $this->config->base_path.$this->config->pathto_galleries.$this->gallery->id."/".$image;
      $srcImage = $image;
     
      if(file_exists($path))
        switch($this->config->upload_overwrite) {
          case 1 : //overwrite
            $this->deleteImage($image);
            break;
          case 2 : //generate unique
            for($i=0;file_exists($path);$i++) {
              $pivot = strrpos($srcImage,".");
              $image = substr($srcImage, 0, $pivot).'-'.$i.substr($srcImage, $pivot,strlen($srcImage)-$pivot);
              $path = $this->config->base_path.$this->config->pathto_galleries.$this->gallery->id."/".$image;
            }
            break;
          case 0 : //raise error
          default :
            return $this->pushError($this->translator->_g("File already exists."));
        }
     
      if(!move_uploaded_file($_FILES["sgImageFile"]["tmp_name"],$path))
        return $this->pushError($this->translator->_g("Could not upload file."));
     
      // try to change file-permissions
      @chmod($path, octdec($this->config->file_mode));
     
    }
   
    $img =& new sgImage($image, $this->gallery);
   
    $img->name = strtr(substr($image, strrpos($image,"/"), strrpos($image,".")-strlen($image)), "_", " ");
    list($img->width, $img->height, $img->type) = GetImageSize($path);
   
    //leave owner of guest-uploaded files as default '__nobody__'
    if(!$this->user->isGuest())
      $img->owner = $this->user->username;
   
    $this->gallery->images[] =& $img;
   
    //set as gallery thumbnail?
    if($this->gallery->imageCount()==1)
      $this->gallery->filename = $img->id;
   
    if($this->io->putGallery($this->gallery)) {
      $this->selectImage($image);
      return $this->pushMessage($this->translator->_g("Image added", $image));
    } else {
      @unlink($path);
      return $this->pushError($this->translator->_g("Unable to save metadata."));
    }
  }

Offline

 

#5 2008-07-16 22:38:21

DC
Administrator

Re: Limiting allowable size of uploaded image

Great glad you got it all working as I said you might need to play with it, but at least my code got you in the correct direction you also added h I was just checking the width as that for my use works fine. Glad you got it working Im sure others can benifit as well ...

DC


To code or not to code that is the question?

Did my response help you out? Consider donating by buying me a slice, Whats this? Read More!
http://www.clickcraft.net/slice.php

Offline

 
  • Index
  •  » Mods
  •  » Limiting allowable size of uploaded image