GD & Images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jrcal
Forum Newbie
Posts: 3
Joined: Fri Jul 30, 2004 1:14 pm

GD & Images

Post by jrcal »

I know this may sound silly to some of you - but I'm a newbie and I definitely need some direction.

I would like to create a site that allows one to upload & display images. How do I use the GD library to resize/alter images that the user uploads?

Also, how do I store these images in a MySQL database? Is there a special image function?



Thanks,
JR
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Welcome to the forums.

Searching the forums, you'll find all that you asked for:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Be sure to read the links ~feyd has provided, but to sum up what they'll say:

To resize images:

use [php_man]imagecopyresampled[/php_man]() if you've got GD 2.01 or greater, or [php_man]imagecopyresized[/php_man]() if you don't. imagecopyresampled() does a much better job of making thumbnails.

To insert into a DB:
A number of methods to do this, but I use this function

Code: Select all

function read_in($p_file_path,$add_slashes = true)
{
  $filehandle = fopen($p_file_path,'rb');
  $image_data = fread($filehandle,filesize($p_file_path);
  $image_data = ($add_slashes) 
          ? mysql_escape_string($image_data) 
          : $image_data;
  fclose($filehandle);

  return($image_data);
}
Then, take the returned data, and insert it into the DB. Be sure to use a BLOB type so the data will fit. BLOB will only accept 16K worth of data (I think), so I'd use MEDIUMBLOB which can old up to 1.6 MB.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply