Crop from About 20% of the Way Down

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Danny
Posts: 6
Joined: 2011-04-17T22:04:30-07:00
Authentication code: 8675308
Location: New York

Crop from About 20% of the Way Down

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop from About 20% of the Way Down

Post 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
Danny
Posts: 6
Joined: 2011-04-17T22:04:30-07:00
Authentication code: 8675308
Location: New York

Re: Crop from About 20% of the Way Down

Post 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);
}

?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop from About 20% of the Way Down

Post 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?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Crop from About 20% of the Way Down

Post 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.
Danny
Posts: 6
Joined: 2011-04-17T22:04:30-07:00
Authentication code: 8675308
Location: New York

Re: Crop from About 20% of the Way Down

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Crop from About 20% of the Way Down

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Danny
Posts: 6
Joined: 2011-04-17T22:04:30-07:00
Authentication code: 8675308
Location: New York

Re: Crop from About 20% of the Way Down

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop from About 20% of the Way Down

Post 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";
Danny
Posts: 6
Joined: 2011-04-17T22:04:30-07:00
Authentication code: 8675308
Location: New York

Re: Crop from About 20% of the Way Down

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop from About 20% of the Way Down

Post 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";
Danny
Posts: 6
Joined: 2011-04-17T22:04:30-07:00
Authentication code: 8675308
Location: New York

Re: Crop from About 20% of the Way Down

Post by Danny »

what it did was this
http://d.pr/X5Vy
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop from About 20% of the Way Down

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Crop from About 20% of the Way Down

Post by anthony »

Add a space to the appended string! two arguments are being joined together.

$cmd .= " -gravity south -crop 100%x80%+0+0 +repage";
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop from About 20% of the Way Down

Post 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.
Post Reply