Support » Plugins » [Plugin: User Photo] Cropping user photos in square thumbnails

  • Resolved tessina

    (@tessina)



    I needed this too and so edited some code in the plugin. Thought I’d share with others looking for such a solution too.

    In the user-photo.php file at line 852, comment out this whole chunk of code:

    // figure out the longest side
    		if ( $info[0] > $info[1] ) {
    			$image_width = $info[0];
    			$image_height = $info[1];
    			$image_new_width = $maxdimension;
    
    			$image_ratio = $image_width / $image_new_width;
    			$image_new_height = $image_height / $image_ratio;
    			//width is > height
    		} else {
    			$image_width = $info[0];
    			$image_height = $info[1];
    			$image_new_height = $maxdimension;
    			$image_ratio = $image_height / $image_new_height;
    			$image_new_width = $image_width / $image_ratio;
    			//height > width
    		}
    
    		$imageresized = imagecreatetruecolor( $image_new_width, $image_new_height);
    		@ imagecopyresampled( $imageresized, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $info[0], $info[1] );

    In its place, put this code instead:

    // cropping into square thumbnails
    		$new_w = $maxdimension;
    		$new_h = $maxdimension;
    		$orig_w = $info[0];
    		$orig_h = $info[1];
    
    		$w_ratio = ($new_w / $orig_w);
    		$h_ratio = ($new_h / $orig_h);
    
    		if ($orig_w > $orig_h ) {//landscape
    	   		$crop_w = round($orig_w * $h_ratio);
    	   		$crop_h = $new_h;
    		}
    		elseif ($orig_w < $orig_h ) {//portrait
    			$crop_h = round($orig_h * $w_ratio);
       			$crop_w = $new_w;
    		}
    		else {//square
    	    	$crop_w = $new_w;
    	    	$crop_h = $new_h;
    		}
    
    		$imageresized = imagecreatetruecolor( $new_w, $new_h);
    		@ imagecopyresampled( $imageresized, $image, 0, 0, 0, 0, $crop_w, $crop_h, $info[0], $info[1] );

    Reference:
    http://return-true.com/2009/02/making-cropping-thumbnails-square-using-php-gd/

  • The topic ‘[Plugin: User Photo] Cropping user photos in square thumbnails’ is closed to new replies.