Dynamic Image

images php coding issues or problems here.

Moderators: macek, egami, gesf

ravinder
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Mar 31, 2011 6:17 am

Dynamic Image

Postby ravinder » Tue Apr 05, 2011 1:10 am

How to upload a image in mysql data base and retrieve from data base

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Dynamic Image

Postby egami » Tue Apr 05, 2011 4:36 am

drink lots of India Pale Ale beer, and hope for the best!

Kairu
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Apr 05, 2011 7:48 am

Re: Dynamic Image

Postby Kairu » Tue Apr 05, 2011 8:34 am

Well, in order to store image-data in a database, you need to use a BLOB field which stores binary data.
You should be familiar with:
- Uploading files using HTML-Forms
- Inserting Rows into databases

First you should create a database-table with at least four fields:
- imageid (INT-field: auto-incremented)
- imagedata (BLOB-field: the image-data.)
- imagesize (INT-field: the size (in Bytes) of the image)
- imagetype (VARCHAR-field: holds the file-extension of the image)

Uploading the image to the database

Here's what you need to do FOR UPLOADING:
1. Take the image you want (presumably uploaded from an HTML form) and read the contents using something like file_get_contents() -- this will be a variable and we will name it "$image_data". We will also define 2 more variables which will hold the size of the image, and the file-extension of the image.
1.1.

Code: Select all

$image_data = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$image_size = $_FILES['image']['size'];
$image_type = mysql_real_escape_string(end(explode('.', $_FILES['image']['name'])));

***Note: $_FILES['image'] comes from the HTML-Form Element: <input type="file" name="image" />

2. Insert a new row into a database
2.2

Code: Select all

mysql_query("INSERT INTO `image_table` (`imagedata`, `imagesize` `imagetype`) VALUES ('".$image_data."', ".$image_size.", '".$image_type."')");


Retrieving the image from the database

You need to define a $_GET variable which will hold the image-id (we'll call this "$_GET['imageid']")

Here's what you need to do FOR READING:
1. Using the $_GET variable we defined, we can retrieve the image (specified by $_GET['imageid']) from the database
1.1

Code: Select all

$result = mysql_query("SELECT `image_data`, `image_size`, `image_type` FROM `image_table` WHERE `imageid`=".(int)$_GET['imageid']);

2. Check if a row corresponding to the value of $_GET['imageid'] was found. If a row was found, then we need to output the image-data
2.1

Code: Select all

if(mysql_num_rows($result) == 0){
    echo 'Image not found';
} else {
    $row = mysql_fetch_assoc($result);
    header('Content-type: image/'.$row['image_type']);
    header('Content-Length: '.$row['image_size']);
    echo stripslashes($row['image_data']);
}


This method will enable to retrieve the image using the HTML IMG-tag like so:

Code: Select all

<img src="http://example.com/get_image.php?imageid=42" />

Note: get_image.php is just an arbitrary name. You can make it whatever you want.

Note: The code I have provided is only for clarification purposes. It is not 100% secure with the exception of escaping strings and type-casting variables.

annyphp
New php-forum User
New php-forum User
Posts: 76
Joined: Sun Nov 28, 2010 6:33 pm
Contact:

Re: Dynamic Image

Postby annyphp » Wed Apr 20, 2011 6:15 pm

Hi
You can find some dynamic image scripts on PHPKode.com
Recommend a good scripts named Image dynamic resizer:
Images can be loaded from a specified file or a previously computed one can be used. Image dynamic resizer can resize the image to a given width or height keeping the original aspect ratio.

If the original aspect ratio differs from the desired size aspect ratio, the resulting image can either be cropped or filled with a given background color.
Check out http://www.phpkode.com/scripts/item/ima ... ic-esizer/ for more info...

Sherlyn
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Apr 25, 2011 5:04 am
Location: Cleveland
Contact:

Re: Dynamic Image

Postby Sherlyn » Mon Apr 25, 2011 5:19 am

can upload by simple php mysql Database can just see few function how to use upload function :- http://stackoverflow.com/questions/3869 ... -using-php

User avatar
webinfopedia
New php-forum User
New php-forum User
Posts: 84
Joined: Sat May 21, 2011 10:26 pm
Contact:

Re: Dynamic Image

Postby webinfopedia » Wed May 25, 2011 10:49 am

Hello

I think its better to upload an image to the web directory and saving its name in database than the uploading image directly to database. because we have some advantages if you uploaded image to web directory.
In below article i have explained it. You can download the example file also by viewing demo
http://www.webinfopedia.com/php-upload-immage-to-database.html

Thank You.

maryjiel
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Aug 14, 2011 4:25 pm

Re: Dynamic Image

Postby maryjiel » Sun Aug 14, 2011 4:47 pm

Hello Guys, Im having a problem with my thesis. I am trying to create a program for our department and i am looking some code namely.

1. How to upload Image (jpeg, png, gif)
2. How to upload Audio (mp3)
3. How to upload Documents (pdf, doc, docx. xlsx, xls)
4. How to upload File (rar)

I need a code that are same code but are restricted to only those type of files.

Please help me, I don't have any idea yet with BLOB and how to do it.

Thanks. :help:

User avatar
webinfopedia
New php-forum User
New php-forum User
Posts: 84
Joined: Sat May 21, 2011 10:26 pm
Contact:

Re: Dynamic Image

Postby webinfopedia » Mon Aug 15, 2011 4:17 am

Hi

Kindly check my below article. You can download the example file also by viewing demo.
http://www.webinfopedia.com/php-upload-immage-to-database.html

Here what you have to change is the file type in if condition. U can get the file type using $_FILES['image']['type'] in PHP.

Will post the example file soon..

Thank You
http://www.webinfopedia.com

maryjiel
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Aug 14, 2011 4:25 pm

Re: Dynamic Image

Postby maryjiel » Mon Aug 15, 2011 9:50 pm

@Webinfopedia.

I tried to understand the code but why is it that there is a "../../classes/config.php" and does it mean by

header("Location:index.php?img=".base64_encode($file_name)."");
} else {
header("Location:index.php?mgs=error");



-------------

I have sent you an email please do send me 2 samples (.pdf and .mp3) so that i can understand and see the difference of the two.

thank you very much.


Return to “PHP coding => Images”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron