Upload, resize and crop using PHP

I want to create a very simple download, resize and crop PHP script. The functionality for this will be the same (the last time I checked in any case) the Twitter method used to upload avatar images.

I want the script to accept any size image, reduce the size of the shortest side to 116px, and then crop the top and bottom (or the left and right side if it's terrain) to get a 116px square by 116px.

I don't need a bloated PHP script with resizing on the client side or anything else, just resizing and harvesting PHP. How it's done?

+4
source share
4 answers

The GD library is a good place to start.

http://www.php.net/manual/en/book.image.php

+4
source

There is an easy to use open source library called PHP Image Magician . It uses GD, but simplifies its use to 3 lines.

Base usage example:

$magicianObj = new imageLib('racecar.jpg'); $magicianObj -> resizeImage(100, 200, 'crop'); $magicianObj -> saveImage('racecar_small.png'); 
+4
source

If you want the example to work from my downloadable, resized and cropping class, all this plus some other interesting things - you can use it all if necessary, or just take a bit that you like:

http://www.mjdigital.co.uk/blog/php-upload-and-resize-image-class/

I do not think it is too bloated! - you can just do something this (not verified):

  if ((isset ($ _ FILES ['file'] ['error'])) && ($ _ FILES ['file'] ['error'] == 0)) {// if a file has been posted then upload it
     include ('INCLUDE_CLASS_FILE_HERE.php');
     $ myImage = new _image;
     // upload image
     $ myImage-> uploadTo = 'uploads /';  // SET UPLOAD FOLDER HERE
     $ myImage-> returnType = 'array';  // RETURN ARRAY OF IMAGE DETAILS
     $ img = $ myImage-> upload ($ _ FILES ['file']);
     if ($ img) {
         $ myImage-> newWidth = 116;
         $ myImage-> newHeight = 116;
         $ i = $ myImage-> resize ();  // resizes to 116px keeping aspect ratio
         // get new image height
         $ imgWidth = $ i ['width'];
         // get new image width
         $ imgHeight = $ i ['height'];
         if ($ i) {
             // work out where to crop it
             $ cropX = ($ imgWidth> 116)?  (($ imgWidth-116) / 2): 0;
             $ cropY = ($ imgHeight> 116)?  (($ imgHeight-116) / 2): 0;
             $ cropped = $ myImage-> crop (116,116, $ cropX, $ cropY);
             if ($ cropped) {echo 'It Worked (I think!)';  print_r ($ cropped);
             } else {echo 'Crop failed';  }
         } else {echo 'Resize failed';  }
     } else {echo 'Upload failed';  }
+2
source

I made this simple function that is very easy to use, it allows you to resize, crop and center the image to a specific width and height, it can support JPG, PNG and GIF. Feel free to copy and paste it into your code:

 function resize_imagejpg($file, $w, $h, $finaldst) { list($width, $height) = getimagesize($file); $src = imagecreatefromjpeg($file); $ir = $width/$height; $fir = $w/$h; if($ir >= $fir){ $newheight = $h; $newwidth = $w * ($width / $height); } else { $newheight = $w / ($width/$height); $newwidth = $w; } $xcor = 0 - ($newwidth - $w) / 2; $ycor = 0 - ($newheight - $h) / 2; $dst = imagecreatetruecolor($w, $h); imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($dst, $finaldst); imagedestroy($dst); return $file; } function resize_imagegif($file, $w, $h, $finaldst) { list($width, $height) = getimagesize($file); $src = imagecreatefromgif($file); $ir = $width/$height; $fir = $w/$h; if($ir >= $fir){ $newheight = $h; $newwidth = $w * ($width / $height); } else { $newheight = $w / ($width/$height); $newwidth = $w; } $xcor = 0 - ($newwidth - $w) / 2; $ycor = 0 - ($newheight - $h) / 2; $dst = imagecreatetruecolor($w, $h); $background = imagecolorallocatealpha($dst, 0, 0, 0, 127); imagecolortransparent($dst, $background); imagealphablending($dst, false); imagesavealpha($dst, true); imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight, $width, $height); imagegif($dst, $finaldst); imagedestroy($dst); return $file; } function resize_imagepng($file, $w, $h, $finaldst) { list($width, $height) = getimagesize($file); $src = imagecreatefrompng($file); $ir = $width/$height; $fir = $w/$h; if($ir >= $fir){ $newheight = $h; $newwidth = $w * ($width / $height); } else { $newheight = $w / ($width/$height); $newwidth = $w; } $xcor = 0 - ($newwidth - $w) / 2; $ycor = 0 - ($newheight - $h) / 2; $dst = imagecreatetruecolor($w, $h); $background = imagecolorallocate($dst, 0, 0, 0); imagecolortransparent($dst, $background); imagealphablending($dst, false); imagesavealpha($dst, true); imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight,$width, $height); imagepng($dst, $finaldst); imagedestroy($dst); return $file; } function ImageResize($file, $w, $h, $finaldst) { $getsize = getimagesize($file); $image_type = $getsize[2]; if( $image_type == IMAGETYPE_JPEG) { resize_imagejpg($file, $w, $h, $finaldst); } elseif( $image_type == IMAGETYPE_GIF ) { resize_imagegif($file, $w, $h, $finaldst); } elseif( $image_type == IMAGETYPE_PNG ) { resize_imagepng($file, $w, $h, $finaldst); } } 

All you have to do is call it like this:

 ImageResize(image, width, height, destination); 

eg.

 ImageResize("uploads/face.png", 100, 150, "images/user332profilepic.png"); 
0
source

All Articles