Scale an image using PHP


Scale an image using PHP


<form action="/upload.php" enctype="multipart/form-data" method="post">
<input id="upload" name="upload" type="file" />
<input id="final-upload" type="submit" value="Upload Image" /></form>

<?php
define( 'MAX_FILESIZE', 20000 );
define( 'UPLOAD_PATH', $_SERVER['DOCUMENT_ROOT'] . 'uploads/' );
    
if( !empty( $_FILES['upload'] ) && $_POST )
{

//some error code checking
if( !empty( $_FILES['upload']['error'] ) )
{
switch( $_FILES['upload']['error'] )
{
 case '1':
 $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
 break;
case '2':
     $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  break;
case '3':
  $error = 'The uploaded file was only partially uploaded';
  break;
  case '4':
     $error = 'No file was uploaded.';
       break;
 case '6':
      $error = 'Missing a temporary folder';
      break;
 case '7':
        $error = 'Failed to write file to disk';
            break;
    case '8':
        $error = 'File upload stopped by extension';
       break;
      case '999':
        default:
            $error = 'No error code avaiable';
        }
    }
elseif( empty( $_FILES['upload']['tmp_name'] ) )
{
    $error = 'No file was uploaded.';
    }
    // Is the file an image?
    elseif( !preg_match( '|image/|', $_FILES['upload']['type'] ) )
    {
        $error = 'File is not an image.';
    }
    // Is the file small enough?
    elseif( $_FILES['upload']['size'] > MAX_FILESIZE )
    {
        $error  = 'File is too big.';
    }


if( !$error )
    {
        // Find the uploaded file extension
        $ext    = substr( $_FILES['upload']['name'], strrpos( $_FILES['upload']['name'], '.' ) + 1 );
   
        // If the main file has uploaded, proceed to create the thumbnail
    if( move_uploaded_file( $_FILES['upload']['tmp_name'], UPLOAD_PATH . $_FILES['upload']['name'] ) )
        {
            $main_image = UPLOAD_PATH . $_FILES['upload']['name'];
   
            // Create the thumbnail
            $make_thumb = create_thumb( $main_image, 100, 100, UPLOAD_PATH . 'thumb_' . $_FILES['upload']['name'], $ext );
   
            // Check that the thumbnail was created
            if( !$make_thumb )
            {
                $error = 'Thumbnail was unable to be created.';
            }
        }
        else
        {
            $error  = 'Failed to upload image. Ensure directories are created properly.';
        }
    }
function create_thumb( $imgSrc, $thumbnail_width, $thumbnail_height, $dest_src, $ext )
    {
        //getting the image dimensions
        list( $width_orig, $height_orig ) = getimagesize( $imgSrc );

        // Check if the images is a gif
        if( $ext == 'gif' )
        {
            $myImage = imagecreatefromgif($imgSrc);
        }
        // Check if the image is a png
        elseif( $ext == 'png' )
        {
            $myImage = imagecreatefrompng($imgSrc);
        }
        // Otherwise, file is jpeg
        else
        {
            $myImage = imagecreatefromjpeg($imgSrc);
        }
   
        // Find the original ratio
        $ratio_orig = $width_orig / $height_orig;
   
        // Check whether to scale initially by height or by width
        if( $thumbnail_width / $thumbnail_height > $ratio_orig )
        {
            $new_height = $thumbnail_width/$ratio_orig;
            $new_width  = $thumbnail_width;
        }
        else
        {
            $new_width      = $thumbnail_height*$ratio_orig;
            $new_height = $thumbnail_height;
        }
   
        $x_mid = $new_width / 2;  //horizontal middle
        $y_mid = $new_height / 2; //vertical middle
   
        $process = imagecreatetruecolor( round( $new_width ), round( $new_height ) );
   
        // Scale the image down and the reduce the other axis to create the thumbnail
        imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
        $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
        imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)),   $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
   
        // Depending on the file extension, save the file
        if( $ext == 'gif' )
        {
            imagegif( $thumb, $dest_src );
        }
        elseif( $ext == 'png' )
        {
            imagepng( $thumb, $dest_src );
        }
        else
        {
            imagejpeg( $thumb, $dest_src, 100 );
        }
   
        // Remove rubbish file data
        imagedestroy($process);
        imagedestroy($myImage);
   
        // Return thumb ( success / fail )
        return $thumb;
    }
}?>