sudkrishna/myimagecrop

Crop image upload widget by Sud


License
BSD-3-Clause

Documentation

install 
require 
      -    sudkrishna/myimagecrop":"dev-master"


public function actionUploadimage()
    {//i

        
        $valid_exts = array('jpeg', 'jpg', 'png', 'gif');
$max_file_size = 200 * 1024; #200kb
$nw = $nh = 200; # image with # height

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	if ( isset($_FILES['image']) ) {
		
			$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
                        
                        
			if (in_array($ext, $valid_exts)) {
					$path = 'uploads/' . uniqid() . '.' . $ext;
					$size = getimagesize($_FILES['image']['tmp_name']);

					$x = (int) $_POST['x'];
					$y = (int) $_POST['y'];
					$w = (int) $_POST['w'] ? $_POST['w'] : $size[0];
					$h = (int) $_POST['h'] ? $_POST['h'] : $size[1];

					$data = file_get_contents($_FILES['image']['tmp_name']);
					$vImg = imagecreatefromstring($data);
					$dstImg = imagecreatetruecolor($nw, $nh);
					imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $nw, $nh, $w, $h);
					imagejpeg($dstImg, $path);
					imagedestroy($dstImg);
					return "<img src='$path' />";
					
				} else {
					return 'unknown problem! Please select Image and cordinates';
				} 
		 
	} else {
		return 'file not set';
	}
} else {
    return 'bad request!';
}
        
        
    }//i end of actionUploadimage()