Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize image in PHP

I want to write some PHP code that automatically resizes any image uploaded via a form to 147x147px, but I have no idea how to go about it (I'm a relative PHP novice).

So far, I've got images uploading successfully, filetypes being recognized and names cleaned up, but I'd like to add the resize functionality into the code. For example, I've got a test image that is 2.3MB, and 1331x1331 in dimension, and I'd like the code to size it down, which I'm guessing will dramatically compress the file size of the image, too.

So far, I've got the following:

if ($_FILES) {                 //Put file properties into variables                 $file_name = $_FILES['profile-image']['name'];                 $file_size = $_FILES['profile-image']['size'];                 $file_tmp_name = $_FILES['profile-image']['tmp_name'];                                  //Determine filetype                 switch ($_FILES['profile-image']['type']) {                     case 'image/jpeg': $ext = "jpg"; break;                     case 'image/png': $ext = "png"; break;                     default: $ext = ''; break;                 }                                  if ($ext) {                     //Check filesize                     if ($file_size < 500000) {                         //Process file - clean up filename and move to safe location                         $n = "$file_name";                         $n = ereg_replace("[^A-Za-z0-9.]", "", $n);                         $n = strtolower($n);                         $n = "avatars/$n";                         move_uploaded_file($file_tmp_name, $n);                     } else {                         $bad_message = "Please ensure your chosen file is less than 5MB.";                     }                 } else {                     $bad_message = "Please ensure your image is of filetype .jpg or.png.";                 }             } $query = "INSERT INTO users (image) VALUES ('$n')"; mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query); 
like image 916
Alex Ryans Avatar asked Feb 01 '13 15:02

Alex Ryans


People also ask

How do I scale an image in PHP?

The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height. Parameters: This function accepts four parameters as mentioned above and described below: $image: It is returned by one of the image creation functions, such as imagecreatetruecolor().

How do I make an image smaller in PHP?

Resize Exact SizeFirst pass in the image we want to resize in the class constructor, then define the width and height with the scale option of exact. The class will now have the create dimensions to create the new image, now call the function saveImage() and pass in the new file location to the new image.

How do I resize an image in code?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels. For example, the original image is 640×960.


1 Answers

You need to use either PHP's ImageMagick or GD functions to work with images.

With GD, for example, it's as simple as...

function resize_image($file, $w, $h, $crop=FALSE) {     list($width, $height) = getimagesize($file);     $r = $width / $height;     if ($crop) {         if ($width > $height) {             $width = ceil($width-($width*abs($r-$w/$h)));         } else {             $height = ceil($height-($height*abs($r-$w/$h)));         }         $newwidth = $w;         $newheight = $h;     } else {         if ($w/$h > $r) {             $newwidth = $h*$r;             $newheight = $h;         } else {             $newheight = $w/$r;             $newwidth = $w;         }     }     $src = imagecreatefromjpeg($file);     $dst = imagecreatetruecolor($newwidth, $newheight);     imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);      return $dst; } 

And you could call this function, like so...

$img = resize_image(‘/path/to/some/image.jpg’, 200, 200); 

From personal experience, GD's image resampling does dramatically reduce file size too, especially when resampling raw digital camera images.

like image 115
Ian Atkin Avatar answered Sep 21 '22 03:09

Ian Atkin