Jump to content

Displaying images in a ratio


Mutley

Recommended Posts

Using your function I get:

 

Warning: Cannot modify header information - headers already sent by (output started at /includes/header.php:11) in /products.php on line 267

 

products.php:

<?php
require_once("includes/sql_connect.php");

if (isset($_POST['order'])) {?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Order Page</title>

<style>
.custom {
display: inline;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}

table {
border-right:#999999 1px solid;
border-bottom:#999999 1px solid;
}

td {
padding:3px;
border-top:#999999 1px solid;
border-left:#999999 1px solid;
}
</style>

</head>
<body>

<h1 class="custom">J.J.Metcalfe & Son</h1><br />
<h3 class="custom">Tel: 0044 (0)1609 771124 Fax: 0044 (0)1609 760635</h3><br />
<i><b>Printable</b> Online Fax/Document order form.</i><br /><br />
Please fill in your details:<br />
<table width="400" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Name:</td>
    <td>____________________</td>
  </tr>
  <tr>
    <td>Telephone:</td>
    <td>____________________</td>
  </tr>
  <tr>
    <td>Address:</td>
    <td>____________________<br />____________________</td>
  </tr>
  <tr>
    <td>Postcode:</td>
    <td>____________________</td>
  </tr>
  <tr>
    <td>E-Mail:</td>
    <td>____________________</td>
  </tr>
  <tr>
    <td>Order Notes:</td>
    <td>______________________________<br />
	______________________________<br />
	______________________________<br />
</td>
  </tr>
</table>
<br /><br />

<table width="600" border="0" cellspacing="0" cellpadding="0">
<td>JJM #:</td>
<td>Product #:</td>
<td>Category:</td>
<td>Name:</td>
<td>Description:</td>
<td>Weight:</td>
<?
if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass'])) {
$userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES);
$pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES);

$check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid'") or die(mysql_error());
  
if(mysql_num_rows($check) <> 1)	{
echo "";
} else {
/////////////// ### PRICE ###
?>
<td>Price:</td>
<?
////////////////////// End login ### PRICE ###
}
} else {
echo "";
}
?>
<td>Quantity:</td>
<?
foreach ($_POST['order'] as $id => $x) {

$sql = "SELECT id, category, name, cpn, pn, description, descriptionf, descriptiong, weight, price, ofeatured, onew, osale FROM products WHERE id = ".$id." LIMIT 1";
$result = mysql_query($sql) or die("SQL Query: {wordwrap($sql)}<br />Error because:  " . mysql_error());

if(mysql_num_rows($result)!=0) {

while(list($id, $category, $name, $cpn, $pn, $description, $descriptionf, $descriptiong, $weight, $price, $ofeatured, $onew, $osale) = mysql_fetch_row($result)) {
?>
<!-- START ORDER FORM -->

<tr>
<td><?=$cpn?> </td>
<td><?=$pn?> </td>
<td><?=$category?> </td>
<td><?=$name?> </td>
<td><?=$description?> </td>
<td>
<?=$weight?></td>
<?
if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass'])) {
$userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES);
$pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES);

$check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid'") or die(mysql_error());
  
if(mysql_num_rows($check) <> 1)	{
echo "";
} else {
/////////////// ### PRICE ###
?>
<td>€<?=$price?></td>
<?
////////////////////// End login ### PRICE ###
}
} else {
echo "";
}
?>
<td>______</td>
</tr>

