i have images that are 90% A4 size and some that are post card size. when i click on the thumbnails i create i display the full sized image but A4 is massive to display so i need to shrink it down to fit on the screen.
i create the thumbnails from the original image in the first place so i thought i could use the same code to just shrink the oringal image to a size thats suitable. (the code just grabs the image from the folder and just shrinks it down)
i know i need to use it as my source as i have used it in my thumbnail creation but dont understand the code to use.
what i think i need is how to use the imageResize file as my img src like the one for my thumbnails. but when i try doing something similair it doesnt display the image.
could you please explain where i might be going wrong?
sorry i missunderstood what you wanted ill give you both: here is the thumbnails page:
<?php
/* set up the database connection because real_escape_string() relies on an open connection to work */
$dbLink = new mysqli('localhost', 'root', '', 'gallery');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
/* check that $_GET['gallery'] has a value and the value is numeric - ie someone hasn't changed the url to just gallery.php or tried to be clever & entered gallery.php?gallery=bob */
if(isset($_GET['gallery']) && is_numeric($_GET['gallery'])) {
/* $_GET['gallery'] passes our test, so perform security functions on it & assign it to the $gallery variable */
$gallery = $dbLink->real_escape_string($_GET['gallery']);
} else {
/* it doesn't exist or it isn't numeric so assign a default value so our query below always gets constructed properly & safely */
$gallery = 1;
}
/* construct the query */
$query = "SELECT id, name, size, image_path FROM images WHERE gallery_type_id = $gallery";
/* run the query */
$result = $dbLink->query($query);
/* loop through the results & display the images */
while ($row = $result->fetch_assoc()) {
echo "<a href=\\"image.php?id=" . $row['id'] . "\\">";
echo "<img src=\\"imageResize.php?imageFilename=" . $row['name'] . "&maxHeight=120,&maxWidth=150\\" />\
";
echo "</a>\
";
}
?>
and here is image.php
<?php
$dbLink = new mysqli('localhost', 'root', '', 'gallery');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
/* $_GET['id'] passes our test, so perform security functions on it & assign it to the $id variable */
$id= $dbLink->real_escape_string($_GET['id']);
} else {
/* it doesn't exist or it isn't numeric so assign a default value so our query below always gets constructed properly & safely */
$id = 1;
}
/* construct the query */
$query = "SELECT name, size, image_path FROM images WHERE id = $id";
/* run the query */
$result = $dbLink->query($query);
while($row = $result->fetch_row()) {
echo "<img src=\\"" . $row[2] . "\\" />"; // you could also play with the other $row[] variables returned by the query here.
}
?>
What rajug is saying, is that you can check the actual image size against the max image size you want to use, and if the image is bigger you call the resize script.
In his script, he uses two variables that would contain the post card size you talked about in your OP, but you can of course use any size you want.
I think there’s a little error in his code. It should be:
No, you pass them to imageResize.php as parameters in the query string (“&maxHeight=” . $maxheight . “&maxWidth=” . $maxwidth).
In imageResize.php, if no parameters are passed, a default height and width is used. But you are passing them, so if you don’t give them a value, it won’t work. Of course, you can also try calling imageResize.php without passing the height and width parameters.
This is a piece of imageResize. It says: if you passed me a parameter called maxWidth, then I’ll use its value as max width. If you didn’t pass me such a parameter, I’ll use 100 as max width.
So you have two choices.
Define a value for maxWidth and maxHeight in the script that calls imageResize (as you did in the last script you posted: $maxheight=300; $maxwidth=250; )
Call imageResize without the maxWidth and maxHeight parameters: