Jump to content

Saving Multiple Recipe Images To A Mysql Db


Legacial

Recommended Posts

Hi guys,

 

I'm working on a recipe website that stores and displays multiple recipes, ingredients and cooking methods.

 

So far, I've managed to add a new recipe, upload 1 image for the recipe, and save the recipe together with it's image and thumbnail (generated automatically from the image) in a mysql DB. The php code obtains the image name when uploading the image, creates a new folder for the image with the same name as the image (to allow multiple images for 1 recipe to be stored). The code is as shown below:

 

<?php
$mysql_link = mysql_connect("localhost", "root", "");
mysql_select_db("vyakula") or die("Could not select database");
//include('functions.php');


if(!isset($_SESSION)) {
session_start();

$id = $_SESSION['id'];
}
//error_reporting(0);
if (isset($_POST['Upload'])) {
// define the posted file into variables
$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];



$oldname = pathinfo($name);
$newname = $oldname['filename'];
mkdir("images/recipes/$newname", 0700);


//if file is empty, return error
if((empty($name))) {
echo "You have not selected an image!!";
?>
<a href="test.php" title="Add a Photo" onclick="Modalbox.show(this.href, {title: this.title}); return false;">Go Back</a> and try again.
<?php
die();
}

// if the mime type is anything other than what we specify below, kill it
if(!($type=='image/pjpeg' OR $type=='image/gif' OR $type=='image/png' OR $type=='image/jpeg')) {
echo "'".$type."' is not an acceptable image format.";
?>
<a href="test.php" title="Add a Photo" onclick="Modalbox.show(this.href, {title: this.title}); return false;">Go Back</a> and try again.
<?php
die();
}


// if the file size is larger than 4 MB, kill it
if($size>'4194304') {
echo $name . " is over 4MB. Please make it smaller.";
?>
<a href="test.php" title="Add a Photo" onclick="Modalbox.show(this.href, {title: this.title}); return false;">Go back</a> and try again.
<?php
die();
}


// if your server has magic quotes turned off, add slashes manually
if(!get_magic_quotes_gpc()){
$name = addslashes($name);
}


// open up the file and extract the data/content from it
$extract = fopen($tmp_name, 'r');
$content = fread($extract, $size);
$content = addslashes($content);
fclose($extract);


//move to folder on server
$uploadDir = "images/recipes/$newname/";
$filePath = $uploadDir . $name;
$result = move_uploaded_file($tmp_name, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}



$query_photo = "insert into temp_pics (name,size,type,content) values ('$name', '$size','$type','$content')";


if (!mysql_query($query_photo))
{
die('Error: ' . mysql_error());
}


$pic_id = mysql_insert_id();


//Get latest created row
$get_photo = mysql_query("Select * from temp_pics WHERE id='$pic_id'") or die(mysql_error());
$result_get = mysql_fetch_assoc($get_photo);


//Create thumbnail
$pathToImages = "./images/recipes/$newname/";
// open the directory
$dir = opendir( $pathToImages );


// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension


$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if (( strtolower($info['extension']) == 'jpg' ) || ( strtolower($info['extension']) == 'png' ) || ( strtolower($info['extension']) == 'gif' ))
{


$pathToThumbs = "./images/recipes/thumbs/";
//echo "Creating thumbnail for {$fname} <br />";


// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );


// calculate thumbnail size
$thumbWidth = '175';
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );


// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );


// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width,$new_height, $width, $height );


// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
$thumbimage = $fname;
}
}
// close the directory
closedir( $dir );
/*
print $thumbimage;
die();
*/
$update_photo = mysql_query("UPDATE temp_pics SET thumb_name='$thumbimage' WHERE id='$pic_id'") or die(mysql_error());
header("Location: new_recipe.php?pic");
}


?>

 

I have 2 resulting problems:

1. I'm not sure how to call the image when displaying it on the site, since the recipe name and the image name are different. Moreso, I wanted to have a gallery where the thumbnails are displayed. This works fine, but I wanted to call the larger images when a thumbnail is clicked on. How will I be able to scroll through the folder and get the larger image from the thumbnail? (Thumbnails have been stored in a separate thumbnails folder, which is contained within the larger recipes folder).

 

2. If another user wants to add another image for the same recipe, how will I make it so that the uploaded image will be saved to the specific recipe folder (which already exists)??

 

 

I would appreciate your feedback. Thanks!!

Link to comment
Share on other sites

Storing binary image data in a database is pretty irregular behaviour. Do you have a specific reason for doing it this way? Commonly you would only hold the url refference to the image that is stored in a file, on the web server (not always the same server as the database server), and in it's native format. Could we get a look at your table structure please?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.