roldahayes Posted October 18, 2010 Share Posted October 18, 2010 Hi, I'm using the script below to that allows a user to upload a photo, which then appears on the page as a thumbnail, the image can be clicked and opens up in a lightbox image pop up. All this works fine except the images are not resizing. I.e if a a picture is 1024px wide, before upload - it stays that size on the server. I need it to compress down to around 600px wide and have the KB of the file reduced. I know that I should be using GD library to do this but not sure how to implement it. The script I am using is here: ############################################################################################## function upload($user_id,$fileid) ############################################################################################## { global $db; $maxsize = $_POST['MAX_FILE_SIZE']; if(is_uploaded_file($_FILES['userfile']['tmp_name'][$fileid])) { if($_FILES['userfile']['size'][$fileid] < $maxsize) { $imgData = addslashes(file_get_contents($_FILES['userfile']['tmp_name'][$fileid])); $size = getimagesize($_FILES['userfile']['tmp_name'][$fileid]); $sql_query = "SELECT * FROM user_images WHERE image_id = '999999999999999999'"; if(mysqli_num_rows(mysqli_query($db, $sql_query)) > 0) { $sql_query = "UPDATE user_images SET image_type = '{$size['mime']}', image = '{$imgData}', image_size = '{$size[3]}', image_name = '{$_FILES['userfile']['name'][$fileid]}' WHERE image_id = '$user_id'"; } else { $sql_query = "INSERT INTO user_images ( image_type ,image, image_size, image_name) VALUES ('{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name'][$fileid]}')"; } if(!mysqli_query($db,$sql_query)) { echo '<div class="error">Unable to upload file</div>'; } else { $return = mysqli_insert_id($db); } } else { echo '<div>File exceeds the Maximum File limit. Skipping uploading of image.</div> <div>Maximum File limit is '.$maxsize.'</div> <div>File '.$_FILES['userfile']['name'][$fileid].' is '.$_FILES['userfile']['size'][$fileid].' bytes</div> <hr />'; } } return $return; }//END - upload() ############################################################################################## function imageResize($width, $height, $target) ############################################################################################## { //protect function if image sizes not set (i.e. image doesn't exist in database) if((!isset($width)) || (!isset($height))) { $width = '1'; $height = '1'; } //if trying to resize an image that is much smaller than target...essentially do nothing and keep the original sizing elseif(($width < $target) && ($height < $target)) { } else { //takes the larger size of the width and height and applies the formula accordingly...this is so this script will work dynamically with any size image if ($width > $height) { $percentage = ($target / $width); } else { $percentage = ($target / $height); } //gets the new value and applies the percentage, then rounds the value $width = round($width * $percentage); $height = round($height * $percentage); } //returns the new sizes in html image tag format... return "width=\"$width\" height=\"$height\""; }//END - imageResize ############################################################################################## Quote Link to comment Share on other sites More sharing options...
litebearer Posted October 18, 2010 Share Posted October 18, 2010 might look at ... http://www.nstoia.com/toh/imageresize.php Quote Link to comment Share on other sites More sharing options...
roldahayes Posted October 18, 2010 Author Share Posted October 18, 2010 I've found this function on another website: Would this sort of thing work ? (if i changed the names to what I already have?) // Resize the original image $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); ?> As final step we returns with the new image. The complete resize function looks like this: <?php function resizeImage($originalImage,$toWidth,$toHeight){ // Get the original geometry and calculate scales list($width, $height) = getimagesize($originalImage); $xscale=$width/$toWidth; $yscale=$height/$toHeight; // Recalculate new size with default ratio if ($yscale>$xscale){ $new_width = round($width * (1/$yscale)); $new_height = round($height * (1/$yscale)); } else { $new_width = round($width * (1/$xscale)); $new_height = round($height * (1/$xscale)); } // Resize the original image $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $imageResized; } Quote Link to comment Share on other sites More sharing options...
quasiman Posted October 18, 2010 Share Posted October 18, 2010 That should work, but you might want to check by file type also: function resizeImage($originalImage,$toWidth,$toHeight){ // Get the original geometry and calculate scales list($width, $height, $type) = getimagesize($originalImage); $xscale=$width/$toWidth; $yscale=$height/$toHeight; // Recalculate new size with default ratio if ($yscale>$xscale){ $new_width = round($width * (1/$yscale)); $new_height = round($height * (1/$yscale)); } else { $new_width = round($width * (1/$xscale)); $new_height = round($height * (1/$xscale)); } // Resize the original image $imageResized = imagecreatetruecolor($new_width, $new_height); switch ($type) { case "1": //gif image $imageTmp = imagecreatefromgif($originalImage); break; case "2": //jpg image $imageTmp = imagecreatefromjpeg ($originalImage); break; case "3": //png image $imageTmp = imagecreatefrompng ($originalImage); break; } imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $imageResized; } Quote Link to comment Share on other sites More sharing options...
roldahayes Posted October 18, 2010 Author Share Posted October 18, 2010 Hmmm, this caused errors with varible names on lots of other pages... Does anyone have a way for me to actually just edit the (semi) working code to reduce the image sizes properly without changing any of the names that will cause the rest of the site to malfunction? Quote Link to comment Share on other sites More sharing options...
quasiman Posted October 18, 2010 Share Posted October 18, 2010 not sure if you know this or not, but your original code isn't calling the function 'imageResize' anywhere. I started rewriting it, and then realized....hey wait a minute, lol Quote Link to comment Share on other sites More sharing options...
roldahayes Posted October 18, 2010 Author Share Posted October 18, 2010 to be honest... I'm totally lost! The script was written for me - but I soon realised that the images were not being reduced - just resized so they are taking up way too much space on the server and the pop ups are too big as well... Ideally, I need to keep the script roughly the same as I really dont have the knowledge to change things around that much... You can see an example of how the page looks at the moment, and see that the pops are really big. http://www.viewmykitchen.com/viewprofile.php?id=13 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.