Learn PHP
Site Map
PHP
HTML
Authentication
Php Functions
C++ - Starting Out
Web Design Tools
Photoshop Tutorials
Windows
Phones
Droid Phones
Building Websites
ArrowTech Blog
Web Servers
Networking
Making Money
Favorite Links
About
ObamaCare
Software
Privacy Policy

Category: php

.

How to get images out of mysql database with php and use them on my webpages

This tutorial expands on another database/image how-to How to Store Images in Mysql Database. In the Images In tutorial I showed you the basic steps to opening a file (your image file) and read it into a variable ($img), convert that file using php's base64_encode() function, and stick that file into a Mysql database. Now, you will learn how to bring those images back out of your Mysql database, and output them to a web browser.

Retrieving Images From Mysql Database With php

This Tutorial will cover two different ways of taking images out of a mysql database and rendering them with a php document.

  1. Create a mysql database with a table matching (Database Example)
    • Database name is "test_imgs"
    • Table name is pictures
    • "id" is primary, key, not null, and auto_increment
    • "pics" blob not null
    • "ext" is varchar(4) not null
    • "gender" is varchar(7) not null
    • I will assume English mysql defaults for everything else.
    • Practice script "test.php"

    Database Example

    id pics ext gender
    1 retergfghfghterhgcfnt
    hy6e5ghjwtjthj
    jpg female
  2. Create a directory on your server and call it "test"
  3. Create a file named "test01.php" in the test directory, and copy theUploading Images Script to it.
  4. The Uploading Images Script, originated in another php tutorial about Putting Images in a Mysql Database.

    Uploading Images Script

  5. Copy the following test images and save them in your test folder.
  6. testpic.jpg testpic.gif testpic.png
  7. Create a file named "test02.php", copy the contents of the PHP Image Rendering Script to it, then save it to the test directory.
  8. First Image Rendering Method

    PHP Image Rendering Script

    This is the first method for rendering database images with php and mysql.

    This method determines what type of image it is retrieving from the database, and uses one of the follwoing php functions to render the images to end users.:

Second Image Rendering Method

  1. Create a file named "test03.php" copy Second Method Script to it. (See below)
  2. Second Method Script

    Using the Sctipts

    After building the database, and uploading all the example files and images to your server:

  3. navigate and execute test01.php script
  4. testpic.jpg will be sent to your MySql server with an id of "1".

  5. Next click the next link and go to test_02.php to view your image.
  6. Now Go back to test02.php PHP Image Rendering Script and comment out all of the php header() functions.
  7. After commenting out the header() fucntions type test03.php into your browsers nav bar.

Notice that on the test03.php page, I inserted test_02.php in the img tag. This tells the the client machine to request the pic from the server.

Author: D.Shaun Morgan

