0

Hi
I know I have been going round the houses with this image resizing quite a bit...but still trying to do it. Some progress has been made. This is not using Imagick, as I have no idea about this library.

So what the below code is now doing is picking up the attributes of the images being selected by the user and then reducing them to a set size (via sqrt etc). What I'm trying to achieve is for any image loaded to be reduced to around 32kb in size.

What is happening is the following:
.gif images - these are working great, and any .gif image loaded is being reduced to sizes between 30-35kb.
.png images - also reasonably consistent, although slightly too small sometimes.

Main Issue - .jpg/.jpeg images...
.jpg images which are already quite small in filesize are being reduced and the image thumbnail displays to the user fine.
.jpg images which are large (i.e. the ones that actually really need to be reduced) are usually coming out at 3kb in filesize, and the thumbnail is just a black image, no picture showing.

What I'm now thinking is that the only attribute of the original image that I don't have is the actual quality of the picture. This might be messing up the jpg's? If I could know the quality of incoming images, then I could set the quality in my resizing function.

If anyone could shed some light on this or help me to understand what is missing or why the .jpg don't resize property that would be great.

(apologies for really long script - but only because 7 images are being loaded and part of the issue so far seems to be that I getimagesize() or something in the code wasn't liking the repitition. This is now working fine, and I will put it all in function when I understand what else needs to be done....

<?php require_once('Connections/dreamsin_localhost.php'); 
      require_once('myaccess/appvars.php');

// Connect to the database
  $dbc = mysqli_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxxxx", "xxxxxxxxxxx");

function createfilename($ID,$image_name)
{

	$output = "Start";

	if (!empty($image_name)) 
	{
	$output = number_format($ID,0,".",",") . '_' . time() . '_' . mt_rand() .$image_name;
	}
	else
	{
	$output = "No Image";
	}

	return $output;

}

function checkfile($image_name,$type,$size,$tmp_name,$error,$old_width,$old_height)
{
	
	$output ='start';

	if (!empty($image_name)) 
	   {
      if (
	  (($type == 'image/gif') || ($type == 'image/jpg') || ($type == 'image/pjpeg') || ($type == 'image/png') || ($type == 'image/jpeg'))
	  && (($size > 0) && ($size <= GW_MAXFILESIZE))
		  && ($error == '0'))
	  {
        // Move the files to the target upload folder
          $target = GW_UPLOADPATH . $image_name;
		  if (move_uploaded_file($tmp_name , $target) ) {$output = 'success';} else {$output = 'load error'; }
	  } 

	  elseif ($size > GW_MAXFILESIZE){ 

 		$target1 = GW_UPLOADPATH . 'original' . $image_name  ;
       	$target2 = GW_UPLOADPATH . $image_name  ;

// leave this line in if you want to keep original 
//     	if (move_uploaded_file($tmp_name , $target1) ) {$output = 'success';} else {$output = 'load error'; }

	    $reductionfactor =   sqrt ( $size / GW_MAXFILESIZE )  ;
		$new_height = number_format( $old_height / $reductionfactor ,0 , '.', '');
		$new_width = number_format( $old_width / $reductionfactor ,0 , '.', '');

	    $new_image = imagecreatetruecolor($new_width, $new_height);

      	$image_info = getimagesize($filename);
        if(( $type == 'image/jpg'  )|| ($type == 'image/pjpeg')   || ($type == 'image/jpeg')){
 			imagejpeg($new_image,$target2,85);  
			$old_image = imagecreatefromjpeg($tmp_name);
    		imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); }
        elseif( $type == 'image/gif'  ) {
			$old_image = imagecreatefromgif($tmp_name);
    		imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
 			imagegif($new_image,$target2);  }
        elseif( $type == 'image/png'  ) {
			$old_image = imagecreatefrompng($tmp_name);
    		imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
 			imagepng($new_image,$target2);  }

	  	$output='success'; 

	   }
	  else { $output='error'; }
	   }
	  else { $output='success'; }

	return $output;
}



  if (isset($_POST['submit'])) {
    // Grab the profile data from the POST
    $ID = ($_POST['ID']);
	$add_owner = mysqli_real_escape_string($dbc, trim($_POST['add_owner']));
	$add_block = mysqli_real_escape_string($dbc, trim($_POST['add_block']));
	$add_road = mysqli_real_escape_string($dbc, trim($_POST['add_road']));
	$add_house = mysqli_real_escape_string($dbc, trim($_POST['add_house']));
	$add_unit = mysqli_real_escape_string($dbc, trim($_POST['add_unit']));
	$add_area = mysqli_real_escape_string($dbc, trim($_POST['add_area']));
	$add_postcode = mysqli_real_escape_string($dbc, trim($_POST['add_postcode']));
	$add_zone = mysqli_real_escape_string($dbc, trim($_POST['add_zone']));
	$prop_cat = mysqli_real_escape_string($dbc, trim($_POST['prop_cat']));
	$prop_hot = mysqli_real_escape_string($dbc, trim($_POST['prop_hot']));
	$prop_sold = mysqli_real_escape_string($dbc, trim($_POST['prop_sold']));
	$prop_contact = mysqli_real_escape_string($dbc, trim($_POST['prop_contact']));
	$prop_type = mysqli_real_escape_string($dbc, trim($_POST['prop_type']));
	if ($prop_type == 'Private Apartments') { $prop_saletype = ($_POST['prop_saletype1']); } 
	else if ($prop_type == 'Landed Property') { $prop_saletype = ($_POST['prop_saletype2']);}
	else if ($prop_type == 'Hdb Flat') { $prop_saletype = ($_POST['prop_saletype3']);}
	else if ($prop_type == 'Commercial') { $prop_saletype = ($_POST['prop_saletype4']);}
	else if ($prop_type == 'Hudc Apartments') { $prop_saletype = ($_POST['prop_saletype5']);}
	$prop_desc = mysqli_real_escape_string($dbc, trim($_POST['prop_desc']));
	$prop_tenure = mysqli_real_escape_string($dbc, trim($_POST['prop_tenure']));
	$prop_bedroom = mysqli_real_escape_string($dbc, trim($_POST['prop_bedroom']));
	$prop_bathroom = mysqli_real_escape_string($dbc, trim($_POST['prop_bathroom']));
	$prop_price = mysqli_real_escape_string($dbc, trim($_POST['prop_price']));
	$prop_garage = mysqli_real_escape_string($dbc, trim($_POST['prop_garage']));
	$prop_park = mysqli_real_escape_string($dbc, trim($_POST['prop_park']));
	$prop_pool = mysqli_real_escape_string($dbc, trim($_POST['prop_pool']));
	$prop_garden = mysqli_real_escape_string($dbc, trim($_POST['prop_garden']));
	$prop_alarm = mysqli_real_escape_string($dbc, trim($_POST['prop_alarm']));
	$prop_appliance = mysqli_real_escape_string($dbc, trim($_POST['prop_appliance']));
	$screenpath = "images/propertyimages/";
	$image_main_name = mysqli_real_escape_string($dbc, trim($_FILES['image_main']['name']));
	$image_kitchen_name = mysqli_real_escape_string($dbc, trim($_FILES['image_kitchen']['name']));
	$image_bed_name = mysqli_real_escape_string($dbc, trim($_FILES['image_bed']['name']));
	$image_bath_name = mysqli_real_escape_string($dbc, trim($_FILES['image_bath']['name']));
	$image_living_name = mysqli_real_escape_string($dbc, trim($_FILES['image_living']['name']));
	$image_dining_name = mysqli_real_escape_string($dbc, trim($_FILES['image_dining']['name']));
	$image_garden_name = mysqli_real_escape_string($dbc, trim($_FILES['image_garden']['name']));
	

	$image_main_name = createfilename($_SESSION[ID],$_FILES['image_main']['name']);
	if (!empty($image_main_name)) { list($old_width, $old_height, $type, $attr)= getimagesize($_FILES['image_main']['tmp_name']) ;}	else { $old_width = 0 ; $old_height = 0 ;}
    $image_main_check = checkfile($image_main_name,$_FILES['image_main']['type'],$_FILES['image_main']['size'],$_FILES['image_main']['tmp_name'],$_FILES['image_main']['error'],$old_width,$old_height) ; 
	if ($image_main_name == 'No Image')  {  $image_main_name = 'dot.gif';  $image_main_check = 'success'; }
		
	$image_kitchen_name = createfilename($_SESSION[ID],$_FILES['image_kitchen']['name']);
	if (!empty($image_kitchen_name)) { list($old_width, $old_height, $type, $attr)= getimagesize($_FILES['image_kitchen']['tmp_name']); }	else { $old_width = 0 ; $old_height = 0 ;}
	$image_kitchen_check = checkfile($image_kitchen_name,$_FILES['image_kitchen']['type'],$_FILES['image_kitchen']['size'],$_FILES['image_kitchen']['tmp_name'],$_FILES['image_kitchen']['error'],$old_width,$old_height)  ;
	if ($image_kitchen_name == 'No Image')  {  $image_kitchen_name = 'dot.gif';  $image_kitchen_check = 'success'; }
	
	$image_bed_name = createfilename($_SESSION[ID],$_FILES['image_bed']['name']);
	if (!empty($image_bed_name)) { list($old_width, $old_height, $type, $attr)= getimagesize($_FILES['image_bed']['tmp_name']); }	else { $old_width = 0 ; $old_height = 0 ;}
	$image_bed_check = checkfile($image_bed_name,$_FILES['image_bed']['type'],$_FILES['image_bed']['size'],$_FILES['image_bed']['tmp_name'],$_FILES['image_bed']['error'],$old_width,$old_height)  ;
	if ($image_bed_name == 'No Image')  {  $image_bed_name = 'dot.gif';  $image_bed_check = 'success'; }
	
	$image_bath_name = createfilename($_SESSION[ID],$_FILES['image_bath']['name']);
	if (!empty($image_bath_name)) { list($old_width, $old_height, $type, $attr)= getimagesize($_FILES['image_bath']['tmp_name']); }	else { $old_width = 0 ; $old_height = 0 ;}
	$image_bath_check = checkfile($image_bath_name,$_FILES['image_bath']['type'],$_FILES['image_bath']['size'],$_FILES['image_bath']['tmp_name'],$_FILES['image_bath']['error'],$old_width,$old_height)  ;
	if ($image_bath_name == 'No Image')  {  $image_bath_name = 'dot.gif';  $image_bath_check = 'success'; }
	
	$image_living_name = createfilename($_SESSION[ID],$_FILES['image_living']['name']);
	if (!empty($image_living_name)) { list($old_width, $old_height, $type, $attr)= getimagesize($_FILES['image_living']['tmp_name']); }	else { $old_width = 0 ; $old_height = 0 ;}
	$image_living_check = checkfile($image_living_name,$_FILES['image_living']['type'],$_FILES['image_living']['size'],$_FILES['image_living']['tmp_name'],$_FILES['image_living']['error'],$old_width,$old_height)  ;
	if ($image_living_name == 'No Image')  {  $image_living_name = 'dot.gif';  $image_living_check = 'success'; }
	
	$image_dining_name = createfilename($_SESSION[ID],$_FILES['image_dining']['name']);
	if (!empty($image_living_name)) { list($old_width, $old_height, $type, $attr)= getimagesize($_FILES['image_dining']['tmp_name']); }	else { $old_width = 0 ; $old_height = 0 ;}
	$image_dining_check = checkfile($image_dining_name,$_FILES['image_dining']['type'],$_FILES['image_dining']['size'],$_FILES['image_dining']['tmp_name'],$_FILES['image_dining']['error'],$old_width,$old_height)  ;
	if ($image_dining_name == 'No Image')  {  $image_dining_name = 'dot.gif';  $image_dining_check = 'success'; }

	$image_garden_name = createfilename($_SESSION[ID],$_FILES['image_garden']['name']);
	if (!empty($image_garden_name)) { list($old_width, $old_height, $type, $attr)= getimagesize($_FILES['image_garden']['tmp_name']); }	else { $old_width = 0 ; $old_height = 0 ;}
	$image_garden_check = checkfile($image_garden_name,$_FILES['image_garden']['type'],$_FILES['image_garden']['size'],$_FILES['image_garden']['tmp_name'],$_FILES['image_garden']['error'],$old_width,$old_height)  ;
	if ($image_garden_name == 'No Image')  {  $image_garden_name = 'dot.gif';  $image_garden_check = 'success'; }

   
	  if ($image_main_check == 'success' && $image_kitchen_check == 'success'  && $image_bed_check == 'success'  && $image_bath_check == 'success' && $image_living_check == 'success' && $image_dining_check == 'success' && $image_garden_check == 'success' ) 
		  {
	
   
// Connect to the database
  $dbc = mysqli_connect("localhost", "xxxxxxxxx", "xxxxxxxxx", "xxxxxxxxxx");	

 $query = "INSERT INTO DEV_property_details
VALUES (0, NOW(), '$ID', '$add_owner', '$add_block', '$add_road', '$add_house', '$add_unit', '$add_area', '$add_postcode', '$add_zone', '$prop_cat', '$prop_hot', 'Active', '$prop_contact', '$prop_type', '$prop_saletype', '$prop_desc', '$prop_tenure', '$prop_bedroom', '$prop_bathroom', '$prop_price', '$prop_garage', '$prop_park', '$prop_pool', '$prop_garden', '$prop_alarm', '$prop_appliance', '$screenpath', '$image_main_name', '$image_kitchen_name', '$image_bed_name', '$image_bath_name', '$image_living_name', '$image_dining_name', '$image_garden_name')";
                      
  mysqli_query($dbc, $query);
// Confirm success with the user
		echo '<p class="maintextbold">Thanks for Listing your Property! Please view your listing below. <br />You may <a href="../view_property_record.php">view and edit</a> your listing at anytime.</p>';
   	    ?>
                <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
                  <tr class="maintext">
                    <td width="109">Registered User ID: </td>
                    <td width="144"><span class="maintextbold"><?php echo number_format($_POST[ID],0,".",","); ?></span></td>
                    <td width="170">Property Owner: </td>
                    <td width="177" ><span class="maintextbold"><?php echo "$add_owner"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>Block No.: </td>
                    <td><span class="maintextbold"><?php echo "$add_block"?></span></td>
                    <td>Road Name: </td>
                    <td><span class="maintextbold"><?php echo "$add_road"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>House Name: </td>
                    <td><span class="maintextbold"><?php echo "$add_house"?></span></td>
                    <td>Unit No.: </td>
                    <td><span class="maintextbold"><?php echo "$add_unit"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>Area: </td>
                    <td><span class="maintextbold"><?php echo "$add_area"?></span></td>
                    <td>Postcode: </td>
                    <td><span class="maintextbold"><?php echo "$add_postcode"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>Zone No.: </td>
                    <td><span class="maintextbold"><?php echo "$add_zone"?></span></td>
                    <td>Category:</td>
                    <td><span class="maintextbold"><?php echo "$prop_cat"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>Property Contact: </td>
                    <td><span class="maintextbold"><?php echo "$prop_contact"?></span></td>
                    <td>Property Type: </td>
                    <td><span class="maintextbold"><?php echo "$prop_type"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>Property sub-type: </td>
                    <td><span class="maintextbold"><?php echo "$prop_saletype"?></span></td>
                    <td>Tenure: </td>
                    <td><span class="maintextbold"><?php echo "$prop_tenure"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>Bedrooms: </td>
                    <td><span class="maintextbold"><?php echo "$prop_bedroom"?></span></td>
                    <td>Bathrooms: </td>
                    <td><span class="maintextbold"><?php echo "$prop_bathroom"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>Price:</td>
                    <td><span class="maintextbold">$<?php echo number_format($prop_price,0,".",",")?></span></td>
                    <td>Garage: </td>
                    <td><span class="maintextbold"><?php echo "$prop_garage"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>Off Street Parking: </td>
                    <td><span class="maintextbold"><?php echo "$prop_park"?></span></td>
                    <td>Swimming Pool: </td>
                    <td><span class="maintextbold"><?php echo "$prop_pool"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>Garden: </td>
                    <td><span class="maintextbold"><?php echo "$prop_garden"?></span></td>
                    <td>Alarm System: </td>
                    <td><span class="maintextbold"><?php echo "$prop_alarm"?></span></td>
                    </tr>
                  <tr class="maintext">
                    <td>White Goods: </td>
                    <td><span class="maintextbold"><?php echo "$prop_appliance" ?></span></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    </tr>
                  </table>
                <table width="500" border="0" align="center">
                  <tr class="maintext">
                    <td height="29" colspan="8">Property Description:<br /></td>
                    </tr>
                  <tr class="maintextbold">
                    <td colspan="8"><?php echo "$prop_desc"?></td>
                    </tr>
  </table>
                <table width="500" border="0" align="center" cellpadding="1" cellspacing="1">
                  <tr>
                    <td width="242" height="100" align="center" valign="middle"><?php  echo '<img src="' . GW_UPLOADPATH . $image_main_name . '" alt="Front of House" width="70" height="55"/></p>';?></td>
                    <td width="242" height="100" align="center" valign="middle"><?php echo '<img src="' . GW_UPLOADPATH . $image_kitchen_name . '" alt="Kitchen" width="70" height="55"/></p>'; ?></td>
                    <td width="241" height="100" align="center" valign="middle"><?php echo '<img src="' . GW_UPLOADPATH . $image_bed_name . '" alt="Bedroom" width="70" height="55"/></p>'; ?></td>
                    <td width="241" align="center" valign="middle"><?php echo '<img src="' . GW_UPLOADPATH . $image_bath_name . '" alt="Bathroom" width="70" height="55"/></p>'; ?></td>
                    <td width="241" align="center" valign="middle"><?php echo '<img src="' . GW_UPLOADPATH . $image_living_name . '" alt="Living Room" width="70" height="55"/></p>'; ?></td>
                    <td width="241" align="center" valign="middle"><?php echo '<img src="' . GW_UPLOADPATH . $image_dining_name . '" alt="Dining Room" width="70" height="55"/></p>'; ?></td>
                    <td width="241" align="center" valign="middle"><?php echo '<img src="' . GW_UPLOADPATH . $image_garden_name . '" alt="Garden" width="70" height="55"/></p>'; ?></td>
                    </tr>
                </table>
                
             
                <?php

            // Clear the image data to clear the form
            $image_main = "";
			$image_kitchen = "";
			$image_bed = "";
			$image_bath = "";
			$image_living = "";
			$image_dining = "";
			$image_garden = "";
			
			
            mysqli_close($dbc);
			exit();
          }
      elseif (($image_main_check == 'error' ) || ($image_kitchen_check == 'error' ) || ($image_bed_check == 'error' ) || ($image_bath_check == 'error' ) || ($image_living_check == 'error' ) || ($image_dining_check == 'error' ) || ($image_garden_check == 'error' ))	   {
		  
	  echo '<p class="maintextactive">All the images must be a GIF, JPG, JPEG, or PNG image file no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size. Please return to the form and re-select your images.</p>';
      }
      elseif (($image_main_check == 'load error' )  || ($image_kitchen_check == 'load error' ) || ($image_bed_check == 'load error' ) || ($image_bath_check == 'load error' ) || ($image_living_check == 'load error' ) || ($image_dining_check == 'load error' ) || ($image_garden_check == 'load error' )) {
	 
	  echo '<p class="maintextactive">The files did not load successfully. Please try again</p>';      }
	 
	  else {
		  echo '<p class="maintextactive">Someting else has gone wrong</p>';	  
		  echo '<p class="maintextactive">Image Main Check-' . $image_main_check . ' </p>';	  
		  echo '<p class="maintextactive">Image Main Name-' . $image_main_name . ' </p>';	  
		  }

      // Try to delete the temporary screen shot image file
      @unlink($_FILES['image_main']['tmp_name']) && ($_FILES['image_kitchen']['tmp_name']) && ($_FILES['image_bed']['tmp_name']) && ($_FILES['image_bath']['tmp_name']) && ($_FILES['image_living']['tmp_name']) && ($_FILES['image_dining']['tmp_name']) && ($_FILES['image_garden']['tmp_name']);
 
     }
    else { echo '<p class="maintextbold">Please enter all of the information to add to your property.</p>';  }
	
	

