Forum Moderators: coopster
Resize Code:
$maxwidth = 150;
$maxheight = 100;
$imagehw = GetImageSize(images/$pic);
$imagewidth = $imagehw[0];
$imageheight = $imagehw[1];
if ($imagewidth >= $imageheight)
{
if($imagewidth > $maxwidth)
{
$imageprop = ($maxwidth*100)/$imagewidth;
$imagevsize = ($imageheight*$imageprop)/100;
$imagewidth=$maxwidth;
$imageheight=ceil($imagevsize);
}
}
else
{
if($imageheight > $maxheight)
{
$imageprop = ($maxheight*100)/$imageheight;
$imagehsize = ($imagewidth*$imageprop)/100;
$imageheight = $maxheight;
$imagewidth = ceil($imagehsize);
}
}
code calling resize information:
<td width="145" valign="top" class="asmc"><img src=images/<? echo nl2br($row->pic);?> width=<? $imagewidth;?> height=<? $imagewidth;?>></td>
When using the shorthand method to echo a $var, it should be like this:
<?=$imagewidth?>
Also, you have both the width and height set with $imagewidth.
width=<? $imagewidth;?> height=<? $imagewidth;?>
Also, I'm not sure about how you call the image file with $row->pic, try this code and see what happens:
<td width="145" valign="top" class="asmc"><img src=images/<?=$pic?> width=<?=$imagewidth?> height=<?=$imageheight?>></td>
Birdman
in my last lot of code i have
$maxwidth = 150;
$maxheight = 100;
$imagehw = GetImageSize(images/$pic);
$imagewidth = $imagehw[0];
$imageheight = $imagehw[1];
but i cant get the getimagesize to recognise the address, ive tried putting "" and '' round both the images, and the $pic and it still cant read them. it would only work if i place a definate address such as
$imagehw = GetImageSize(images/pic.jpg);
basically the page shows the five most recent post with the first picture from the post. i want to scale the picture to keep its aspect ratio depending on the size. if ie if it is taller than it is long it keeps that ratio by shrinking it to a predetermined size and scaling the width appropriatly.
however i need to do this for each pic as they will be different sizes.
the problem is that it wont read the pic from the database and place it behind images/. if it did, it would then find the appropriate image for each post, and scale them accordingly.
// includes
include("../conf.php");
include("../functions.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$query = "SELECT id, title, taster, pic, timestamp FROM news ORDER BY timestamp DESC LIMIT 0, 5";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$maxwidth = 75;
$maxheight = 50;
$img = "/images/$pic";
$imagehw = GetImageSize($img);
$imagewidth = $imagehw[0];
$imageheight = $imagehw[1];
if ($imagewidth >= $imageheight)
{
if ($imagewidth > $maxwidth)
{
$imageprop = ($maxwidth*100)/$imagewidth;
$imagevsize = ($imageheight*$imageprop)/100;
$imagewidth=$maxwidth;
$imageheight=ceil($imagevsize);
}
}
else if($imageheight > $maxheight)
{
$imageprop = ($maxheight*100)/$imageheight;
$imagehsize = ($imagewidth*$imageprop)/100;
$imageheight = $maxheight;
$imagewidth = ceil($imagehsize);
}
// if records present
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
while($row = mysql_fetch_object($result))
{
?>
all my html code gubbins goes here.
<?
}
}
// if no records present
// display message
else
{
?>
<font size="-1">No press releases currently available</font> <font face="Arial, Helvetica, sans-serif">
<?
}
// close database connection
mysql_close($connection);
?>
I think the problem is that when you specify a root relative path here it isn't root relative to the filesystem but to your url.
You could put the whole http://www.site.com/images or /home/user/mysite/images.
from php.net
Example 1. getimagesize (file)<?php
$size = getimagesize ("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" {$size[3]}>";
?>Example 2. getimagesize (URL)
<?php $size = getimagesize ("http://www.example.com/gifs/logo.gif");?>
I think the path is no good.
corrected code is
// if records present
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
while($row = mysql_fetch_object($result))
{
$maxwidth = 75;
$maxheight = 50;
$img = "images/$row->pic";
$imagehw = GetImageSize($img);
$imagewidth = $imagehw[0];
$imageheight = $imagehw[1];
etc etc.
you were right, just put the code to high up. moved it down which meant the addy could be changed to "images/$row->pic";
owe you a drink. cheers!