Site Cloner PHP Script
Bargain Hunter PHP Script
Job Hunter PHP Script
Site Login and Access Control PHP Script

Image Uploads and Resizing with PHP

You’ve added images everywhere before in sites like facebook and MSN. The procedure is fairly straightforward to open an image, resize the image, rewite the image and save the new image. Below is a siomple tutorial with a form showing how to add a resized image into a form.

require “connect.php”;
//only resize images if form is filled in properly
if ($_POST[‘FirstName’] && $_POST[‘LastName’] ) {// file must be jpg, gif, png and maximun 1 MB
if (($_FILES[“img_upload”][“type”] == “image/jpeg” || $_FILES[“img_upload”][“type”] == “image/pjpeg” || $_FILES[“img_upload”][“type”] == “image/gif” || $_FILES[“img_upload”][“type”] == “image/x-png”) && ($_FILES[“img_upload”][“size”] < 1000000))
// some settings
$max_image_width = 2592;
$max_image_height = 1944;

// if user chosed properly then scale down the image according to user preferences
if(isset($_REQUEST[‘max_img_width’]) and $_REQUEST[‘max_img_width’]!=” and $_REQUEST[‘max_img_width’]<=$max_image_width){
$max_image_width = $_REQUEST[‘max_img_width’];
if(isset($_REQUEST[‘max_img_height’]) and $_REQUEST[‘max_img_height’]!=” and $_REQUEST[‘max_img_height’]<=$max_image_height){
$max_image_height = $_REQUEST[‘max_img_height’];

// JPEGs
if($_FILES[“img_upload”][“type”] == “image/jpeg” || $_FILES[“img_upload”][“type”] == “image/pjpeg”){
$image_source = imagecreatefromjpeg($_FILES[“img_upload”][“tmp_name”]);
// GIFs
if($_FILES[“img_upload”][“type”] == “image/gif”){
$image_source = imagecreatefromgif($_FILES[“img_upload”][“tmp_name”]);

if($_FILES[“img_upload”][“type”] == “image/x-png”){
$image_source = imagecreatefrompng($_FILES[“img_upload”][“tmp_name”]);

$remote_file = “my_uploaded_images/”.$_FILES[“img_upload”][“name”];

// get the dimensions in width and height of orig image
list($image_width, $image_height) = getimagesize($remote_file);

if($image_width>$max_image_width || $image_height >$max_image_height){
$proportions = $image_width/$image_height;

$new_width = $max_image_width;
$new_height = round($max_image_width/$proportions);
$new_height = $max_image_height;
$new_width = round($max_image_height*$proportions);

$new_image = imagecreatetruecolor($new_width , $new_height);
$image_source = imagecreatefromjpeg($remote_file);

imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);





<form name=”form1″ method=”post” action=”<?php echo  $_SERVER[‘SELF’];?>” enctype=”multipart/form-data”>

Maximum 1MB. Accepted Formats: jpg, gif and png:<br />
<input name=”img_upload” type=”file” id=”img_upload” size=”40″ />

<br />
<br />
<br />
<input name=”max_img_width” type=”hidden” id=”max_img_width” value=”130″ size=”4″>

<input name=”max_img_height” type=”hidden” id=”max_img_height” value=”54″ size=”4″>

<td >First Name</td>
<td><input name=”FirstName” value=”” type=”text” id=”FirstName”></td>
<td>Last Name </td>
<td ><input name=”LastName” value=”” type=”text” id=”LastName”></td>

<p><input type=”submit” class=”form_submit_button” onclick=”display_alert()” name=”Submit” value=”Submit”>


You may want to add the image name to a database table for other purposes; such as a user’s profile picture. Below, shows how this can be done.

You can add this above the form!

if (!$remote_file){
//here is a dummy image if they upload no image
} else {
$emp_filename=str_replace(‘my_uploaded_images/’, ”, $emp_filename);
$command = “INSERT INTO table VALUES (NULL, ‘$ip’, now() , ‘0000-00-00 00:00:00’,  ‘$emp_filename’)”;
$result = mysqli_query($db, $command);