Vijaykarthik
Vijaykarthik

Reputation: 333

How to resize image with out loss clarity in php

I am uploading image in php, while uploading i want to resize image in four pixel(800*700, 620*310, etc...).For this i tried below code, in that i am getting image with exact pixel but i am getting bad image quality. its not resizing with quality, i have attached my code below please help me to solve this Thank you

PHP:

if(isset($_FILES['file_img']['name']))
    {
        for($i=0; $i<count($_FILES['file_img']['name']); $i++) 
        {
               $tmpFilePath = $_FILES['file_img']['tmp_name'][$i];
               if ($tmpFilePath != "")
               {    
                   $path = "floorplan/"; // create folder 
                   $floorPlanName = $_POST['file_name'][$i];
                   $name = $_FILES['file_img']['name'][$i];
                   $filename = stripslashes($_FILES['file_img']['name'][$i]);
                    $extension = getExtension($filename);
                    $extension = strtolower($extension);


                    if($extension=="jpg" || $extension=="jpeg" )
                    {
                    $src = imagecreatefromjpeg($tmpFilePath);
                    }

                    else 
                    {
                    $src = imagecreatefromgif($tmpFilePath);
                    }
                    list($width,$height)=getimagesize($tmpFilePath);


                    $newwidth=1000;
                    $newheight=($height/$width)*$newwidth;
                    $tmp=imagecreatetruecolor($newwidth,$newheight);


                    $newwidth2=312;
                    $newheight2=209;
                    $tmp1=imagecreatetruecolor($newwidth2,$newheight2);


                    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);



                    imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height);




                    $filename = "floorplan/".$_FILES['file_img']['name'][$i];



                    $filename1 = "floorplan/vs". $_FILES['file_img']['name'][$i];





                    imagejpeg($tmp,$filename,100);

                    imagejpeg($tmp1,$filename1,100);

                    imagedestroy($src);
                    imagedestroy($tmp);
                    imagedestroy($tmp1);

                    $paname = $filename;

                   if(move_uploaded_file($_FILES['file_img']['tmp_name'][$i], $filename)) 
                   { 
                    $query=mysql_query("INSERT INTO `floor_plan` (project_id, floorplan_name, floorplan_img, vs_img, created_on) VALUES('$getId', '$floorPlanName', '$paname', '$filename1', '$createdOn' )");
                   }
             }
        }

    }

HTML:

<div class="col-md-8">
                            <div class="form-group">
                                <label>Choose file</label>
                                <input name="file_img[]" id="floorplanId" type="file" data-change="floorEvent" style="margin-top: 8px;" multiple>
                                <small class="text-danger">Note: Upload files like jpg formate</small>
                                <output id="result" />
                                <!-- <div id="remove1"></div> -->
                            </div>
                            </div>

While use this code my image quality get damage. please give some idea frndz

Upvotes: 1

Views: 216

Answers (3)

Sam
Sam

Reputation: 1559

You use imagecopyresampled() which generate image with better quality than imagecopyresized() and set the imagejpeg() quality to 100.These are the only ways to make better output.

Basically PHP does not manipulate images with excellent quality which you experience in Photoshop or other software but you can make it a little bit better by using ImageMagick library instead of GD library.How ever GD library is widely used in some cases ImageMagick can fulfill your task with better quality.

Upvotes: 1

Gaurav Dave
Gaurav Dave

Reputation: 7474

Since, you are trying to hard code a specific height and width. I would suggest you to re-sample that image via percentage.

I have done something like this:

// Re-sizing Image (Technically Resampling)
$filenameResize = 'celeb8.jpg';
$percent = 0.65;

// Get new dimensions
list($wid, $hei) = getimagesize($filenameResize);
$new_width = $wid * $percent;
$new_height = $hei * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image_org = imagecreatefromjpeg($filenameResize);
imagecopyresampled($image_p, $image_org, 0, 0, 0, 0, $new_width, $new_height, $wid, $hei);

imagejpeg($image_p, 'demo0.jpg');

See, if that helps!

Upvotes: 0

Richard
Richard

Reputation: 2815

You can use imagecopyresized() also. That works probably for me with no quality loss.

Upvotes: 0

Related Questions