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