开发者

Upload, resize, and crop center of image with PHP

开发者 https://www.devze.com 2023-03-15 23:59 出处:网络
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'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 res开发者_JAVA技巧izing or anything, just a simple PHP resize and crop. How is this done?


The GD Library is a good place to start.

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


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');


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'; }


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消