-1

I am currently using this code so users can upload images to my site

$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg','.JPG','.PNG','.BIF','.GIF','.JPEG'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; //  filesize in BYTES (currently 0.5MB).
$uploadpath = "/home/path/to/file/files/avatars/$_SESSION[user]";
$posted9 = hash('md5',$_SESSION["user"]).$ext; /* not for security mind you. */
// Upload the file to your specified path.
        $f = file_put_contents(
            $upload_path . $posted9,
            file_get_contents('php://input')
            );
        if($f)
    $m="Avatar updated<br/>"; // It worked.
  else
     $m="There was an error during the file upload.  Please try again."; // It failed :(.

However, when the images are rezised to fit inside the comments on my website, they become distorted and lose their quality.

How using PHP can I resize the images so that the quality is maintained?

Community
  • 1
  • 1
user3236661
  • 702
  • 1
  • 6
  • 12
  • There are plenty of tutorials and samples on Google. Did you even bother to research it? There are also countless questions of that type on StackOverflow too... – BenM Jan 31 '14 at 20:05
  • 1
    RTFM: http://php.net/imagecopyresampled – Marc B Jan 31 '14 at 20:05
  • You're asking about resizing images, but only show code for uploading images... and find a better method for testing filetype than a long array of case-sensitive file extensions – Mark Baker Jan 31 '14 at 20:06

2 Answers2

1
   public function resize($width,$height) {
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  $this->image = $new_image;
  } 

http://us3.php.net/imagecopyresampled

ssaltman
  • 3,623
  • 1
  • 18
  • 21
1
<?php

// Url of the file
$url = "/homepages/0/d502303335/htdocs/foo.jpg";

// Set a maximum height and width, I used the ones on your picture
$width = 49;
$height = 47;


// Get new dimensions
list($width_orig, $height_orig) = getimagesize($url);

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output, You can use another url, In my case i just overwrite the picture
imagejpeg($image_p, $url);

?>
Here's the result: http://www.filmgratuiti.org/varie/cenBqHxMD90Q2.jpg
Much more better then your's

So basically after you have uploaded the file you run my code and you are good to go

Community
  • 1
  • 1
Giacomo Pigani
  • 2,256
  • 27
  • 36