Doug Posted September 18, 2011 Share Posted September 18, 2011 Hello, I have a website where users may upload image (photos). The problem I have is that there are a lot and the download time is very long. I know I can ask users to limit the image size but feel this will discourage people from uploading images as they will not want to resize. Is there any way that I can automatically store images in a lower resolution once uploaded? One that takes less memory., and will therefore load more quickly. I have seen images load quickly on other website (eg Facebook) but for the life of me can’t see how it is done. I have the code below which resizes the images loads them in to two separate tables but each image retains the same resolution. They are in essence the same image. I would like to have one table with a lower resolution while retaining the higher resolution in another. So that I can have thumbnails that load quickly and if the user wishes to see the larger image they can click on it. Is this at all possible? Thanks for any replies <?php require_once('top1.php'); ?> <?php require_once('navmenu.php'); ?> <title>Add a picture</title> <?php require_once('appvars.php'); require_once('connectvars1.php'); //Connect to the database $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); error_reporting(0); $change=""; $abc=""; define ("MAX_SIZE","40000"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if($_SERVER["REQUEST_METHOD"] == "POST") { $name = trim($_POST['name']); $image =$_FILES["file"]["name"]; $uploadedfile = $_FILES['file']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['file']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $change='<div class="msgdiv">Unknown Image extension </div> '; $errors=1; } else { $size=filesize($_FILES['file']['tmp_name']); if($extension=="jpg" || $extension=="jpeg" ) { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if($extension=="png") { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } echo $scr; list($width,$height)=getimagesize($uploadedfile); $newwidth=960; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); $newwidth1=100; $newheight1=($height/$width)*$newwidth1; $tmp1=imagecreatetruecolor($newwidth1,$newheight1); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); $filename = "images/" . $_FILES['file']['name']; $filename1 = "". $_FILES['file']['name']; //Write the data to the database $query = "INSERT into pictures values (0, '$name', '$filename1', NOW())"; mysqli_query($dbc, $query); $query2 = "INSERT into bigpictures values (0, '$name', '$filename', NOW())"; mysqli_query($dbc, $query2); imagejpeg($tmp,$filename,100); imagejpeg($tmp1,$filename1,1); $name = ""; imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); }} } //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { // mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'"); $change=' <div class="msgdiv">Image Uploaded Successfully!</div>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/247383-image-resize-alter-resolution/ Share on other sites More sharing options...
JKG Posted September 18, 2011 Share Posted September 18, 2011 so you are having problems with this script? or does the script work, you just want to add to it? Does it resize the uploaded images? Quote Link to comment https://forums.phpfreaks.com/topic/247383-image-resize-alter-resolution/#findComment-1270433 Share on other sites More sharing options...
Doug Posted September 19, 2011 Author Share Posted September 19, 2011 Yes, the script works. It can resize the images, but it does not alter the resolution so the images, no matter what size they are, still take up a lot of memory. I would like to add/ edit the script so that uploaded images can be automatically altered to take up less memory. I assume this would be by lowering the resolution. Is this at all possible? Quote Link to comment https://forums.phpfreaks.com/topic/247383-image-resize-alter-resolution/#findComment-1270600 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.