Solved

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

Posted on 2011-03-05
2
196 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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 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