Computer Programming web Web programming Tips



How to Display Image. PHP Code Example

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 also find the PHP source code for creating a 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

PHP.INI

;Windows Extensions
;Note that MySQL and ODBC support is now built in, so no dll is needed for it.
;
;extension=php_mbstring.dll
;extension=php_bz2.dll
;extension=php_cpdf.dll
;extension=php_crack.dll
;extension=php_curl.dll
;extension=php_db.dll
;extension=php_dba.dll
;extension=php_dbase.dll
;extension=php_dbx.dll
;extension=php_domxml.dll
;extension=php_exif.dll
;extension=php_fdf.dll
;extension=php_filepro.dll
extension=php_gd2.dll
;extension=php_gettext.dll
;extension=php_hyperwave.dll

Open notepad or textpad and write a PHP code:

<?php

$imagepath="phpimages/dog.jpg";

$image=imagecreatefromjpeg( $imagepath);

header('Content-Type: image/jpeg');

imagejpeg($image);

?>




Save the file as image.php in "htdocs/dspimage" directory. Create "phpimages" directory inside dspimage directory and place inside an image. In my example, an 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 a jpg extension. Of course, it is possible to display gif or png images, but for now, use the jpg image for simplicity sake.

Start Apache and your browser and type in the URL: http://localhost/dspimage/image.php.

The image will be displayed. It is easy to understand the code.

1. You get the path to the image file

2. You create the image using function imagecreatefromjpeg (for jpg image file) If you have a gif image, then you have to use the imagecreatefromgif function.

3. You have to set mime type in the header so that your browser knew what kind of data will be sent to it.

4. You display the image in a 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 the browser will not display the image.

You can display a caption on the image. Add a few lines of code. Get image height, allocate color, display caption text with font 5 and the color white, 100 pixels from the left and 50 from the bottom.

<?php

$imagepath="phpimages/dog.jpg"; $image=imagecreatefromjpeg($imagepath); // get image height $imgheight=imagesy($image); //allocate a 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); header('Content-Type: image/jpeg'); imagejpeg($image); ?>

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.

My eBooks on Amazon.com

US    UK    BR    CA
US   UK   BR   CA
US    UK    BR    CA