View Full Version : Resize image..?
EthanX
04-28-2003, 09:15 PM
Okay I have been trying for some time to get a image from MySQL database to resize for thumbnailing.
Well I already have code to display the image, but now this code below is supose to resize it. Well it doesn't work I just get a nice X :(
I'm using <img src="tumb.php?id=1&sz=110"> to display the thumbnail.
Here's my PHP code.
<?
if ($id_files) {
include "database.inc";
$linkID = @mysql_connect("$dbhost", "$dbuser", "$dbpassword");
mysql_select_db("$dbname", $linkID) or die(mysql_error());
$sql = "SELECT data, filetype FROM images WHERE id_files=$id_files";
$result = @mysql_query($sql, $linkID);
$type = @mysql_result($result, 0, "filetype");
$data = @mysql_result($result, 0, "data");
header("Content-type: $type");
echo $data;
}
//echo "$type";
//if ($type == "image/pjpeg") {
$img = imagecreatefromjpeg("cms_admin/images/logo.gif");
//}
//if ($type == "image/gif") {
// $img = imagecreatefromgif("thumb.php?id_files=$id");
//}
//if ()) {
// $img = imagecreatefromjpeg("$dir/Gallery_folder/$Gal_name/$img");
//}
//if () {
// $img = imagecreatefrompng("$dir/Gallery_folder/$Gal_name/$img");
//}
$img_width=imageSX($img);
$img_height=imageSY($img);
if($img_width >= $img_height) {
$factor = $sz/$img_width;
$new_width = $sz;
$new_height = $img_height * $factor;
} else {
$factor = $sz/$img_height;
$new_height = $sz;
$new_width = $img_width * $factor;
}
// resize
$new_img=ImageCreate($new_width,$new_height);
ImageCopyResized($new_img,$img,0,0,0,0,$new_width,$new_height,$img_width,$img_height);
// output
//header("Content-type: image/jpeg");
Imagejpeg($new_img,'',75); // quality 75
// cleanup
ImageDestroy($img);
ImageDestroy($new_img);
?>
if anyone has any idea please let me know.
Thanks.
EthanX
04-28-2003, 11:00 PM
updated the code.. still doesn't work though..
<?
if ($id_files) {
include "database.inc";
$linkID = @mysql_connect("$dbhost", "$dbuser", "$dbpassword");
mysql_select_db("$dbname", $linkID) or die(mysql_error());
$sql = "SELECT data, filetype FROM images WHERE id_files=$id_files";
$result = @mysql_query($sql, $linkID);
$type = @mysql_result($result, 0, "filetype");
$data = @mysql_result($result, 0, "data");
header("Content-type: $type");
echo $data;
}
//echo "$type";
//if ($type == "image/pjpeg") {
$img = imagecreatefromjpeg("thumb.php?id_files=$id");
//}
//if ($type == "image/gif") {
// $img = imagecreatefromgif("thumb.php?id_files=$id");
//}
//if ()) {
// $img = imagecreatefromjpeg("$dir/Gallery_folder/$Gal_name/$img");
//}
//if () {
// $img = imagecreatefrompng("$dir/Gallery_folder/$Gal_name/$img");
//}
$img_width=imageSX($img);
$img_height=imageSY($img);
if($img_width >= $img_height) {
$factor = $sz/$img_width;
$new_width = $sz;
$new_height = $img_height * $factor;
} else {
$factor = $sz/$img_height;
$new_height = $sz;
$new_width = $img_width * $factor;
}
// resize
$new_img=ImageCreate($new_width,$new_height);
ImageCopyResized($new_img,$img,0,0,0,0,$new_width,$new_height,$img_width,$img_height);
// output
//header("Content-type: image/jpeg");
Imagejpeg($new_img,'',75); // quality 75
// cleanup
ImageDestroy($img);
ImageDestroy($new_img);
?>
why are you storing the image binary in the database instead of in the filesystem?
surely you don't want to force the additional overhead of having to instantiate a database connection to view any image, not to mention the distribution and scalability drawbacks?
because the practice of storing image binaries in databases is one of my pet hates (as yet not a single person has given me a valid reason for them doing that) I'm loathe to help further - perhaps if you explain your reasoning behind putting them in as a blob I'll help.
note: I'm not just trying to be awkward here, I'm trying to set you on a much better system for image handling than the one you're using.
EthanX
04-29-2003, 08:00 PM
My reson of using it is that it's harder to leach the image or hotlink. Also the fact that the script may be used on other sites that may not have permissions keeping them from using a upload script, such as base dir and Safe_mode.
There is more resons I can't think of right now but the main is mysql is easy to use on any server. It is rare to run into problems with this compared to file uploading.
Hope this is a good enough reson. The only problem I've run into is how to resize into a thumbnail. So if you could please help, It would be most helpful.
Thanks.
EthanX
05-01-2003, 10:29 PM
Any help here?
and a simple .htaccess file (whether allow,deny 127.0.0.1 or a full re-write) wouldn't accomplish the remote linking ban better?
the likelihood of a host having GD/Gimp/ImageMagick and be running safe mode is null - any admin who needs safe mode probably wouldn't be able to suss out installs of any of the image manipulation modules - so there's no need to try resizing the images.
on an aside: if you really insisted upon using a database to store non changing data (such as images) you'd do best storing the thumbnail blob in there as well (utterly ridiculous concept to me - pet hate as you might have guessed) as you'd obviously create the thumbnail somewhere in the upload function. Doing thumbnails at every page load would bring the server to a crawl if anyone used the app for a decent sized site.
upload...
test image [type / size / dimensions]
create thumb from ['tmp_name'] file
...
do whatever with both large ['tmp_name'] file and thumbnail blo (I'd guess you can run the imagejpeg() call to a variable and then do whatever with that.
...
end upload
If you are designing a gallery application and want a feature that puts it above the rest, you might like to go GD2+ and incorporate thumbnail manipulations - perhaps with my thumbnail class (http://www.phpclasses.org/browse.html/package/1007.html) (shameless plug)...
include(.....thumb_class.php');
$a = new Thumbnail($_FILES['tmp_name'],'base.jpg','output.jpg',85,manipulations);
EthanX
05-02-2003, 09:06 PM
Hmm.. Thanks for the help.
So your saying using a database will slow down the server?
Also, If I make the thumbnail in a blog you think in the upload it can auto make a thumbnail of what they're uploading to the database?
Thanks
yes, of course it will slow down the pages and server as every call to an image has to instantiate a db connection and pull the binary data out - rather than the call being a simple http request straight to the file.
statistically (speed wise and resource wise) it is best to
upload the image -
get information from it (eg size, dimensions, type, title) -
add only those details to the database -
mysql_insert_id() reference check -
move_uploaded_file() it to a public folder (protected with .htaccess) - naming it based on the prior mysql_insert_id -
and create a thumbnail version named 'thumb_' .$ref (either in the same or a sub folder) -
then when you come to display, you only read the db once to get the image information - which includes the id field which can be used within a src="'.$id.'.jpg" echo.
protecting the folder shouldn't be too difficult with .htaccess (worth learning)
fast and sleek
also - as you have created the thumbnail already, the resource used by GD to do the resizing doesn't have to be used again. plus, as the files are static, you as admin know that any page you view will always render that way for everyone.
EthanX
05-03-2003, 10:11 AM
Thanks.
what's title on a image?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.