DreadyCrig
1/13/2015 - 4:53 PM

Image Resizer

Image Resizer

<?php


function handle_file()
{
	global $msg;
	$path = ABSPATH . '/upload/company/';

	if ( ! isset( $_FILES['file'] ) )
	{
		$msg = 'No file selected.<br>';
		return FALSE;
	}
	$file = $_FILES['file'];

	//if there was an error uploading the file
    if ( $file['error'] !== UPLOAD_ERR_OK )
    {
        $msg = '<p style="color:red;"><strong>Error!</strong><br> No file uploaded. Please try again.</p>';
        return FALSE;
    }

	if ( ! verify_valid_image( $file['tmp_name'] ) )
	{
		$msg = '<p style="color:red;"><strong>Error!</strong><br> Only valid Images of JPG, BMP or PNG files are accepted. Please try again.</p>';
		return FALSE;
	}

	// ensure a safe filename
	$storagename = preg_replace( "/[^A-Z0-9._-]/i", "_", $file['name'] );

	// don't overwrite an existing file
	$i = 0;
	$parts = pathinfo( $storagename );
	while ( file_exists( $path . $storagename ) )
	{
		$i++;
		$storagename = "{$parts['filename']}-{$i}.{$parts["extension"]}";
	}

/*
	if ( FALSE === move_uploaded_file( $file['tmp_name'], $path . $storagename ) )
	{
		$msg = '<p style="color:red";><strong>Error!</strong><br>File did not upload properly.</p>';
		return FALSE;
	}
*/

//	$file    = $path . $storagename;

	require( 'lib/Zebra_Image.php' );

	// create a new instance of the class
	$image = new Zebra_Image();

	// indicate a source image (a GIF, PNG or JPEG file)
	$image->source_path = $file['tmp_name'];

	$file_name  = $path . $storagename;
	$image->target_path = $file_name;
	$image->jpeg_quality = 100;
	$image->preserve_aspect_ratio  = true;
	$image->enlarge_smaller_images = true;
	$image->preserve_time = true;

	$width  = 0;
	$height = 77;

	if ( ! $image->resize($width, $height, ZEBRA_IMAGE_CROP_CENTER) )
	{
        die( 'Error uploading image!' );
	// if no errors
	}

	//echo "Stored in: " . "upload/" . $_FILES["file"]["name"] . "<br />";
	return $storagename;

}