Jump to content


Photo
- - - - -

Force uploaded images to be no greater than a particular size


  • Please log in to reply
No replies to this topic

#1 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 04 May 2007 - 04:36 PM

This is a modification of the modification (how about that) that was originally posted in June 27, 2006 here.
Look in add.php for

CODE
//is user allowed to add?

and above that insert

CODE
//mod - added on May 4, 2007 in response to user request
//uncomment to use
$resize_orig_threshold = 1024;

Then look for

CODE
if($config['debug_mode'] == 1)   echo "Cannot change the mode of file ($path_server)";

and below that insert

CODE
//May 4, 2007 -- resize original image to $resize_orig_width x $resize_orig_height if it's bigger than it
                //added in response to user request (...)
                //uncomment the following lines and the ones at the top of the page ($resize_orig_width and $resize_orig_height) to resize all original
                //versions of uploaded pictures
                
                $arr_correct_dimensions = correctDimensions($uploaded_pwidth, $uploaded_pheight, $resize_orig_threshold); //new in v0.9.4 DEV
                $uploaded_pwidth = $arr_correct_dimensions[0];
                $uploaded_pheight = $arr_correct_dimensions[1];

                include("image_man.php");
                $imn = new ImageManipulator($config, $lang);
                $imn -> resizeOriginal($filename,$uploaded_pwidth,$uploaded_pheight,$final_upload_location, $final_upload_location_server,$config,$extension);

Finally, look for

CODE
include("image_man.php");
$imn = new ImageManipulator($config, $lang);

and replace it with

CODE
//include("image_man.php");
//$imn = new ImageManipulator($config, $lang);

Save and close.

Now look in image_man.php for

CODE
}//end class

and above that insert

CODE
//----------------------
    //resizeOriginal(): resize original if it's bigger than $width x $height (Set in add.php)
    //User request.  Added on June 27, 2006
    //----------------------
    function resizeOriginal($filename, $width, $height, $path, $path_server, $config, $type) {
        $path_server = $path_server.$filename;
    
        $msg = "1"; //initially, assume all is well
        
        $size = getimagesize($path_server); //get width/height  (path support from PHP 4.05)
    
        //resize original as normal
        if($type == "jpg" || $type == "jpeg")   $image = @imagecreatefromjpeg($path_server);
        elseif($type == "png")   $image = @imagecreatefrompng($path_server);
        elseif($type == "gif")   $image = @imagecreatefromgif($path_server);
        
        //resize only if bigger than than the allowed max size
        if($size[0]>$width || $size[1]>$height) {
            //echo "creating image";
            $image_resized = imagecreatetruecolor($width,$height);
            @imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); //resize and resample image
            
            // try to save new image
            if($type == "jpg" || $type == "jpeg") {
                if (!@imagejpeg($image_resized, $path_server, 100))   $msg .= $lang['image_man_check'];
            }
            elseif($type == "png") {
                if (!@imagepng($image_resized, $path_server))   $msg .= $lang['image_man_check'];
            }
            elseif($type == "gif") {
                if (!@imagegif($image_resized, $path_server))   $msg .= $lang['image_man_check'];
            }
        }
        
        return $msg;
    }

Save and close.

Finally, look in functions/f_global.php for (at the very bottom)

CODE
?>

and above that insert

CODE
//--------------------------------------------
// correctDimensions(): maintains the ratio of the width and height args while making sure that the
// returned coords are less than the threshold (new in v0.9.4 DEV, user request: Bruwat)
// if desired, pass in height instead of width and vice versa to enforce a threshold on the height
// change / 2 to control granularity
//--------------------------------------------
function correctDimensions($width, $height, $width_threshold) {
    if($width <= $width_threshold || $width_threshold == "")   return array($width, $height);

    $width = ceil($width / 2);
    $height = ceil($height / 2);

    $arr = correctDimensions($width, $height, $width_threshold);
    
    return $arr;
}


Do a search for correctDimensions first since you may already have it, in which case you don't need to add it again.


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users