Forum Moderators: coopster

Message Too Old, No Replies

Resize images from DB

         

Sub_Seven

10:37 pm on Dec 28, 2010 (gmt 0)

10+ Year Member



I know I have been posting quite a few questions lately but I’m stuck on one of those projects that request things that you’ve never done before, I believe though that this is the last piece to solve the puzzle.
I have created a form to upload images to MySQL, I can display them in the browser by specifying the ID like this: [url...] show.php?id=1 and just when I thought I was getting finished I remember the little fact that I need those images to have a specific height (I can’t ask the end users to resize their images before uploading them) so here is where I am now.
Some research has led me to figure I need to use imagecopyresized, I have been reading about it and I could not mix it or make it work with the script that serves the images in the browser which would be the following:
<?php
$username = "user";
$password = "pass";
$host = "localhost";
$database = "binary";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$id = $_GET['id'];
if(!isset($id) || empty($id)){
die("");
}
else{

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
echo $content;
}
?>

Or maybe there are better options to resize images from a DB, I don’t need to keep two copies, I just need the one resized to fit a max height, could someone give me a little extra help here? Thank you :)

rainborick

6:35 am on Dec 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You should be able to find some suitable code to use by searching for thumbnail scripts. The key is getting the image data out of the script once you've resized the GD object. Fetch the user's image from the temp file in a GD image object. Calculate the size reduction and create a second GD object with that new size as the destination for your call to imagecopyresized().

If you're storing the image data in your database, then you should be able to use imagejpeg(), imagegif(), etc. (as appropriate, and enclosed in mysql_real_escape_string) as the value to include in your MySQL INSERT statement by not including a file name in the image#*$!() function call. While I've worked a lot with PHP and GD, I don't store images in my databases, so if someone corrects this advice you should listen to them.

If you're storing the resulting images on the server (and just saving a URI or URL in the database), then you'll want to include a file name in the image#*$!() call, with the appropriate file path information. In this case, the job is deciding whether to use a complete or partial URL for the database and translating the file path into a URL properly. Watch out for write permission issues... those often trip me up when storing uploaded files from users.