Page 1 of 1

How can I crop pictures

Posted: Fri Nov 22, 2019 10:42 pm
by salvador
Hi.
I download pictures from the site, for further use I need to cut off part of the picture.
How can I do that?

Re: How can I crop pictures

Posted: Sat Nov 23, 2019 12:02 am
by kojon
Hi, you can do that using php code:

Code: Select all

function crop($file_input, $file_output, $crop = 'square',$percent = false) {
	list($w_i, $h_i, $type) = getimagesize($file_input);

   if( $type=='jpg')
       $type='jpeg';

	if (!$w_i || !$h_i) {
		echo 'not possible to get length and width';
		return;
        }
        $types = array('','gif','jpeg','png');
        $ext = $types[$type];
        if ($ext) {
    	        $func = 'imagecreatefrom'.$ext;
    	        $img = $func($file_input);
        } else {
    	        echo 'wrong format';
		return;
        }
	if ($crop == 'square') {
		$min = $w_i;
		if ($w_i > $h_i) $min = $h_i;
		$w_o = $h_o = $min;
	} else {
		list($x_o, $y_o, $w_o, $h_o) = $crop;
		if ($percent) {
			$w_o *= $w_i / 100;
			$h_o *= $h_i / 100;
			$x_o *= $w_i / 100;
			$y_o *= $h_i / 100;
		}
    	        if ($w_o < 0) $w_o += $w_i;
	        $w_o -= $x_o;
	   	if ($h_o < 0) $h_o += $h_i;
		$h_o -= $y_o;
	}
   //echo "$w_o, $h_o<br>";
	$img_o = imagecreatetruecolor($w_o, $h_o);
	imagecopy($img_o, $img, 0, 0, $x_o, $y_o, $w_o, $h_o);
	if ($type == 2) {
		return imagejpeg($img_o,$file_output,100);
	} else {
		$func = 'image'.$ext;
		return $func($img_o,$file_output);
	}
}
function call

Code: Select all

crop("path to picture","path to result picture", array(left, top, width, height),"in percents");

Re: How can I crop pictures

Posted: Sat Nov 23, 2019 12:42 am
by salvador
And what does left, top, width, height and "in percent" mean?

Re: How can I crop pictures

Posted: Sat Nov 23, 2019 1:23 am
by kojon
left - indent from the left edge of the picture
top - indent from the top edge of the picture
width - picture width
height - picture height
"in percent" - indicates the height and width parameters specified in percent or not.

Here is such a call:

Code: Select all

crop ("1.png", "1_0x10x80x92_t.png", array (0,10,80,92), true);
This is retreating from the top edge of 10px with 80% of the width remaining, and 92% of the height. According to these parameters, the image is cropped.

If making a call:

Code: Select all

crop ("1.png", "1_0x10x80x92_t.png", array (0,10,80,92), false);
Then the indentation from above is the same, the width of the image is 80, the height is 92. That is, we can either set rigid dimensions or cut off a percentage of the width and height.

Re: How can I crop pictures

Posted: Sat Nov 23, 2019 2:23 am
by salvador
Thank you very much!