Jump to content

[SOLVED] Problem on image resize


MP145

Recommended Posts

I am trying to display an image with a new height and width, but it's not working. Below is the code. ". $info['thumb'] ." will return the filename.jpg and the actual image is stored in "images/news"

 

<?php
mysql_connect("localhost", "xxx", "yyyy") or die(mysql_error()) ;
mysql_select_db("news") or die(mysql_error()) ;

//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM newsitem") or die(mysql_error());

//Puts it into an array
while($info = mysql_fetch_array( $data ))
{

$size = getimagesize("images/news/". $info['thumb'] ."");
$height = $size[1];
$width = $size[0];
     if ($height > $width)
          {
               $height = 100;
               $percent = ($size[1] / $height);
               $width = ($size[0] / $percent);
          }
     else if ($width > $height)
          {
               $width = 100;
               $percent = ($size[0] / $width);
               $height = ($size[1] / $percent);
          }


echo "<b><font face=\"Verdana\" size=\"2\" color=\"#000000\">News Title:</b> " . $info['title'] . "</font><br>";
echo "<b><font face=\"Verdana\" size=\"2\" color=\"#000000\">Posted By:</b> " .$info['addby'] . "</font><br>";
echo "<b><font face=\"Verdana\" size=\"2\" color=\"#000000\">Date Posted:</b> " .$info['date'] . "</font><br><br>";
echo "<b><font face=\"Verdana\" size=\"1\" color=\"#000000\"><a href=\"editnews.php?ID=" .$info['newsID']. "\">Edit News</a> | <a href=\"deletenews.php?ID=" .$info['newsID']. "\">Delete News</a></b></font><br>";
echo "<br><img src\"images/news/" . $info['thumb'] . "\" height=\"$height\" width=\"$width\" />";
echo "<hr>";

}
?>

 

I can't see anything when the page is called. Please help.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/129648-solved-problem-on-image-resize/
Share on other sites

Thanks for the reply Maq

 

It's working now. Also i missed = on the img src

 

echo "<br><img src\"images/news/" . $info['thumb'] . "\" height=\"$height\" width=\"$width\" />";

 

should have been

 

echo "<br><img src=\"images/news/" . $info['thumb'] . "\" height=\"$height\" width=\"$width\" />";

 

but now, images that are same in width and height is not resizing. How can i resize the image so that the height and width is 80 ? regardless the original size.

 

Thanks guys. I just changed it to :-

 

<?php
$maxheight = 80;
$maxwidth = 80;

     if ($height > $maxheight)
          {
               $height = 80;

          }
     if ($width > $maxwidth)
          {
               $width = 80;

          }
?>

 

;D

You should use:

<?php
$maxheight = 80;
$maxwidth = 80;

     if ($height > $maxheight)
          {
               $height = $maxheight;

          }
     if ($width > $maxwidth)
          {
               $width = $maxwidth;

          }
?>

 

Incase you change it to something other than 80.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.