<!-- END ORDER FORM -->
<?
}
}
// Split closing between results and selected
}
?>
</table>
<br />
<form><input type="button" value="Click here to Print Page"
onclick="window.print();return false;" /></form>
...or click <a href="index.php">here</a> to cancel and visit home page.
</body>
</html>
<?
} else {
////////////////////////////////////////// ^ORDER PAGE^

include("includes/header.php");

$sql = "SELECT id, category, name, cpn, pn, description, descriptionf, descriptiong, weight, price, ofeatured, onew, osale FROM products WHERE category='".$c."' ORDER BY pn DESC";
$result = mysql_query($sql) or die("SQL Query: {wordwrap($sql)}<br />Error because:  " . mysql_error());

if(mysql_num_rows($result)!=0) {
// Start table code^

$c = $_GET['c'];

if($_GET['listview'] == 1) {
?> 
<a href="products.php?c=<?=$c?>">Thumbnail View</a><br /><br />
<? } else { ?>
<a href="products.php?c=<?=$c?>&listview=1">List View</a><br /><br />
<? } ?>

<form enctype="multipart/form-data" action="products.php" method="post" />
<table align="center" class="productlist" <? if(empty($_GET['listview'])) {?>width="730"<? }else{?>width="650"<? }?> cellpadding="0" cellspacing="0">
  <tr>
<?
if(empty($_GET['listview'])) { 
?>
    <td width="80" class="tableheader"> </td>
<?
} else {
}
?>
    <td width="60" class="tableheader">Manufacturers Reference:</td>
    <td width="60" class="tableheader">JJM Part No.</td>
    <!--<td width="180" class="tableheader">Product:</td>-->
    <td width="250" class="tableheader">Description:</td>
    <td width="50" class="tableheader">Weight:</td>
<?
//////////////////// ### PRICE START ###
if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass'])) {
$userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES);
$pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES);

$check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid'") or die(mysql_error());
  
if(mysql_num_rows($check) <> 1)	{
echo "";
} else {
////////////////////// Login above ### PRICE ##
?>
    <td width="50" class="tableheader">Price:</td>
<?
////////////////////// End login ### PRICE ###
}
} else {
echo "";
}
////////////////////// ### PRICE END ###
?>


    <td colspan="2" class="tableheader"> </td>

<?
if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass'])) {
$userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES);
$pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES);

$check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid' AND id = '$staffid'") or die(mysql_error());
  
if(mysql_num_rows($check) <> 1)	{
echo "";
} else {
// Login above
?>
<td colspan="2" class="tableheader"> </td>
<?
// End login
}
} else {
echo "";
}
?>
  </tr>

<?php

function resizeimage($filename){ //here was my mistake
	if(!file_exists($filename)) return false;
	$im_info	= getimagesize($filename); //unresized image
	$im_width	= $im_info[0];
	$im_height	= $im_info[1];
	$im_flag	= $im_info[2];

	if ($im_flag == 1) $image = imagecreatefromgif($filename); //unresized image
	elseif ($im_flag == 2) $image = imagecreatefromjpeg($filename);
	elseif ($im_flag == 3) $image = imagecreatefrompng($filename);
	else return false;

	//max height: 100px; max width: 100px
	if($im_width >= $im_height) {
		$im_divice	= $im_width / 100;
	}else {
		$im_divice	= $im_height / 100;
	}
	$thumb_width	= $im_width / $im_divice;
	$thumb_height	= $im_height / $im_divice;

	//create empty image
	$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
	//resize image
	imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

	header('Content-Type: image/jpeg', true);
	imagejpeg($thumb, NULL, 100); //thumbnail with the best quality

	imagedestroy($thumb); //destroy temporary image-resource
	imagedestroy($image); //destroy temporary image-resource
}

// Function to shorten description:
function displayText($text)
{
   $addon = "...<a class=\"more\" href=\"viewproduct.php?id=$id\">[Read More]</a>";
   if(isset($_GET['listview'])) {
$cutoff = '40';
} else {
$cutoff = '85';
}
   $text1 = substr($text, 0, $cutoff);
   if($text <> $text1){
   $returntext = "$text1$addon";}else{$returntext = $text;}
   return $returntext;
}

while(list($id, $category, $name, $cpn, $pn, $description, $descriptionf, $descriptiong, $weight, $price, $ofeatured, $onew, $osale) = mysql_fetch_row($result)) {
// Attach variables from database^
?>

  <tr <? if($ofeatured == '1') {?>style="background-color:#ffffc0;"<? } ?>>
<?
if(empty($_GET['listview'])) { 
// List view^
?>
    <td>
<!-- Previous method -->
<a href="viewproduct.php?id=<?=$id?>"><img height="80" width="80" src="images/products/<?=$id?>.jpg" alt="<?=$cpn?>" /></a>
<!-- New method -->
<?
resizeimage("./images/products/". $id .".jpg");
?>
</td>
<?
} else {
}
?>
    <td class="pn" align="center"><?=nl2br($pn)?> </td>
    <td class="pn" align="center"><?=nl2br($cpn)?> </td>
    <!--<td>-->
<?
//if($onew == '1') {
//echo "<img src=\"images/icon_new.gif\" alt=\"New Product!\" />";
//} else {
//echo " ";
// If product is labeld new, show image^
//}
?>
<?=//nl2br($name)?>
<!--</td>-->
    <td><? $description_cut = displayText($description); echo $description_cut;?> </td>
    <td align="center">
<?=nl2br($weight);
if($weight == NULL) {
echo "";
} else {
echo " Kg";
}
?>
</td>
<?
if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass'])) {
$userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES);
$pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES);

