Tuesday, 15 July 2014

file upload - Why the quality of uploaded image quality is severely getting affected during image re-size even after using PHP GD functions? -



file upload - Why the quality of uploaded image quality is severely getting affected during image re-size even after using PHP GD functions? -

i'm re-sizing images uploaded user specific size(i.e. specific width , height). want create uploaded images of dimension 940 px * 370 px. while doing don't want impact quality of image uploaded user @ all.

the issue i'm facing code image dimensions getting reduced specified dimensions quality of image severely getting affected. uploaded images getting shrink.

i'm not getting how should maintain quality of uploaded image intact

for achieving functionality i've written next code.

html code :

<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="submit"> </form> </body> </html>

php code :

<?php $allowedexts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_files["file"]["name"]); $extension = end($temp); if ((($_files["file"]["type"] == "image/gif") || ($_files["file"]["type"] == "image/jpeg") || ($_files["file"]["type"] == "image/jpg") || ($_files["file"]["type"] == "image/pjpeg") || ($_files["file"]["type"] == "image/x-png") || ($_files["file"]["type"] == "image/png")) && ($_files["file"]["size"] < 5242880) && in_array($extension, $allowedexts)) { if ($_files["file"]["error"] > 0) { echo "return code: " . $_files["file"]["error"] . "<br>"; } else { echo "upload: " . $_files["file"]["name"] . "<br>"; echo "type: " . $_files["file"]["type"] . "<br>"; echo "size: " . ($_files["file"]["size"] / 1024) . " kb<br>"; echo "temp file: " . $_files["file"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_files["file"]["name"])) { echo $_files["file"]["name"] . " exists. "; } else { //move_uploaded_file($_files["file"]["tmp_name"], "upload/" . $_files["file"]["name"]); //store name of temporary re-create of file stored on server $images = $_files["file"]["tmp_name"]; /*create new file name uploaded image file : *prepend string "upload" it's original file name */ $new_images = "upload".$_files["file"]["name"]; //copies file contents 1 file //copy($_files["file"]["tmp_name"],"upload/".$_files["file"]["name"]); $width = 940; //determine size of given image file , homecoming dimensions along file type $size=getimagesize($images); //$height=round($width*$size[1]/$size[0]); $height = 370; //create new image file or url & returns image identifier representing image obtained given filename. $images_orig = imagecreatefromjpeg($images); //get image width of uploaded image $photox = imagesx($images_orig); //get image height of uploaded image $photoy = imagesy($images_orig); //create new true color image & returns image identifier representing black image of specified size. $images_fin = imagecreatetruecolor($width, $height); /*copy , resize part of image resampling *copies rectangular portion of 1 image image, *smoothly interpolating pixel values that, in particular, *reducing size of image still retains great deal of clarity. */ imagecopyresampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photox, $photoy); /*output image browser or file *creates jpeg file given image. */ imagejpeg($images_fin,"upload/".$new_images); /*destroy image *frees memory associated image image. */ imagedestroy($images_orig); imagedestroy($images_fin); echo "stored in: " . "upload/" . $_files["file"]["name"]; } } } else { echo "invalid file"; } ?>

it immensely helpful me if help me in regard.

thanks in advance.

for more clear understanding of issue i'm facing i'm attaching original image , modified image using php code:

original image follows:

the image after uploading server(i.e. affected image) follows:

instead of scaling image 940x370, should scale preserve aspect ratio. can this:

$scalex = $width / $photox; $scaley = $height / $photoy; $scale = min($scalex, $scaley); $scalew = $scale * $photox; $scaleh = $scale * $photoy; $images_fin = imagecreatetruecolor($width, $height); $background = imagecolorallocate($images_fin, 0, 0, 0); imagefill($images_fin, 0, 0, $background); imagecopyresampled($images_fin, $images_orig, $width / 2 - $scalew / 2, $height / 2 - $scaleh / 2, 0, 0, $scalew+1, $scaleh+1, $photox, $photoy);

this ensure resulting image fits within 940x370 dimensions. remainder of image (if aspect ratio didn't match 940 370) filled in black.

php file-upload image-uploading image-resizing php-gd

No comments:

Post a Comment