Can someone please help me connect the bottom script with the top script? The results should be that each image that is randomly displayed will be resized proportionaly.
Code:
<?
// This is the random product script
// Connect to the database
mysql_connect ('host', 'user', 'pass') ;
mysql_select_db ('dbname');
// Edit this number to however many links you want displaying
$num_displayed = 1 ;
// Select random rows from the database
$result = mysql_query ("SELECT * FROM products ORDER BY RAND() LIMIT $num_displayed");
// For all the rows that you selected
while ($row = mysql_fetch_array($result))
{
// Display them to the screen...
echo "<a href=\"http://www.brothersvarietystore.com/shop/index.php?p=product&id=" . $row["pid"] ."&parent=" . $row["cid"] . "\">
<img src=\"" . $row["image_url"] . "\" border=1 alt=\"" . $row["title"] . "\">
</a>" ;
}
?>
Code:
// The script below is a GD image sizing script (by proportions) that I am trying to use in conjunction with the script above.
<?php
/*
You call it like so:
resizeImage ($src, $dest, $newHeight, $newWidth)
$src = .
$dest =
$newHeight =
$newWeight =
*/
function resizeImage ($src, $dest, $newHeight, $newWidth)
{
if ( $dest )
{
fopen($dest, "w") ||
die("Can not write to $dest. Check directory permissions!");
}
$imgInfo = getimagesize($src); $width = $imgInfo[0]; $height = $imgInfo[1];
$file_type = $imgInfo[2];
if ( $newHeight || $newWidth )
{
if ( $newHeight && $newWidth )
{
$width = $newWidth;
$height = $newHeight;
} else {
if ( $newWidth == "0" )
{
$ratio = (intval(($width / $newWidth) * 100)) / 100;
$width = (intval(($width / $ratio) * 100)) / 100;
$height = (intval(($height / $ratio) * 100)) / 100;
} else {
$ratio = (intval(($width / $newHeight) * 100)) / 100;
$width = (intval(($width / $ratio) * 100)) / 100;
$height = (intval(($height / $ratio) * 100)) / 100;
}
}
}
$destimg=ImageCreateTrueColor($width,$height);
switch ($file_type)
{
case 1:
$srcimg = imagecreatefromgif($src);
if (function_exists(ImageGIF))
{
$imgType = "gif";
} else {
$imgType = "jpeg";
}
break;
case 2:
$srcimg = imagecreatefromjpeg($src);
$imgType = "jpeg";
break;
case 3:
$srcimg = imagecreatefrompng($src);
$imgType = "png";
break;
default:
break;
}
ImageCopyResized($destimg,$srcimg,0,0,0,0,$width,$height,ImageSX($srcimg),ImageSY($srcimg));
if ( !$dest ) { header ("Content-type: image/$imgType"); }
switch ($file_type)
{
case 1:
if (function_exists(ImageGIF))
{
if ( $dest) { imagegif ($destimg, $dest); } else { imagegif($destimg); }
} else {
if ( $dest) { imagejpeg ($destimg, $dest); } else { imagejpeg($destimg); }
}
break;
case 2:
if ( $dest) { imagejpeg ($destimg, $dest); } else { imagejpeg($destimg); }
break;
case 3:
if ( $dest) { imagepng ($destimg, $dest); } else { imagepng($destimg); }
break;
default:
break;
}
imagedestroy ($srcimg);
imagedestroy ($destimg);
}
?>