user701510
user701510

Reputation: 5763

restricting size of image that's retrieved from SQL table

I'm making a rotating banner system but there is a problem of inconsistent image size display. I am storing the images in a SQL table. I know its bad practice to store images in SQL tables but I am only storing a few files. I need to fix the image display size to be, say, 150 px by 150 px. How do I do this with php? Thanks in advance

Here is the code I have so far:

<?php
require ("connect.php");

$id = mysql_real_escape_string($_REQUEST['id']);
$query = mysql_query("SELECT * FROM banner WHERE id= $id ");
$row = mysql_fetch_array($query);
$content = $row['image'];
header('Content-type: image/jpg');
echo $content;
?>

<html>
    <head>
        <style type="text/css">
            a
            {
                text-decoration:none;
            }
        </style>
    </head>
    <body>

    <?php
require ("connect.php");

$query = mysql_query("SELECT * FROM banner");
$number = mysql_num_rows($query);
$result = mysql_query("SELECT * FROM banner ORDER BY RAND() LIMIT 1 ");

while ($row = mysql_fetch_assoc($result))
{
    echo '<a href = ' . $row[url] . '> <img src= "get.php?id= ' . $row[id] . ' "/> </a>';
    echo "<br />";
    echo '<a href = ' . $row[url] . '> ' . $row[description] . ' </a>';
}

echo mysql_error();
?>

    </body>
</html>

Upvotes: 0

Views: 2315

Answers (2)

John Green
John Green

Reputation: 13435

You can scale your image using an external library call gd.

Although I've never used it, there is a something called imagecreatefromstring(); (I HAVE used the rest of this code, btw.... I would just never store an image in a DB).

$size = 150;
// This is a reference to your MySQL row.
$img = imagecreatefromstring($row['image_data']);
$width = imagesx($img);
$height = imagesy($img);

$aspect = $height/$width;

// This is a simple width check limiter.  There are dozens of ways to treat this,
// so code away until you get what you want.  Personally, I have a 3K line class to handle
// all of the different ways I may want to change this scaling around.
if ($width < $size) 
{
   $w = $width;
   $h = $height;
}
else 
{
   $w = $size;
   $h = floor($w*aspect);
}

$buffer = imagecreatetruecolor($w,$h);

// There are some more advanced ways of doing this if you need an alpha channel.
imagecopyresized($buffer,$img,0,0,0,0,$w,$h,$width,$height);

// File is now copied and ready to go.  You should save your file with a unique, but 
// reconstructable name.  For instance, if your parameters to get here was 
// 'image.php?image_id=25', I'd actually md5('image.php?image_id=25') and cache it.  In the
// future, you could check your cache to see if the image was there before you even make your
// DB call, which would speed things up greatly.

// I show an example in the code.

//    $filename = '/var/www/public/'.md5('image.php?'.$_SERVER['query_string'].'_'.$size);
$filename = 'test.jpg';
imagejpeg($buffer, $filename, 85 );

header('Content-Type: image/jpeg');
echo file_get_contents($filename);

imagedestroy($buffer);
imagedestroy($img);

Upvotes: 1

martynthewolf
martynthewolf

Reputation: 1708

Well if you have control over the images and they only ever need to be 150px x 150px I would recommend that you just change the size of them manually and upload them back to your db.

The way you will have to do it now means you'll resize images on the fly everytime the page is loaded which is bad. To get that to work all you do is pass width and height parameters to the img tag like so

<img src="yourimage" width="150" height="150" />

Upvotes: 1

Related Questions