$check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid'") or die(mysql_error());
  
if(mysql_num_rows($check) <> 1) {
echo "";
} else {
////////////////////// Login above ### PRICE ##
?>
    <td class="price" align="center">€<?=nl2br($price)?></td>
<?
////////////////////// End login ### PRICE ###
}
} else {
echo "";
}
?>

    <td colspan="2" align="center"><a href="viewproduct.php?id=<?=$id?>"><img src="images/icon_information.gif" alt="More Information" border="0" /></a><br /><input name="order[<?=$id?>]" type="checkbox" value="1" /></td>
<?
if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass']))
{
$userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES);
$pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES);

$check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid' AND id = '$staffid'") or die(mysql_error());
  
if(mysql_num_rows($check) <> 1)
{
echo "";
}
else
{
// Login above
////////////////////// BELOW ARE STAFF EDIT/DELETE BUTTONS
?>
<td><a href="viewproduct.php?id=<?=$id?>&edit=<?=$id?>"><img src="images/icon_edit.gif" border="0" alt="Edit Product" /></a></td>
<td><a href="viewproduct.php?id=<?=$id?>&delete=<?=$id?>"><img src="images/icon_delete.gif" border="0" alt="Delete Product" /></a></td>
<?
// End login
}
}
else
{
echo "";
}
?>
</tr>
<?
}
?>

<tr><td <? if(empty($_GET['listview'])) {?>colspan="11"<? } else {?>colspan="10"<? }?> align="right">
Using the tick-boxs you can produce an order form: <input type="submit" name="submit" value="Fax Order Form" /> <input type="submit" name="submit" value="E-mail Order" />
</form>
</td></tr>	
</table>
<br /><br />

<?
} else {
echo "No Products.";
}
// If no products in database^

include("includes/footer.php");

}
// End order else statement^
?>

 

 

header.php

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>J.J Metcalfe & Son</title>
</head>

<body style="margin:0">

<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td class="header" height="100" colspan="3"><center><h1>J.J.Metcalfe & Son</h1></center></td>
  </tr>
  <tr>
    <td class="infobar" colspan="3"><center>Tel: 0044 (0)1609 771124 Fax: 0044 (0)1609 760635</center></td>
  </tr>
  <tr>
    <td class="searchbar" colspan="3"><center><? include('search.php'); ?></center></td>
  </tr>
  
<?php
include('includes/sql_connect.php');

// Staff ID variable for all staff permissions:
$staffid = "1";

if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass']))
{
$id = htmlspecialchars($_COOKIE['id'], ENT_QUOTES);
$pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES);

$check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$id' AND id = '$staffid'") or die(mysql_error());
  
if(mysql_num_rows($check) <> 1)
{
echo "";
}
else
{
// Login above
?>
  <tr style="background-color:#FFFFFF">
  	<td style="border-bottom:#000000 1px solid;" colspan="3">
Admin Menu: 
<a href="admin_addproduct.php">Add product</a> | 
<a href="admin_addcategory.php">Add category</a> | 
<a href="admin_menu.php">Edit Menu</a> | 
<a href="logout.php">Logout</a>
</td>
</tr>
<?php
// End login
}
}
else
{
echo "";
}

?>

  <tr>
    <td class="menu" width="15%" valign="top">

<?php
$result = mysql_query("SELECT menu FROM menu");
while($row = mysql_fetch_array( $result ))
$menu = $row['menu'];
    {
echo "$menu";
}
?>

