Forum Moderators: coopster

Message Too Old, No Replies

Resizing images stored in mysql database

         

WarrenN

3:50 pm on Oct 8, 2005 (gmt 0)

10+ Year Member



Hi
I am a relative newby to php. I am making some changes to an existing site where the atcual images are all stored in a mysql database. They are called using a script that accesses the database and then displays the image. I need to resize these images on a couple of different pages to various sizes as the stored images are really large. Thumbnail images take as long as the actual file to load. I have found some scripts using the GD library to do this but do not know how to use them on a database stored image.
Any help would be greatly appreciated
Thanks
Warren

bomburmusicmallet

12:33 am on Oct 9, 2005 (gmt 0)

10+ Year Member



Hi Warren,

You are on the right track with the GD library; very powerful!

Try this. Use this for your image tag:

<img border="0" src="phalbumresize.php?userfile=somephoto.jpg">

somephoto.jpg being the name of the image you are getting out of the database.

This will be your phalbumresize.php file, or something like this. You will need to get the image from your database in your own code; I usually use images as files in a directory.

<?php
session_start();

$thumbnailsize = 125; // or whatever you want
$userfile = $_REQUEST['userfile'];

/*do something here to get the actual image so it is the var $userfile */

$size = GetImageSize ($userfile);

if($size[2] == '2'){
$src_img = imagecreatefromjpeg($userfile);
$headertype = "jpeg";
}elseif($size[2] == '3'){
$src_img = imagecreatefrompng($userfile);
$headertype = "png";
}

if($size[0] > $size[1])
{
$new_w = $thumbnailsize;
$new_h = (int)($thumbnailsize * $size[1] / $size[0]);
}
else
{
$new_w = (int)($thumbnailsize * $size[0] / $size[1]);
$new_h = $thumbnailsize;
}

$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
header ("Content-type: image/$headertype");
if($size[2] == '2'){
imagejpeg($dst_img);
}elseif($size[2] == '3'){
imagepng($dst_img);
}
imagedestroy($dst_img);
?>

Tweak this a bit, but I'm pretty sure this is what you'll need. Let me know how you make out.

Jenny

mcavic

1:04 am on Oct 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That would require the image to be stored in a file on the disk. You might have to read it from the database, write to a temp file, then resize the temp file.

But that might be too slow or too intensive if the images are big and you're displaying lots of thumbnails. Maybe you could add a new column to the database table (or a new table) and resize all of the images before-hand?

WarrenN

2:05 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



Thanks for the help.
We decided to add a couple more fields to the table to store the image name, and then to resize the images and upload them to the server rather than the database as there is a flash file that will be reading random images as well.
Warren