Page 1 of 1

Crop from About 20% of the Way Down

Posted: 2011-04-17T22:11:20-07:00
by Danny
What if I wanted ImageMagick to crop from about 20% of the way down and not the center or top?
What would I put here?

Code: Select all

// Resize using ImageMagick executable
function ime_im_cli_resize($old_file, $new_file, $width, $height, $crop) {
	$cmd = ime_im_cli_command();
	if (empty($cmd))
		return false;

	$old_file = addslashes($old_file);
	$new_file = addslashes($new_file);

	$cmd = "\"$cmd\" -limit memory 150mb -limit map 128mb -size {$width}x{$height} '{$old_file}' -resize {$width}x{$height}";
	if ($crop)
		$cmd .= "^ -gravity north -extent {$width}x{$height}";

	$quality = ime_get_option('quality', '-1');
	if (is_numeric($quality) && $quality >= 0 && $quality <= 100 && ime_im_filename_is_jpg($new_file))
		$cmd .= " -quality " . intval($quality);

	$cmd .= " '{$new_file}'";
	exec($cmd);

	return file_exists($new_file);
}

Image

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T10:17:52-07:00
by fmw42
The direct approach would need to compute the y offset in pixels from 20% of the input image height

-crop WidthxHeight+0+Yoff

However, you cannot specify offsets in percentages, but you can specify width and height by percentages.

So the other (better) way is to do

convert image -gravity south -crop 100%x80%+0+0 +repage resultimage

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T13:10:30-07:00
by Danny
thanks bro
so

Code: Select all

// Resize using ImageMagick executable
function ime_im_cli_resize($old_file, $new_file, $width, $height, $crop) {
	$cmd = ime_im_cli_command();
	if (empty($cmd))
		return false;

	$old_file = addslashes($old_file);
	$new_file = addslashes($new_file);

	$cmd = "\"$cmd\" -limit memory 150mb -limit map 128mb -size {$width}x{$height} '{$old_file}' -resize {$width}x{$height}";
	if ($crop)
		$cmd .= "^ south -crop 100%x80%+0+0 +repage resultimage {$width}x{$height}";

	$quality = ime_get_option('quality', '-1');
	if (is_numeric($quality) && $quality >= 0 && $quality <= 100 && ime_im_filename_is_jpg($new_file))
		$cmd .= " -quality " . intval($quality);

	$cmd .= " '{$new_file}'";
	exec($cmd);

	return file_exists($new_file);
}

?

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T13:14:07-07:00
by fmw42
$cmd .= "^ south -crop 100%x80%+0+0 +repage resultimage {$width}x{$height}";
Perhaps I don't follow your script well, but this line looks strange. Where is the -gravity for the south and why the {$width}x{$height} at the end of the line?

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T13:29:04-07:00
by Bonzo

Code: Select all

// Resize using ImageMagick executable
function ime_im_cli_resize($old_file, $new_file, $width, $height, $crop) {
   $cmd = ime_im_cli_command();
   if (empty($cmd))
      return false;

   $old_file = addslashes($old_file);
   $new_file = addslashes($new_file);

   $cmd = "\"$cmd\" -limit memory 150mb -limit map 128mb -size {$width}x{$height} '{$old_file}' -resize {$width}x{$height}";
   if ($crop)
      $cmd .= "^ south -crop 100%x80%+0+0 +repage resultimage {$width}x{$height}";

   $quality = ime_get_option('quality', '-1');
   if (is_numeric($quality) && $quality >= 0 && $quality <= 100 && ime_im_filename_is_jpg($new_file))
      $cmd .= " -quality " . intval($quality);

   $cmd .= " '{$new_file}'";
   exec($cmd);

   return file_exists($new_file);
}
Looks a bit of a mess to me but then I have never used cli command :?

There is no convert or -gravity, what does the ^ and resultimage do and as Fred says why the extra {$width}x{$height}?

The variable like this '{$old_file}' on their own usualy do not need to be in curly brackets let alone curly brackets and inverted commas. But used like this {$width}x{$height} they need the curly brackets or '.

I would start with something simple and get each stage to work echoing out the contents of $cmd as you go. Then put it all together when everything is working.

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T13:55:00-07:00
by Danny
the problem is that the width and heights are not the same throughout the site. this is a script that regenerates thumbnails throughout my website

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T15:48:43-07:00
by anthony
shell scripting point about {$width}x{$height}