<br /><br />
<table align="center" class="productlist" width="150" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td class="tableheader">Featured:</td>
</tr>
<tr>
<td>
<marquee scrollamount="3" direction="up">
<table class="productlist" width="100%" border="0" cellspacing="0" cellpadding="3">

<?	
$sql = "SELECT id, category, name, cpn, pn, description, descriptionf, descriptiong, weight, price, ofeatured, onew, osale FROM products WHERE ofeatured='1'";
$result = mysql_query($sql) or die("SQL Query: {wordwrap($sql)}<br />Error because:  " . mysql_error());

if(mysql_num_rows($result)!=0) {

while(list($id, $category, $name, $cpn, $pn, $description, $descriptionf, $descriptiong, $weight, $price, $ofeatured, $onew, $osale) = mysql_fetch_row($result)) {
?>

<tr>
<td>
<center>
<a href="viewproduct.php?id=<?=$id?>"><img height="80" width="80" src="images/products/<?=$id?>.jpg" alt="<?=$id?>" /></a><br />
<font class="pn"><?=$cpn?>/<font class="pn"><?=$pn?><br />
</center>
</td>
</tr>

<?
}
} else {
echo "No Featured Products.";
}
echo "</table>";
?>
</marquee>
</td>
  </tr>
</table>
<br /><br />

</td>
    <td class="content" valign="top" colspan="2">
<!-- Content -->

Link to comment
Share on other sites

You cannot use that function when you're writing plain code.

 

Copy that function into a new file and save it as picture.php:

<?php
function resizeimage($filename){ //here was my mistake
	if(!file_exists($filename)) return false;
	$im_info	= getimagesize($filename); //unresized image
	$im_width	= $im_info[0];
	$im_height	= $im_info[1];
	$im_flag	= $im_info[2];

	if ($im_flag == 1) $image = imagecreatefromgif($filename); //unresized image
	elseif ($im_flag == 2) $image = imagecreatefromjpeg($filename);
	elseif ($im_flag == 3) $image = imagecreatefrompng($filename);
	else return false;

	//max height: 100px; max width: 100px
	if($im_width >= $im_height) {
		$im_divice	= $im_width / 100;
	}else {
		$im_divice	= $im_height / 100;
	}
	$thumb_width	= $im_width / $im_divice;
	$thumb_height	= $im_height / $im_divice;

	//create empty image
	$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
	//resize image
	imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

	header('Content-Type: image/jpeg', true);
	imagejpeg($thumb, NULL, 100); //thumbnail with the best quality

	imagedestroy($thumb); //destroy temporary image-resource
	imagedestroy($image); //destroy temporary image-resource
}
$id = '44';

//no echos please
//echo "<img src='./images/products/". $id. ".jpg'>";

//also you don't use that 
//resizeimage(".images/products/". $id ."");
//the filename was wrong... it has to be ./...
$id = isset($_GET['id']) ? $_GET['id']:'1'; // replace 1 as you default id.
resizeimage("./images/products/". $id .".jpg");
?>

 

Then make your HTML-File and call a picture like

<img src="picture.php?id=myid" />

Link to comment
Share on other sites

excuse me if I missed the point or something, but as I understood from your post, you want to display a small version of an image, 80 pixels wide (or high) with the other dimension corresponding to the correct ratio of the original image.

 

as far as my experience goes, if you define only the width or the height of an image in html or css, the browser will resize the image to the correct ratio...

 

again, sorry if I missed something here....

 

arumdevil

Link to comment
Share on other sites

Replace that lines:

$id = isset($_GET['id']) ? $_GET['id']:'1'; // replace 1 as you default id.
resizeimage("./images/products/". $id .".jpg");

 

to

 

$id = isset($_GET['id']) ? $_GET['id']:'1'; // replace 1 as you default id.
if(!file_exists('./images/products/'.$id.'.jpg') $id = 'default'; //set the default id here
resizeimage("./images/products/". $id .".jpg");

Link to comment
Share on other sites

Tried this:

 

$id = isset($_GET['id']) ? $_GET['id']:'1'; // replace 1 as you default id.
if(file_exists('./images/products/'.$id.'.jpg') {
$id = $_GET['id']; //set the default id here
} else {
$id = 'noimage';
}
resizeimage("./images/products/". $id .".jpg");

 

Still doesn't work, no idea why?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.