Jump to content

Image Resize Blury


Guest

Recommended Posts

When i upload an image that is to big the picture gets very blury it does resize it though. Please help.

[code]
define ( 'UPLOAD_PATH', 'Uploads/Houses/' );
define ( 'MAX_SIZE', '100000' ); // define the max single file size (bytes)
$allow = array ( 'png', 'gif', 'jpg' ); // allowed types
$max_width = 640; // max width (resize width)
$max_height = 480; // max height (resize height)
$max_twidth = 100; // max width (resize width)
$max_theight = 100; // max height (resize height)
$save = false; // save original upload image (bool true or false)

$quality = 100; // the quality for (jpg) images


//upload thumbnail
if ( is_uploaded_file ( $_FILES['image']['tmp_name']['0'] ) )
{
$fs = filesize ( $_FILES['image']['tmp_name']['0'] );
// file size test (size test)

if ( $fs > 10 && $fs <= MAX_SIZE )
{
$fn = strtolower ( $_FILES['image']['name']['0'] );
$fe = substr ( $fn, ( strrpos ( $fn, '.' ) + 1 ) );

// is it a valid image type (extension test)
if ( in_array ( $fe, $allow ) )
{
// is it a valid image type (image file test)
if ( ( $image = getimagesize ( $_FILES['image']['tmp_name']['0'] ) ) !== false )
{
// get the width and height of the upload image
list ( $width, $height ) = $image;
// load the upload image into the image resource

switch ( $fe )
{
case 'gif' :
$old = imagecreatefromgif ( $_FILES['image']['tmp_name']['0'] );
break;
case 'jpg' :
$old = imagecreatefromjpeg ( $_FILES['image']['tmp_name']['0'] );
break;
case 'png' :
$old = imagecreatefrompng ( $_FILES['image1']['tmp_name']['0'] );
break;
}
// create the original image ratio
$original_ratio = ( $width / $height );
// figure what controls the resize (width or height)
if ( $max_twidth / $max_theight > $original_ratio )
{
$max_twidth = ( $max_theight * $original_ratio );
}
else
{
$max_theight = ( $max_twidth / $original_ratio );
}

// create a new image resource the size of the new ratio
$new = imagecreatetruecolor ( $max_twidth, $max_theight );
//copy original to the resized image and resample it
imagecopyresampled ( $new, $old, 0, 0, 0, 0, $max_twidth, $max_theight, $width, $height );

// save the new thumbnail image
switch ( $fe )
{
case 'gif' :
imagegif ( $new, UPLOAD_PATH . $imagename . "-1-t." . $fe );
break;
case 'jpg' :
imagejpeg ( $new, UPLOAD_PATH . $imagename . "-1-t." . $fe, $quality);
break;
case 'png' :
imagepng ( $new, UPLOAD_PATH . $imagename . "-1-t." . $fe );
break;
}

// kill the image resources

imagedestroy ( $old );
imagedestroy ( $new );

// are we saving the orignal

if ( $save ) {
@move_uploaded_file ( $_FILES['image']['tmp_name']['0'], UPLOAD_PATH . $sn );
}

$out = "Image 1 uploaded successfully";
$sqlthumb1 = UPLOAD_PATH . $imagename . "-1-t." . $fe;
}
}
}
}

// end of thumbnail[/code]
Link to comment
https://forums.phpfreaks.com/topic/28021-image-resize-blury/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.