NOTE: in shell the '$' goes outside the curly braces... EG: ${width}

As you have it the variable will be replaced, but be left surrounded by braces. EG: {12345}

However the braces would still do the job of separating the variables from the 'x' or other letters,
The shell however would probably remove them (unless in quotes) as being 'curly brace filename expansion. EG: aa{b,c}zz maps to aabzz aaczz without any comma the braces are preserved by the shell, so remains.

IM on seeing the braces would probably error with... "invalid argument for option"
Except in this case the position would cause IM to seeing it as a filename! As such you would probably have a lot of weird filenames in your current directory.

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T19:25:14-07:00
by Danny
i have no idea what you guys are saying, but i'll have someone look at this thread and find out how to translate what was said into modifying the script to do what i want it to do

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T19:35:32-07:00
by fmw42
Danny wrote:i have no idea what you guys are saying, but i'll have someone look at this thread and find out how to translate what was said into modifying the script to do what i want it to do

Go back to my original reply above where I suggest the following to do the command line to crop an image:

convert image -gravity south -crop 100%x80%+0+0 +repage resultimage

Compare that to your line

$cmd .= "^ south -crop 100%x80%+0+0 +repage resultimage {$width}x{$height}";

Seems like you have some left over code and should have something like

$cmd .= "-gravity south -crop 100%x80%+0+0 +repage";

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T20:16:57-07:00
by Danny
gotcha.

Code: Select all

// Resize using ImageMagick executable
function ime_im_cli_resize($old_file, $new_file, $width, $height, $crop) {
	$cmd = ime_im_cli_command();
	if (empty($cmd))
		return false;

	$old_file = addslashes($old_file);
	$new_file = addslashes($new_file);

	$cmd = "\"$cmd\" -limit memory 150mb -limit map 128mb -size {$width}x{$height} '{$old_file}' -resize {$width}x{$height}";
	if ($crop)
		$cmd .= "-gravity south -crop 100%x80%+0+0 +repage";

	$quality = ime_get_option('quality', '-1');
	if (is_numeric($quality) && $quality >= 0 && $quality <= 100 && ime_im_filename_is_jpg($new_file))
		$cmd .= " -quality " . intval($quality);

	$cmd .= " '{$new_file}'";
	exec($cmd);

	return file_exists($new_file);
}
didnt work though.
maybe it's a wordpress thing

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T20:21:06-07:00
by fmw42
I would suggest you write some temp files out to see where it is failing and how.

Where does wordpress come into all this?

If the ^ you had in the earlier code was really part of the resize, then perhaps you should put it back in that part of the code rather as the beginning of the crop code, e.g. and put a space before -gravity

$cmd = "\"$cmd\" -limit memory 150mb -limit map 128mb -size {$width}x{$height} '{$old_file}' -resize {$width}x{$height}^";
if ($crop)
$cmd .= " -gravity south -crop 100%x80%+0+0 +repage";

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T20:28:35-07:00
by Danny
what it did was this
http://d.pr/X5Vy

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T20:38:36-07:00
by fmw42
Danny wrote:what it did was this
http://d.pr/X5Vy

Sorry I don't see the connection nor do I understand your script very well. It seems to be only a part of a larger script that runs IM command lines from PHP exec() command. But as I really don't know what you start with and what you are trying to accomplish, it is hard to determine what is an error in your code. I am really not a PHP expert, so I am just trying to look at your command line part of the code.

Again I would suggest you are your expert try writing some intermediate images out and see what they produce and where it is becoming unexpected.

Can you post a link to an input image and your expected output image and explain what it is you want to happen.

I am also puzzled by

$cmd = "\"$cmd\" -limit memory 150mb -limit map 128mb -size {$width}x{$height} '{$old_file}' -resize {$width}x{$height}^";

but again I am not familiar with you way of coding. I simply use the command line and not any API or PHP. So I am not really expert on that aspect.

Re: Crop from About 20% of the Way Down

Posted: 2011-04-18T22:22:08-07:00
by anthony
Add a space to the appended string! two arguments are being joined together.

$cmd .= " -gravity south -crop 100%x80%+0+0 +repage";

Re: Crop from About 20% of the Way Down

Posted: 2011-04-19T09:57:44-07:00
by fmw42
anthony wrote:Add a space to the appended string! two arguments are being joined together.

$cmd .= " -gravity south -crop 100%x80%+0+0 +repage";
I also mentioned that above.