Upload, resize, and crop center of image with PHP

感情迁移 提交于 2019-12-04 12:04:22

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!