Jump to content

image resize/ alter resolution


Doug

Recommended Posts

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>';
}
?>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.