Solved

This code works, but I want to know why...

Posted on 2011-03-05
2
197 Views
Last Modified: 2012-05-11
Head out to http://countryshowdown.com/Texaco/photo_go.php to see what I've done.

I built this script that resizes all of the images in my database, then inserts those that match a specific adjusted width value into a temporary table so that I can then display them in a table that has only four cells across.

I'm stoked because after several hours of working on it, I've gotten what I want. Thing is, my script probes of an adjusted width value of 75 and the results I get all of have a width value of 67.

Like I say, the results are precisely what I want, but because I like knowing why something works and not just that it works, I was hoping I could get a ninja to look at it and tell me why what I've written is working.

Code is attached.

Thanks!
<?php
//here's my resizing function

function bruceResize($b_width, $b_height, $b_target) {
//takes the larger size of the width and height and applies the formula. Your function is designed to work with any image in any size.
if ($b_width > $b_height) {
$b_percentage = ($b_target / $b_width);
} else {
$b_percentage = ($b_target / $b_height);
}

//gets the new value and applies the percentage, then rounds the value

$b_width = round($b_width * $b_percentage);
$b_height = round($b_height * $b_percentage);
//returns the new sizes in html image tag format...this is so you can plug this function inside an image tag so that it will set the image to the correct size, without putting a whole script into the tag.
//return "width=\"$width\" height=\"$height\"";
echo $b_height.'<BR>';
return $b_height;
}

//declare your variables

function imgResize($width, $height, $target) {
//takes the larger size of the width and height and applies the formula. Your function is designed to work with any image in any size.
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...this is so you can plug this function inside an image tag so that it will set the image to the correct size, without putting a whole script into the tag.
//echo "width=\"$width\" height=\"$height\"";
return "width=\"$width\" height=\"$height\"";
}



?>
<html>
<head>
<title>Gallery</title>
</head>
<body>

<?php

$maxcols = 4; 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");

//delete all photos in photos_temp table so we don't duplicate pictures

$michelle = "delete from photos_temp";
$michelle_query = mysqli_query($cxn, $michelle);

$vivian = "select * from photos order by photo_date";
$vivian_query = mysqli_query($cxn, $vivian)
or die("Couldn't execute guery.");
while($vivian_row = mysqli_fetch_assoc($vivian_query))
{
extract($vivian_row);
	$the_photo_url = str_replace("+", " ", $url);
	$temp_url = $url;
	$photo_id = $id;
	$mypicture = getimagesize("../Photos/$the_photo_url");
	$killer_height = bruceResize($mypicture[0],  $mypicture[1], 100);
	if($killer_height==75){
	$insert = "insert into photos_temp (photo_id, url)
	values ('$photo_id', '$temp_url')";
	$insertexe = mysqli_query($cxn, $insert);
	if(!$insertexe) {
	$error = mysqli_errno($cxn).': '.mysqli_error($cxn);
	die($error);
	}
	}
}

$query = "select * from photos_temp";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");

$count=0; // initialize count

while ($row=mysqli_fetch_assoc($result))
{
$count++; // increment count

if ($count == 1)
 { // initalize table
?>

	<table width="585" border="0" align="center" cellspacing="1" cellpadding="1">
	<tr>

<?php
 }
?>

	<td align="center">
	<?php
	$photo_url = str_replace("+", " ", $row['url']);
	?>

	<?php
	
	?>
	<A HREF="../Photos/<?php echo $photo_url; ?>" target="_blank"><img src="../Photos/<?php echo $photo_url; ?>" <?php echo imgResize($mypicture[0],  $mypicture[1], 100); ?> 
	border="0" alt="<?php echo $row['description']; ?>"><BR><?php echo $killer_height; ?> 
	</a>
	</td>

<?php
if ($count % $maxcols == 0)
 { // if modulus of count is = 0 then end row
echo "</tr><tr>"; 
 }

}

if ($count)
{ // data exists
 $fill = ($count % $maxcols); // current column
if ($fill){ // if not last column already fill in blank columns
for ($i = $fill; $i <= ($maxcols -1); $i++){
echo "<td>&nbsp;</td>";
}
echo "</tr>";
 }
 echo "</table>";
}
?>
</td>
</tr>
</table>

Open in new window

0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35044332
Is there any specific about that you want to know ?
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35044844
I have not delved into your code but I'm afraid it is not working as you intend. The images are shown the size they are because you have a width=95 on the table cells. Try removing that and see what happens. The browser is simply making the images fit the cell, but they are as big as they were originally. The one 'Montana.jpg' is over 2MB in size and measures 3264x2448 pixels in size.

Resizing pictures for thumbnails is not difficult and the usual way is to create a thumbnails folder and put your resized images in there and then you can load them for display. I did a similar question some time ago which has working code for this. Scroll down to the solution for this question

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24935011.html

0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question