Resize in convert ?

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

Post by Bonzo »

Code: Select all

<? 
if ( $Submit ) { 

// Display what is being posted 
echo "<br> filename = ".$filename; 
echo "<br> HTTP_POST_VARS filename = ".$HTTP_POST_VARS["filename"]; 
echo "<br> _POST filename = ".$_POST["filename"]; 

// Temporary upload image name 
$original_image = $_FILES['filename']['tmp_name']; 
// Display what original_image contains 
echo "<br> original_image = ".$original_image; 

// Get the image dimensions 
$size=GetImageSize( $original_image ); 

// Name to save the image as - in this case the same as the original 
$new_image = $_FILES['filename']['name']; 
echo "<br> new_image = ".$new_image; 

if ( $size[0] > 794 || $size[1] > 564 )
{ exec("/usr/local/bin/convert $original_image -resize 794x564 -segment 1x1 +dither -colors 2 -edge 1 -normalize -negate $new_image"); }

else
{exec("/usr/local/bin/convert $original_image -segment 1x1 +dither -colors 2 -edge 1 -normalize -negate $new_image"); }

echo "File uploaded"; 
echo "<img src=\"".$new_image."\">"; 

} 
?>
This is untested but should work ( possibly with a bit of modification ! )

Code: Select all

<? 
if ( $Submit ) { 

// Display what is being posted 
echo "<br> filename = ".$filename; 
echo "<br> HTTP_POST_VARS filename = ".$HTTP_POST_VARS["filename"]; 
echo "<br> _POST filename = ".$_POST["filename"]; 

// Temporary upload image name 
$original_image = $_FILES['filename']['tmp_name']; 
// Display what original_image contains 
echo "<br> original_image = ".$original_image; 

// Get the image dimensions 
$size=GetImageSize( $original_image ); 

// Name to save the image as - in this case the same as the original 
$new_image = $_FILES['filename']['name']; 
echo "<br> new_image = ".$new_image; 

exec("/usr/local/bin/convert $original_image -resize 794x564> -segment 1x1 +dither -colors 2 -edge 1 -normalize -negate $new_image"); 

echo "File uploaded"; 
echo "<img src=\"".$new_image."\">"; 

} 
?>
Or something like this in Imagemagick only.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Pass a '>' flag as part of the size to resize. it will only resize images that are larger than the requested size.

See IM Examples, Resize for a demostration using commandline.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

Thats strange; according to the Imagemagick instructions if you use 794x564> it should only resize the image if it is larger than 794x564. I have just tried it and created an image of 0kb !

I would say use my first example where it checks the size with getimagesize and the uses the if else function.

Note: I think I may have found the problem, try changing this line :
exec("/usr/local/bin/convert $original_image -resize '794x564>' -segment 1x1 +dither -colors 2 -edge 1 -normalize -negate $new_image");

-resize 794x564> is now -resize '794x564>'
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Not unless you add a '!' to the resize the images aspect ration is preserved, just as you want. That is the final image will fit into the requested box, NOT fill the requested box.

See IM Examples. Resize examples.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply