On Thu, 2002-02-14 at 08:42, Eric Torr Klopper wrote:
> How would you go about doing this. Could somebody send me some sample
code and also explain how to get the image link into the database so I
can access the image. I'm fairly new to MySql and 
> PHP so any help on this will be appreciated.

I use this for the photo album on my web site (http://www.guydavis.ca).
It's not a high traffic site by any means, so performance isn't an
issue.  Can't show you PHP, but here's what I did in an Java servlet. 
It handles uploaded (POST) images from friends.  byte_out is a
ByteArrayOutputStream.  imageIcon is an Icon that I create using JDK
1.4's headless AWT support to allow me to calculate the image's
thumbnail dimensions.

  Connection con = WebUtils.getConnection();
  String st = "INSERT INTO photos (location, description, date_shot,
filename, mimetype, img_data, ";
  st += "height, width, thumb_height, thumb_width, category, access,
title) ";
  st += "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  PreparedStatement stmnt = con.prepareStatement(st);
  stmnt.setString(1, ((String) params.get("location")));
  stmnt.setString(2, ((String) params.get("description")));
  stmnt.setString(3, date);
  stmnt.setString(4, (image.getFileName()));
  stmnt.setString(5, image.getContentType());
  stmnt.setBytes(6, byte_out.toByteArray());
  stmnt.setInt(7, imageIcon.getIconHeight());
  stmnt.setInt(8, imageIcon.getIconWidth());
  stmnt.setInt(9, thumb_height);
  stmnt.setInt(10, thumb_width);
  stmnt.setInt(11, Integer.parseInt((String) params.get("category")));
  stmnt.setString(12, (String) params.get("access"));
  stmnt.setString(13, ((String) params.get("title"))); 
  stmnt.executeUpdate();

Given that most of the photos posted by myself and friends are personal,
I needed an access control system and I wanted something more robust
than a publicly accessible image with a random name.  The only way to do
that is to have a servlet or CGI read in a image from disk or database
(where's it's not web accessible) and write the data out to the client.

Here's the relevant code from my PhotoServlet:

  String query = "SELECT * FROM photos WHERE id="+id;
  ResultSet rs = WebUtils.doSQL(query);
  if (!rs.first()) {
        displayError(request, response);
        return;
  }  

  // if it's marked personal, only logged in people should see it
  if ((rs.getString("access").equalsIgnoreCase("Personal")) && 
    (session.getValue("login") == null)) {
        displayError(request, response);
        return;
        }

  if (rs.getString("filename").toUpperCase().indexOf(".GIF") >=
0)                  
        response.setContentType("image/gif");       
  else
        response.setContentType("image/jpeg");      

  Blob img_blob = rs.getBlob("img_data");
  InputStream in = img_blob.getBinaryStream();
  while (in.available() > 0)
  buf_out.write(in.read());
  buf_out.flush();
  buf_out.close();          


Here's the CREATE TABLE for my photos table:

photos | CREATE TABLE `photos` (
  `id` smallint(5) unsigned NOT NULL auto_increment,
  `location` varchar(255) NOT NULL default '',
  `description` mediumtext NOT NULL,
  `date_shot` date NOT NULL default '0000-00-00',
  `filename` varchar(255) NOT NULL default '',
  `mimetype` varchar(50) NOT NULL default '',
  `height` smallint(5) unsigned default NULL,
  `width` smallint(5) unsigned default NULL,
  `thumb_height` smallint(5) unsigned default NULL,
  `thumb_width` smallint(5) unsigned default NULL,
  `img_data` longblob,
  `category` smallint(5) unsigned default NULL,
  `access` enum('Public','Personal') default 'Personal',
  `title` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM


I don't see why you shouldn't be able to do something similar in PHP.
Hope this helps.

-- 
Guy Davis               Phone:   (403) 301-3426       
Pason Systems           Fax:     (403) 301-3499
PGP: 65BA 484B 0B96 5F3B 4D40  DCA2 B2AE 6B5A F52B 1445

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to