?>
2
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by ebanbury
0

Actually here is a simpler script to look at...this is the same thing, but just using one field and one image, with all the rest of my workings stripped out...

The .jpg images just return to the screen as a black square - which I guess means it is working as far as the imagecreatetruecolour() function........
However if I try and upload a really large .jpg - then the whole thing just freezes and nothing returns to the screen at all....

<?php require_once('Connections/dreamsin_localhost.php'); 
      require_once('myaccess/appvars.php');

// Connect to the database
  $dbc = mysqli_connect("localhost", "xxxxxxxxx", "xxxxxxxxx", "xxxxxxxxx");

function createfilename($ID,$image_name)
{

	if (!empty($image_name)) 
	{
	$output = number_format($ID,0,".",",") . '_' . time() . '_' . mt_rand() .$image_name;
	return $output;
	}
	else
	{
	$output = "";
	}
}

function checkfile($image_name,$type,$size,$tmp_name,$error,$old_width,$old_height)
{
	
	$output ='start';

	if (!empty($image_name)) 
	   {
      if (
	  (($type == 'image/gif') || ($type == 'image/jpg') || ($type == 'image/pjpeg') || ($type == 'image/png') || ($type == 'image/jpeg'))
	  && (($size > 0) && ($size <= GW_MAXFILESIZE))
		  && ($error == '0'))
	  {
        // Move the files to the target upload folder
          $target = GW_UPLOADPATH . $image_name;
		  if (move_uploaded_file($tmp_name , $target) ) {$output = 'success';} else {$output = 'load error'; }
	  } 

	  elseif ($size > GW_MAXFILESIZE){ 

 		$target1 = GW_UPLOADPATH . 'original' . $image_name  ;
       	$target2 = GW_UPLOADPATH . $image_name  ;

// leave this line in if you want to keep original 
//     	if (move_uploaded_file($tmp_name , $target1) ) {$output = 'success';} else {$output = 'load error'; }

	    $reductionfactor =   sqrt ( $size / GW_MAXFILESIZE )  ;
		$new_height = number_format( $old_height / $reductionfactor ,0 , '.', '');
		$new_width = number_format( $old_width / $reductionfactor ,0 , '.', '');

	    $new_image = imagecreatetruecolor($new_width, $new_height);

      	$image_info = getimagesize($filename);
        if(( $type == 'image/jpg'  )|| ($type == 'image/pjpeg')   || ($type == 'image/jpeg')){
 			imagejpeg($new_image,$target2,85);  
			$old_image = imagecreatefromjpeg($tmp_name);
    		imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); }
        elseif( $type == 'image/gif'  ) {
			$old_image = imagecreatefromgif($tmp_name);
    		imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
 			imagegif($new_image,$target2);  }
        elseif( $type == 'image/png'  ) {
			$old_image = imagecreatefrompng($tmp_name);
    		imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
 			imagepng($new_image,$target2);  }

	  	$output='success'; 

	   }
	  else { $output='error'; }
	   }
	  else { $output='success'; }

	return $output;
}


  if (isset($_POST['submit'])) {
    // Grab the profile data from the POST
    
	$screenpath = "images/propertyimages/";
	$image_main_name = mysqli_real_escape_string($dbc, trim($_FILES['image_main']['name']));
	
	
	$image_main_name = createfilename($_SESSION[ID],$_FILES['image_main']['name']);
	list($old_width, $old_height, $type, $attr)= getimagesize($_FILES['image_main']['tmp_name']);
	$image_main_check = checkfile($image_main_name,$_FILES['image_main']['type'],$_FILES['image_main']['size'],$_FILES['image_main']['tmp_name'],$_FILES['image_main']['error'],$old_width,$old_height) ;

	$tmp_name = $_FILES['image_main']['tmp_name'];
	list($old_width, $old_height, $type, $attr)= getimagesize($_FILES['image_main']['tmp_name']);
	$image_type = $_FILES['image_main']['type'];
	$image_size = $_FILES['image_main']['size'];
    $reductionfactor =   sqrt ( $image_size / GW_MAXFILESIZE )  ;
	$new_height = number_format( $old_height / $reductionfactor ,0 , '.', '');
	$new_width = number_format( $old_width / $reductionfactor ,0 , '.', '');
	$max_size = GW_MAXFILESIZE;
	   
	  if ($image_main_check == 'success') 
		  {
	
   
// Connect to the database
  $dbc = mysqli_connect("localhost", "xxxxxxxx", "xxxxxxxxx", "xxxxxxxxxx");	

 $query = "INSERT INTO test_imagesize
VALUES (0, '$screenpath', '$image_main_name')";
                      
  mysqli_query($dbc, $query);
// Confirm success with the user
		echo '<p class="maintextbold">Thankyou kind sir for Listing your Property! Please view your listing below. </p>';
		echo 'image main ' . $tmp_name . '</br>';   	    
		echo 'image type ' . $image_type . '</br>';   	       	    
		echo 'image width ' . $image_width . '</br>';   	       	    
		echo 'image height ' . $image_height . '</br>';   	       	    
		echo 'new width ' . $new_width . '</br>';   	       	    
		echo 'new height ' . $new_height . '</br>';   	       	    
		echo 'reduction factor ' . $reductionfactor . '</br>';   	       	    
		echo 'image size ' . $image_size . '</br>';   	    
		echo 'maximum file size ' . $max_size . '</br>';   	       	    
		echo 'type ' . $type . '</br>';   	       	    
		echo 'attr ' . $attr . '</br>';   	       	    

		echo '<img src="' . GW_UPLOADPATH . $image_main_name . '" alt="Front of House" width="70" height="55"/></p>';
         ?>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>
                  <?php

            // Clear the image data to clear the form
            $image_main = "";
						
			
            mysqli_close($dbc);
			exit();
          }
      elseif ($image_main_check == 'error' ) 	   {
		  
	
		  
	  echo '<p class="maintextactive">All the images must be a GIF, JPG, JPEG, or PNG image file no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size. Please return to the form and re-select your images.</p>';
      }
      elseif ($image_main_check == 'load error' ) {
	 
	  echo '<p class="maintextactive">The files did not load successfully. Please try again</p>';      }
	 
	  else {echo '<p class="maintextactive">Someting else has gone wrong</p>';	  }

      // Try to delete the temporary screen shot image file
      @unlink($_FILES['image_main']['tmp_name']) ;
 
     }
    else { echo '<p class="maintextbold">Please enter all of the information to add to your property.</p>';  }
	
	

?>
                  
                  
                  
                </p>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <p>
    <label for="image_main"></label>
    <input name="image_main" type="file" id="image_main" size="50" maxlength="100" />
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>
</body>
</html>
0

Alright....lets fire this thread up with some real code...

resizeImage.php:

class SimpleImage {
 
   var $image;
   var $image_type;
 
   function load($filename) {
 
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
 
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
 
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
 
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
 
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
 
         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {
 
         imagepng($this->image,$filename);
      }
      if( $permissions != null) {
 
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
 
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
 
         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {
 
         imagepng($this->image);
      }
   }
   function getWidth() {
 
      return imagesx($this->image);
   }
   function getHeight() {
 
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
 
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
 
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
 
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }
 
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }      
 
}

How to use this the simple way?

//create thumb	
// the $name is the filename of the image you want resized.
// I get the $ext (extension of the image)
//if this image is a .bmp the code will not resize it, therefor i let the script skip its process if so. i also dont resize it if its already smaller than i want it resized to. 
$name = "iamanerd.jpg";
$loadPic = "/the/path/to/the/pic/$name";
$savePic = "/thumbs/".$name."";
if(!is_file($savePic))	{
$ext = end(explode('.', $name));
list($width, $height, $type, $attr) = getimagesize($loadPic);
if($ext !="bmp" && $width > "140" && $height > "117" ){
include('../classes/resizeImage.php');
$image = new SimpleImage();
$image->load($loadPic);
$image->resize(140,117);
$image->save($savePic);

}
0

Hi Many thanks for this! I only just saw this. It looks like your code is resizing the actual fileszie and scaling...which I do need.

What I actually now have working is the following function. It works fine but with 7 images and quite a long form with dynamic fields, if the user submits large images it really does take several minutes to do the work.

I will try your version also. Will it be quicker? If not my next step is to try and implement some kind of progress bar which only launches when the 'submit'button is clicked. Many thanks for the help!

function checkfile($image_name,$type,$size,$tmp_name,$error,$old_width,$old_height)
{
	
	$output ='start';

	if (!empty($image_name)) 
	   {
      if (
	  (($type == 'image/gif') || ($type == 'image/jpg') || ($type == 'image/pjpeg') || ($type == 'image/png') || ($type == 'image/jpeg'))
	  && (($size > 0) && ($size <= GW_MAXFILESIZE))
		  && ($error == '0'))
	  {
        // Move the files to the target upload folder
          $target = GW_UPLOADPATH . $image_name;
		  if (move_uploaded_file($tmp_name , $target) ) {$output = 'success';} else {$output = 'load error'; }
	  } 

	  elseif ($size > GW_MAXFILESIZE){ 

 		$target1 = GW_UPLOADPATH . 'original' . $image_name  ;
       	$target2 = GW_UPLOADPATH . $image_name  ;

// leave this line in if you want to keep original 
//     	if (move_uploaded_file($tmp_name , $target1) ) {$output = 'success';} else {$output = 'load error'; }

	    $reductionfactor =   sqrt ( $size / GW_MAXFILESIZE )  ;
		$new_height = number_format( $old_height / $reductionfactor ,0 , '.', '');
		$new_width = number_format( $old_width / $reductionfactor ,0 , '.', '');

	    $new_image = imagecreatetruecolor($new_width, $new_height);

      	$image_info = getimagesize($filename);
        if(( $type == 'image/jpg'  )|| ($type == 'image/pjpeg')   || ($type == 'image/jpeg')){
			$old_image = imagecreatefromjpeg($tmp_name);
    		imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
 			imagejpeg($new_image,$target2,85);  }
        elseif( $type == 'image/gif'  ) {
			$old_image = imagecreatefromgif($tmp_name);
    		imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
 			imagegif($new_image,$target2);  }
        elseif( $type == 'image/png'  ) {
			$old_image = imagecreatefrompng($tmp_name);
    		imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
 			imagepng($new_image,$target2);  }

	  	$output='success'; 

	   }
	  else { $output='error'; }
	   }
	  else { $output='success'; }

	return $output;
}
0

Hi
Thanks for this. The image resizing (i.e dimensions, scaling etc) is really good.

What I'm also needing to address is the actual filesize in kb. So whichever images have been selected by the user, they are resized to approx 32kb. This is the tricky bit...hence the:

$reductionfactor =   sqrt ( $size / GW_MAXFILESIZE )  ;		$new_height = number_format( $old_height / $reductionfactor ,0 , '.', '');		$new_width = number_format( $old_width / $reductionfactor ,0 , '.', '');

I have actually managed to get the reduction function to work, so that files coming in are reduced to anything between 20-45kb (which is acceptable)to be stored on the server. When they are displayed I then call up the code to fix width, height by scaling etc - this is called where the image is placed.

My really big problem is that if a user is trying to load 7 images - all around 2-4MB - then it is literally taking about 5 minutes to reduce the filesize....

I'm not sure how to get around this in PHP? Any ideas?

Many thanks for the help

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.