How to display image with PHP
By Sergey Skudaev
By Sergey Skudaev
Here you will learn how to display image and how to create image thumbnail with PHP. The PHP source code for this tutorial you will download at the bottom of the page. You will find also PHP source code for creating thumbnail image. To use PHP image functions you have to uncomment extension line in PHP.INI file. Find PHP.INI file in "C:\PHP" directory and in "C:\WINNT\System32\" directory. Scroll down to Windows Extensions and delete ";" character in front of gd2.dll
;Note that MySQL and ODBC support is now built in, so no dll is needed for it.
Open notepad or textpad and write a PHP code:
Save the file as image.php in "htdocs/dspimage" directory. Create "phpimages" directory inside dspimage directory and place inside an image. In my example image file name is "dog.jpg". If your image file name is different, then change the image file name in php code. Image file must have jpg extension. Of course, it is possible to display gif or png images, but for now use jpg image for simplicity sake.
Start Apache and your browser and type in the URL: http://localhost/dspimage/image.php.
Image will be displayed. It is easy to understand the code.
1. You get path to the image file
2. You create image using function imagecreatefromjpeg (for jpg image file) If you has gif image then you have to use imagecreatefromgif function.
3. You have to set mime type in header so that your browser knew what kind of data will sent to it.
4. You display image in browser with imagejpeg function.
Actually, imagejpeg function has two more arguments. It can output image in file for example:
imagejpeg($image, "newfile.jpg", 50);
The third argument is image quality. If you enter a file name then jpg file will be created, but browser will not display the image.
You can display caption on the image. Add few lines of code. Get image height, allocate color, display caption text with font 5 and color white, 100 pixels from the left and 50 from the bottom.
// get image height
//allocate color for image caption (white)
$color=imagecolorallocate($image, 255, 255, 255);
//Add text to image bottom
imagestring($image, 5, 100, $imgheight-50, "September 2005", $color);
Problem with this code is that you cannot display any text on the page, because you set mime type for image/jpeg. If you want to display the image on the page with text use image.php file as source for the img src tag.
To dowload PHP script .
If you sign for hosting through one of 4 links on my site I placed below I will send you PHP authentication templates for free. See details user Authentication Demo
Hosting companies I use and recomend!
1. Fatcow.com I use it for www.configure-all.com, www.hardstuffez.com and www.healthstairs.com
40% off at FatCow!
Host Unlimited Domains - 1 Account $6.95 Per Month
HostMonster - Perl, CGI, SSH, Free Domain
Affiliate Disclosure: I may get comission, when you purchase products or services through links on my website.
Did you find information useful?
Send to your friend a link to this page
Please rate the tutorial
My eBooks on Amazon.com3>
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- Environment Variables
- Delete Trojan horse
- PHP Code Examples
- Learn PHP
- Modeless Popup
- Read Screen Resolution
- PHP Class Example
- Page View Counter
- Display Any Table
- Read csv file
- PHP Array Functions
- Array of Fields
- GET mess
- Array of Check boxes
- PHP and MS Access
- User_Auth. Demo
- PHP App Example
- PHP + Oracle 1
- PHP + Oracle 2
- Image Collection
- Create Gallery
- Block Spam Post
- Headers Allredy Sent
- Virtual Server
- Apache:OS 10048
- Set Shopping Cart
- My Best CSS Layout
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn C++
- Learn Java
- Learn Visual Basic
- Web Site Tips
- Site map