creating image watermarks Creating PHP Watermarks $my_image = imagecreatefromjpeg('photo.jpeg'); $watermark = imagecreatetruecolor(100

need to print out an array using php How toPrint PHP Arrays Things to Consider Are: Array Type- Single Array or Multidimensional Array

php      

odd number php script Testing for Odd/Even Numbers - PHP Example Banner Rotation PHP Script Odd/Even Numbers The easiest way to setup an Odd / Even test using PHP i

how to upload images and files using php Upload Images to Webserver HTML Form PHP Function # UPLOAD FILE function upload_file(){ echo '

using php cookies variables Using Cookie Variables in PHP Setting cookies in php is pretty straight forward. There is a php function: setcookie(); This function mu

how to unzip files on your free hosting account Uzipping Files on Your Web Server If you are using a free hosting account with godaddy.com and many of the other Free hosts out there, you may have found that they don't offer you a lot o

passing data from one form to another form using php Passing data from one form to another form To pass php data from a form and then catch that data into another form one way to accomplish this task is to intermi

Just In Time Compiling JIT - Just in Time Compilers JIT refers to a special software that is found in web-browsers, and scripting engines such PHP's Zend. (My understanding of ZEND is that it

Storing image locations mysql Store Image URL in Database Retrieve Image with PHP Script

how to turn variable value into variable name Variable Variables in PHP Every so often when you are writing a php file, we all run into a situation where we would like for the name of the string that is inside

what-is-php Explanation of PHP Basic Syntax --New to PHP Php is a language that is very much like writing c. The best feature of PHP versus c is that it is designed pri

how to use dreamweaver Dreamweaver - WISIWIG Dreamweaver is a tool that arguably could be called the industry standard for web design . Dreamweaver, is an Adobe product that originally b

Filezilla Filezilla Ftp Client, FTP SERVER vs Wsftp By: D.Shaun Morgan While I was writing this article, about halfway through, I realized that it would be unfair to tell my readers about

notepad plus Notepad++ Download Notepad Plus Plus Notepad plus plus is an open source, free text editor. It is

how to open create write files using php Php file functions, Opening a file in php, reading a file in php, writing a file in php In this tutorial we will look at the most commonly used file functions used in php. The

stop form spam captcha PHP -- How to Protect My Email Form / Contact Form Against Spam Bots Author: D.Shaun Morgan Versions and Skill Level PHP 5

retreive images mysql using php How to get images out of mysql database with php and use them on my webpages Author: D.Shaun Morgan PHP Version - PHP 5x

how to make money online Making Money on the Internet Sign up for this series of free money making articles Name:

store images mysql using php Storing Images in Mysql Database Tutorial Outline: Creating a Test Mysql Database Example php Database Functio

php example post array Visual example of how PHP handles the super-global array variable "$_POST" Author: D.Shaun Morgan Versions and Skill Level

php arrays Passing PHP Arrays How to pass php variable arrays between pages. Author:

how to write php functions Explanation of a Basic PHP Function What is a PHP function? In PHP a function represents a block of instructions that perform

how to write html form using php When learning PHP to build a website, one of the very first things that any newcomer, or beginner needs to learn is how to use HTML or XHTML (both will mean the same for this tutorial.) fo

register globals long arrays Security, php.ini, register_globals and register_long_arrays Author: D.Shaun Morgan Versions and Skill Level PHP Version - PHP 5x

how to install php 5 How to Install PHP 5.x.x.x on Windows This is beginner's how-to manual explaining how to quickly setup a PHP 5 installation on a Windows PC. You must have an Apache webserver installed

php passing variables Pass Variables from One Page to Another -- PHP Author: D.Shaun Morgan Tutorial Outline : Passing PHP variables with $_

.

Leave a Comment

retreive_images_mysql_php.php

Name:      | Email:      | Website:

Comment:


captcha

Enter The Above Security Code:


Author: Ravi Lohia | Website URL: |
Comment:
On using the function imagecreatefromstring() function it shows an error, "Call to undefined function imagecreatefromstring()".What should I do now.

re:Admin

Check your syntax , and make sure that your php installation supports this image function. Typically if you are wanting to create images in php you will need to have what is call the GD libray installed and available.

Look at the following link:

http://php.net/manual/en/ref.image.php
Author: Cegonsoft | Website URL: http://www.cegonsoft.co.in |
Comment:
Hi all,

I have stored images in database.. But while retrieving images, I couldn't get full picture..

Am just getting half of the image only.. This is my code.. Can anyone give me suggestions..

<?php
#connect to mysql server
$link = mysql_connect localhost , root ,

if $link {
die Not connected : . mysql_error
}

$db_selected = mysql_select_db images , $link

if $db_selected {
die Database error : . mysql_error
}

$sql = SELECT FROM testimage where sno=4
#the result of the query
$result = mysql_query $sql or die Invalid query: . mysql_error

while $image = mysql_fetch_assoc $result {
$a=$image[ image ]
$b=$image[ name ]
header Content-type: $b
echo $a
}
?<

Author: pradeep | Website URL: |
Comment:
i would like to know the inserting image into mysql database using php .i hope i will get the info in detail.
Author: ankit | Website URL: |
Comment:
I have tried the above code and getting following error while retrieving images

Warning: imagecreatefromstring [function.imagecreatefromstring]: Empty string or invalid image

Re:Admin

The most likely reason for this would be that the image that you retrieved from the database was either formatted wrong or most likely in your case -- the string that you passed the imagecreatefromstring() function was null or empty.

Look back over your code and make sure that you are getting the image and using the base64decose function before passing it.

Author: andrew | Website URL: www.efreelearn.com |
Comment:
Hi I have used this code to store and retrieve images.
Insertion happened successfully...but while I am selecting I could not view the image.

I can view a corrupted image.

regards
Andrews


Re: Admin


Could be the gd library module in your PHP installation. When retrieving an image and displaying it you can either write a piece of code to send the proper headers to your web browser or you could just recreate the image into your file system, as a temporary file and link to it with regular HTML. The latter probably incurring more of a performance hit.
Author: andrews | Website URL: www.efreelearn.com |
Comment:
Hi I used the given code to insert and view images.Insertion when successfully but when i view I get only a corrupted image like [X] .Please help
, filesize $testpic
fclose $handle


Re: Admin


Did you create an html page and point your " img src " to the page where the image retrieval code is living? Have you checked your PHP install for gd support? are you certain that you retrieved anything from your database? Look in your database and make sure the image got saved. You should see a string of charcacters. That is the bas64encoding. when you take that back of your database you will convert it back to a string and pass that into an image creation function.

$db_img = base64_decode($db_img); //print_r($db_img );

$db_img = imagecreatefromstring($db_img);

header("Content-Type: image/jpeg");
imagejpeg($db_img);

respectivley.

then

point your img src tag to the file that has the file retrieval code.

img src=location of img retrieval code
Author: Ushang Thakker | Website URL: www.smartsoftwares.tk |
Comment:
when i navigate to test02.php i get this error
Warning: imagecreatefromstring [function.imagecreatefromstring]: Data is not in a recognized format

when i printed the $db_img i got :



please help urgent


Re: Admin


make sure you used the base64_decode function before the imagecreatefromstring
Author: kam | Website URL: |
Comment:
steps to store image to mysql by php
Author: Delly | Website URL: |
Comment:
Greetings, I m trying to create a website to display my photographs. I d like to create a kind of image gallery with thumbnail that can be clicked and then open the large image. Please Whats the best way to get along this? Cheers, Drelly
Author: iqra | Website URL: http://www.watchlivestreaming.org |
Comment:
thats cool ...can you please upload for showing image on webpage?
Author: mahesh | Website URL: localhost |
Comment:
good really helpful....
Author: Novella | Website URL: |
Comment:
Hi Someone in my Myspace group shared this website with us so I came to
check it out. I m definitely loving the information. I m bookmarking and
will be tweeting this to my followers Great blog and outstanding style and design.
Author: Franky | Website URL: http://www.facebook.com/profile.php?id=100003427768725 |
Comment:
100 free dating I am rellay enjoying the theme design of your blog. Do you ever run into any browser compatibility issues? A couple of my blog audience have complained about my blog not operating correctly in Explorer but looks great in Firefox. Do you have any recomm
Author: PHP Mysql Training Institute in Bangalore | Website URL: http://www.inspiregec.com/php-mysql-training.php |
Comment:
I never seen this type of website sharing, its really great work done also its very useful to us thank you. code is very good.