Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-05
2
Medium Priority
?
207 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
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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

782 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