i can resize the images using imagemagick with

                $img="$img";
                $path="/usr/local/bin/convert";		// The path to the Imagemagic convert program.
                $size="100x100";					// The size of the thumbnail to create.
                $docroot="/home/user/public_html";		// The relative path of your server's web root. No trailing slash!
                $imagepath="/images";				// The path where your images are present. No trailing slash!
                $thumbpath="/thumbnails";		// The path where the thumbnails are created. No trailing slash!
                if (!file_exists('$thumbpath/$img'))
                {
                $command="$path -size $size $docroot/$imagepath/$img -resize $size +profile \"*\" $docroot/$thumbpath/$img";
                exec($command);
                }

i want to create 100x100 image with a background color white and center the resized image.
Since i am using gif images also i need to stay with imagemagick.
Any ideas?

    i did the same on my homepage. only difference: i crop the to 100x100 instead of center them in the box. anyway, in both cases you have to do some calclulations.

    first you have to calculate how large (width & height) the resized image will be. most likely you will resize it in a way that will match either width or height to 100 pixel. An example:

    Assuming you have a 300x200 picture.
    resizingFactor = 320/100 (=0.32)
    height = 200x0.32 (=64)
    offsetY= (100-64)/2 (=18)
    resizedImage = 100x64

    Now you know you have to output the image 18 pixels away from the top border of your 100x100 thumbnail. Of course you have to handle all possible situations (width is larger than height, height is larger than width, width and height are equal, image is smaller than 100x100 etc.)

      in your exampleof 300x200 picture, without calculating anything with my code imagemagick will resize it to 100x64 since mys max widht =100 height=100.
      The problem i have is that i dont know how to create 100x100 blank image and insert the resized image inside of that, i can do this with GD, but not with imagemagick

        yes... it does resize automatically, but it does not center it automatically, so you have to calculate the offset manually (at least it haven't in the IM version that i've used).

        if i remeber right you can use the -geometry option of IM to do what you want. check the IM manual for further information [http://www.imagemagick.org/www/utilities.html#opti]

          9 days later

          can someone explain how ImageMagick works? Does this create the thumbnail images when the original image is uploaded to the site or does it create the thumbnails on the site when a page is pulled up calling for them?

          I built a page that allows users to upload hi-rez images to a site, but I want a thumbnail to be created at the same time or however ImageMagick works. I've tried finding a manual explaining this on a few sites but have had no luck.

          Thanks.

            What about if i don't have the file in the web space as a jpg file and i have the image into a blob field in a database?

            How i can resize the image?

              to f8ball: imagine imagemagick as a command line photoshop =) IM is quite powerful (you should take a look at imagemagick.org to see the featurelist).

              in order to resize an image you have to upload it first to your server. then you can call imagemagick fom php (with the system command) and do whatever you want with your picture-file. after that you move the transformed image into the web-space of your server.

              to tatoadsl: before you resize the image you have to save your blob as a normal file on the filesystem of your server.

                2 months later

                So, if the following command is used, how would it be changed to make sure the output is exactly 100x100?

                $command="$path -size $size $docroot/$imagepath/$img -resize $size +profile \"*\" $docroot/$thumbpath/$img";

                Thanks in advance.

                  Here's the snippet I'm using now:

                  PHP:

                   $command = "$mogrify -crop 80x80 $resized"; 
                        passthru($command);   

                  Now I get an image 80x80 but a whole bunch of them...imgname.jpg.0, imgname.jpg.1, imgname.jpg.2, etc...

                  I read that can be prevented by adding the measure from the top left corner but I can't get that to work.

                  I'd like to crop the image to 80x80 out from the center of the picture.

                  How do I do that?

                  Thanks

                    i did the cropping on my own - that means: calculate the resize in PHP and then decide which area you have to cut off from your image. here's the function that i wrote for that, maybe you find it useful.

                    	//------------------------------------------------------------
                    	// FUNCTION resize
                    	//------------------------------------------------------------
                    	/**
                    	 * @iFile - Die Input Bilddatei
                    	 * @oFile - Die Output Bilddatei
                    	 * @ox, oy - Grösse der Output Bilddatei
                    	 * @crop - Wenn true, wird Bild beschnitten. Wenn false ist ein
                    	 * Rand sichtbar.
                    	 * @bw - borderWidth, Pixelbreite des Rahmens, wenn 0: kein Rahmen.
                    	 */
                    	function resize($iFile,$oFile,$oX,$oY,$crop, $bW=0 )
                    	{
                    		// ImageMagick Command Strings
                    		$cStr = "";		// crop 
                    		$gStr = ""; 	// geometry 
                    		$qStr = "";		// quality 
                    		$bStr = ""; 	// border 
                    
                    	// Image border
                    	$bColor	= "#cccccc";
                    
                    	// Get information on input
                    	$info = $this->getInfo($iFile);
                    	if($info == false){
                    		return false;
                    	}
                    	$iType 	= $info[type];
                    	$iX		= $info[x];
                    	$iY		= $info[y];
                    
                    	// Fit image into border
                    	if($bW > 0){
                    
                    		if($oX > 0){ $oX -= 2*$bW; }
                    		if($oY > 0){ $oY -= 2*$bW; }
                    		// create BORDER command
                    		$bStr = '-bordercolor "'.$bColor.'" -border '.$bW.'x'.$bW;
                    	}
                    
                    	// Extract extension from output-file
                    	$pInfo = pathinfo($oFile);
                    	$oType = strtolower($pInfo[extension]);
                    
                    	// Shrink to Image Size if smaller than output size
                    	if($iX < $oX){ $oX = $iX; }
                    	if($iY < $oY){ $oY = $iY; }
                    
                    	$nX = $oX;
                    	$nY = $oY;
                    
                    	// scaled x,y
                    	if($oX > 0){ $sY = ceil($iY/($iX/$oX)); }
                    	if($oY > 0){ $sX = ceil($iX/($iY/$oY)); }
                    	$sMode = "No Scaling";
                    
                    	// Choose type of scaling, cropping
                    	if($oX > 0 && $oY == 0){
                    
                    		// Scale Y Only
                    		$sMode = "Scale Y only";
                    		$nX = $oX;
                    		$nY = $sY;
                    	}
                    	else if($oX == 0 && $oY > 0){
                    
                    		// Scale X Only
                    		$sMode = "Scale X only";
                    		$nX = $sX;
                    		$nY = $oY;
                    	}
                    	else if($oX > 0 && $oY > 0){
                    
                    		// Scale both
                    		if($crop){
                    
                    			// Scale and Crop
                    			// Choose scale with less crop area
                    			if($sY < $oY){
                    
                    				// Crop X
                    				$sMode = "Scale Both, Crop X";
                    				$nX = $sX;
                    				$nY = $oY;
                    				$cX = ceil(($sX-$oX)/2);
                    				$cY = 0;
                    			}
                    			else{
                    
                    				// Crop Y
                    				$sMode = "Scale Both, Crop Y";
                    				$nX = $oX;
                    				$nY = $sY;
                    				$cX = 0;
                    				$cY = ceil(($sY-$oY)/2);
                    			}
                    
                    			// Create CROP command
                    			$cStr = "-crop ".$oX."x".$oY."+$cX+$cY";
                    		}
                    		else{
                    
                    			// Scale both, No crop
                    			$sMode = "Scale Both, No Crop";
                    			$nX = $oX;
                    			$nY = $sY;
                    
                    			if($nY > $oY){
                    
                    				// Rescale to fit size
                    				$sMode = "Scale Both (resized), No Crop ";
                    				$nX = ceil($nX/($nY/$oY));
                    				$nY = $oY;
                    			}
                    		}
                    	}
                    
                    	// Create GEOMETRY command
                    	$gStr = "-geometry ".$nX."x".$nY;
                    
                    	// Create QUALITY command for specified type
                    	// Check if quality is set
                    	if( !strlen($this->quality) )
                    	{
                    		// Default settings
                    		switch($oType){
                    
                    			case "jpg":
                    			case "jpeg":
                    				$qStr = "-quality 50";
                    			break;
                    
                    			case "png":
                    				$qStr = "-quality 1 -colors 16";
                    			break;
                    		}
                    	}
                    	else
                    		$qStr = $this->quality;
                    
                    	// Create ImageMagick Execution String
                    	$execStr = IMAGEMAGICK_DIR."convert $gStr $cStr $bStr $qStr $iFile +profile \"*\" $oFile";
                    
                    	// create thumbnail image
                    	exec( $execStr, $systemOutput, $systemReturn );
                    
                    	// return report on success
                    	if( $systemReturn != 0 )
                    	{
                    		return false;
                    	}
                    	else
                    	{
                    		$pInfo = pathinfo($iFile);
                    		$report[Input_File]		= $pInfo['basename'];
                    		$report[Input_Type] 	= $iType;
                    		$report[Input_Size] 	= "$iX x $iY";
                    
                    		$report[Scale_Mode]		= $sMode;
                    
                    		$pInfo = pathinfo($oFile);
                    		$report[Output_File]	= $pInfo[basename];
                    		$report[Output_Type] 	= $oType;
                    
                    		$fInfo = $this->getInfo($oFile);
                    		$report[Output_Width] 	= $fInfo[x];
                    		$report[Output_Height] 	= $fInfo[y];
                    		$report[Output_Crop]	= "Left:$cX Top:$cY";
                    		return $report;
                    	}
                    }
                    
                      Write a Reply...