Forum Moderators: coopster

Message Too Old, No Replies

resizing an image.

         

lazarus

12:48 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



i am trying to resize an image that i am calling from a specific folder, but when i test the page, it doesnt show up any picture at all, any help?

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>

Birdman

1:39 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

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

lazarus

2:16 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



cheers birdman, i used the code

<td width="145" valign="top" class="asmc"><img src=images/<? echo nl2br($row->pic);?> width=<? echo $imagewidth;?> height=<? echo $imageheight;?> ></td>

and it did the trick.

with your code, it couldnt find my pics unfortunatly.

lazarus

3:30 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



got a new one :) i'd be most grateful for any help.

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);

Birdman

3:46 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try defining your image address before using getimagesize().

$img = "/images/$pic";
$imagehw = GetImageSize($img);

Or you may need the FULL path that your host gives you:

/www/user/html/images/ or similar

lazarus

3:52 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



tried those, still get

Warning: getimagesize: Unable to open 'images/' for reading.

i know its friday the 13th, but it shouldnt be having this much of a fit! :)

Birdman

3:59 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, if that's your error, then the variable $pic is empty. You need to figure out why the variable is empty. Is it passed from another page or a form?

You may need to do:

$_GET['pic']

or

$_POST['pic']

lazarus

4:11 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



$pic is the filename i am pulling from the db.

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.

Birdman

4:20 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>the problem is that it wont read the pic from the database

That most likely explains your problem. Check your SQL query to make sure it is correct.

If you want, post your PHP database code and maybe we'll see what's going on.

lazarus

4:24 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



i would agree if not for the fact without the resizing code, i can still pull image filename, and use it to place the correct images in the right place. its just that getimagesize line im having probs with. anyway here is the entire php code.

// 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);
?>

Birdman

4:31 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try taking the whole block of resizing code and move it into the while loop.

while($row = mysql_fetch_object($result))
{
$maxwidth = 75;
$maxheight = 50;
$img = "/images/$pic";
...
...

jatar_k

4:32 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$img = "/images/$pic";
$imagehw = GetImageSize($img);

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.

lazarus

4:39 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



i'll sit and have a play. thanks for all your help guys.

lazarus

4:52 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



just got it thanx to birdman.

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!