4

I'm wanting to create a very very basic upload, resize, and crop PHP script. The functionality to this will be identical (last i checked anyway) to the method Twitter uses to upload avatar pictures.

I want the script to take any size image, resize the shortest side to 116px, then crop off the top and bottom (or left and right side if it's landscape) as to get a square 116px by 116px.

I don't want a bloated PHP script with client side resizing or anything, just a simple PHP resize and crop. How is this done?

Adam
  • 9,189
  • 15
  • 46
  • 62
  • http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php – Frankie Jun 27 '11 at 23:52
  • 1
    what have you done, so far? some script? – ariefbayu Jun 27 '11 at 23:52
  • I have not started it with this exact project, but I was trying to do it on some past projects and couldn't figure it out. I was hoping someone would know of some basic code to get me started. – Adam Jun 27 '11 at 23:53

4 Answers4

4

There a simple to use, open source library called PHP Image Magician. It uses GD but simplifies it's usage to 3 lines.

Example of basis usage:

$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');
Jarrod
  • 9,349
  • 5
  • 58
  • 73
4

The GD Library is a good place to start.

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

jjwdesign
  • 3,272
  • 8
  • 41
  • 66
2

If you want an example to work from my upload, resize and crop class does all of this plus some other cool stuff - you can use it all if needed or just take the bits out that you like:

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

I don't think it is too bloated! - you can just do something this (not tested):

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'; }
mj7
  • 351
  • 1
  • 7
  • Thanks - although I realised in my original post I left out the last two parameters of the crop method - D'oh! I have fixed it above now so hopefully it should be OK - just in case you needed the crop method should be called using: $myImage->crop($cropToWidth,$cropToHeight,$cropFromX,$cropFromY); – mj7 Jul 07 '11 at 09:38
0

I made this simple function which is very easy to use, it allows you to resize, crop and center an image to a specific width and height, it can suppert JPGs, PNGs and GIFs. Feel free to copy and paste it to 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 to use it is call it like so:

ImageResize(image, width, height, destination);

E.g.

ImageResize("uploads/face.png", 100, 150, "images/user332profilepic.png");
Casey Neistat
  • 81
  • 1
  • 2
  • 6