codescracker


php

PHP Image Processing



« Previous Tutorial Next Tutorial »


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:

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.

php image processing

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.

php image processing example

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.

image processing code php

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.

image processing php

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.

image process code 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.

process and display image php

« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test