- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP Image Processing
Image can be processed easily using PHP. Here you will learn about processing images using PHP.
When to Process Image
Normally you can process image using PHP in one of the following time:
- when you want to process the uploading image and store the processed image to your computer system (or web server)
- when you want to process and display any image from your computer system (or web server) to your website
Now let's first learn about how to process image that is going to upload on your server by your website user.
Process Uploading Image using PHP
As you have already learned about how to upload any file using PHP in previous tutorial, therefore we will not talk about that. Here we will only talk about the correct way to process any image file using PHP before saving it on the server or inside your computer system.
Below is complete example of image processing using PHP.
<?php $count = 0; if($_SERVER["REQUEST_METHOD"] == "POST") { // let's first check whether the image is selected or not if(empty($_FILES['ImageToProcess'])) { echo "<p>Image is required before doing processing using PHP.</p>"; echo "<p>Exiting...</p>"; exit(); } else { $tmp_image = $_FILES['ImageToProcess']['tmp_name']; // in last part of code given below contains double \ // where first \ is the part of directory and second // \ is to make double inverted comma works $where_to_process_image = "C:/xampp/htdocs/uploaded-file/"; $original_image_name = basename($_FILES['ImageToProcess']['name']); $original_image_name_with_directory = $where_to_process_image.$original_image_name; $process_image_type = pathinfo($original_image_name_with_directory, PATHINFO_EXTENSION); if(isset($_POST['submit'])) { $check_image_before_process = getimagesize($_FILES['ImageToProcess']['tmp_name']); if($check_image_before_process != false) { // if image is correct according to image size } else { echo "<p>Error before processing the given image.</p>"; echo "<p>Exiting...</p>"; exit(); } } $processed_image = $original_image_name.".".$process_image_type; $process_image_size = $_FILES['ImageToProcess']['size']; // now check whether the processing image size is less than // 10MB or not. You can also change the size value according // to your requirement. Here you have to provide the byte value // for 10 MB it will be 1024*1024*10, which will be // 10485760 bytes if($process_image_size > 10485760) { echo "<p>Image with size greather than 10MB is not allowed to processs...exiting...</p>"; exit(); } // now check the type of image before finalizing the process of given image if($process_image_type != "jpg" && $process_image_type != "png" && $process_image_type != "jpeg") { echo "<p>Image other than jpeg, jpg and png is not allowed to process...exiting...</p>"; exit(); } // now move the processed image from temporary directory to your // provided directory and at the same time also check whether // the image is successfully moved or not if(move_uploaded_file($tmp_image, $where_to_process_image.$processed_image)) { $save_processed_image = $where_to_process_image.$processed_image; $image_file = $where_to_process_image.$processed_image; list($width, $height) = getimagesize($image_file); $tln = imagecreatetruecolor($width, $height); $target = $where_to_process_image.$processed_image; $image_info = getimagesize($target); $tln = imagecreatetruecolor($width, $height); $target = $where_to_process_image.$processed_image; $image_info = getimagesize($target); if($image_info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($image_file); } elseif($image_info['mime'] == 'image/png') { $image = imagecreatefrompng($image_file); } imagecopyresampled($tln, $image, 0, 0, 0, 0, $width, $height, $width, $height); $count++; } else { echo "<p>Error occurred in saving the image in your given directory..exiting..</p>"; exit(); } } } ?> <html> <head> <title>PHP Image Processing Example - codescracker</title> </head> <body> <?php if($count==0) { ?> <form method="post" enctype="multipart/form-data"> <input type="file" name="ImageToProcess" required><br/><br/> <input type="submit" value="Process and Upload Image"> </form> <?php } else { echo "<p>Your image is processed and saved successfully.</p>"; } ?> </body> </html>
Here is the initial sample output produced by the above example code of image processing using PHP.

Now to check the demo working of above example code that how the above code in PHP can do image processing and saving the file inside your computer directory provided in the above example, first choose any image file and click on the Process and Upload Image button.
Here is the sample screenshot after selecting an image file to process and upload.

Now click on the Process and Upload Image button. Here is the sample output you will see in your browser, if the image file is correct and processed/uploaded successfully.

Now if you will open the folder where the above action is performed, that is, here uploaded-file is the folder name, then you will see the folder contains a file named image.jpg which is an image file. Below is the sample screenshot after running the above example.

Now you have learned about how to process any uploading/saving image file at your given place, now let's learn about how to process the image from computer or server and display it on the web.
Process and Display Image on Web in PHP
Here is an example shows how to process and display the image on the web or website using PHP. Here we will process the above uploaded image using the example given above in this tutorial of image processing using PHP.
<?php $process_image_dir = 'uploaded-file/'; $process_image_name = 'image.jpg'; $image_with_dir = $process_image_dir.$process_image_name; ?> <html> <head> <title>Image Processing using PHP Example - codescracker.com</title> </head> <body> <?php echo "<img src=$image_with_dir>"; ?> </body> </html>
Here is the sample output produced by the above example of image processing in PHP.

As you can see from the above screenshot of sample output, the full size of image is displayed on the web.
Now let's modify the above example to see the image in particular size using CSS.
<?php $process_image_dir = 'uploaded-file/'; $process_image_name = 'image.jpg'; $image_with_dir = $process_image_dir.$process_image_name; ?> <html> <head> <title>Process and display image on web using PHP</title> <style> .imageDimension img{max-width:300px;} </style> </head> <body> <p>Image processed and displayed using PHP.</p> <?php echo "<div class=\"imageDimension\"><img src=$image_with_dir></div>"; ?> </body> </html>
Here is the sample output, now you will see in your browser after modifying the code of processing and displaying any image on the web using PHP.

« Previous Tutorial Next Tutorial »