php jquery 头像裁剪插件 croppic

最新做的一个站里面应客户要求加上了这个功能,偶然发现开源中国的招聘模块里面也是用的这个插件,放地址。。。 http://www.croppic.net

php jquery 头像裁剪插件 croppic_第1张图片

JS代码



upload_headpic方法   上传头像用的

public function upload_headpic(){
  
  		$config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPEG|JPG|PNG';
  		$config['max_size'] = '2048';
  		$config['max_width']  = '3000';
  		$config['max_height']  = '2000';

        $name = date('Y-m-d', time());
        $path = FCPATH.'public/uploads/'.$name;
        //上传文件按日期分文件夹
        if(!is_dir($path)){
            mkdir($path, 0777);
        }

        $config['upload_path'] = './public/uploads/'.$name;
      
        //文件名
  		$config['file_name'] = time().rand(1000, 9999);
  		
  		$this->load->library('upload', $config);
  		//上传失败
        if(!$this->upload->do_upload('img')) {
            $out = array(
            	'status' => 'error',
            	'msg' => 'fail upload!'
            );
        }else{
            $img = $this->upload->data();  //文件的一些信息
            $out = array(
            	'status'=> 'success',
            	'msg' => 'uploaded',
            	"width" => $img['image_width'],
				"height" => $img['image_height'],
            	'url' => base_url().'public/uploads/'.$name.'/'.$img['file_name']
            );
        }

        $this->output
    		->set_content_type('application/json')
    		->set_output(json_encode($out));
	} 


crop_headpic 方法   裁剪头像用的

public function crop_headpic(){
		$imgUrl = $_POST['imgUrl'];
		// original sizes
		$imgInitW = $_POST['imgInitW'];
		$imgInitH = $_POST['imgInitH'];
		// resized sizes
		$imgW = $_POST['imgW'];
		$imgH = $_POST['imgH'];
		// offsets
		$imgY1 = $_POST['imgY1'];
		$imgX1 = $_POST['imgX1'];
		// crop box
		$cropW = $_POST['cropW'];
		$cropH = $_POST['cropH'];
		// rotation angle
		$angle = $_POST['rotation'];

		$jpeg_quality = 100;
		$cropW = 233; $cropH = 213;

		$name = date('Y-m-d', time());
        $output_filename = FCPATH.'public/uploads/'.$name;
        //上传文件按日期分文件夹
        if(!is_dir($output_filename)){
            mkdir($output_filename, 0777);
        }
        $output_filename .= '/'.time().rand();
		// $output_filename = "temp/croppedImg_".rand();

		// uncomment line below to save the cropped image in the same location as the original image.
		//$output_filename = dirname($imgUrl). "/croppedImg_".rand();

		$what = getimagesize($imgUrl);

		switch(strtolower($what['mime']))
		{
		    case 'image/png':
		        $img_r = imagecreatefrompng($imgUrl);
				$source_image = imagecreatefrompng($imgUrl);
				$type = '.png';
		        break;
		    case 'image/jpeg':
		        $img_r = imagecreatefromjpeg($imgUrl);
				$source_image = imagecreatefromjpeg($imgUrl);
				error_log("jpg");
				$type = '.jpeg';
		        break;
		    case 'image/gif':
		        $img_r = imagecreatefromgif($imgUrl);
				$source_image = imagecreatefromgif($imgUrl);
				$type = '.gif';
		        break;
		    default: die('image type not supported');
		}


		//Check write Access to Directory

		if(!is_writable(dirname($output_filename))){
			$response = Array(
			    "status" => 'error',
			    "message" => 'Can`t write cropped File'
		    );	
		}else{

		    // resize the original image to size of editor
		    $resizedImage = imagecreatetruecolor($imgW, $imgH);
			imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
		    // rotate the rezized image
		    $rotated_image = imagerotate($resizedImage, -$angle, 0);
		    // find new width & height of rotated image
		    $rotated_width = imagesx($rotated_image);
		    $rotated_height = imagesy($rotated_image);
		    // diff between rotated & original sizes
		    $dx = $rotated_width - $imgW;
		    $dy = $rotated_height - $imgH;
		    // crop rotated image to fit into original rezized rectangle
			$cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
			imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
			imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
			// crop image into selected area
			$final_image = imagecreatetruecolor($cropW, $cropH);
			imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
			imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
			// finally output png image
			//imagepng($final_image, $output_filename.$type, $png_quality);
			imagejpeg($final_image, $output_filename.$type, $jpeg_quality);
			$out = pathinfo($output_filename.$type);
			$response = Array(
			    "status" => 'success',
			    "url" => base_url().'public/uploads/'.$name.'/'.$out['basename']
		    );
		}
		$this->ajaxReturn($response);
	}


End so easy.    

你可能感兴趣的:(PHP)