WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] gallery customization: crop thumbs from bottom (4 posts)

  1. kath
    Member
    Posted 5 years ago #

    Hi everyone,

    I've been trying to figure this out but I don't know enough php. I'm working on a design portfolio, and I want to crop the thumbnials in my gallery from the bottom, rather than preserving the middle. I'd prefer to be able to decide this sort of thing for each image (how to crop), but since this site is going to be only a design portfolio, it doesn't really matter if I need to change this for everything.

    I started looking in wp-includes/media.php, and here's what I found:

    // calculate dimensions and coordinates for a resized image that fits within a specified width and height
    // if $crop is true, the largest matching central portion of the image will be cropped out and resized to the required size
    function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false) {
    
    	if ($orig_w <= 0 || $orig_h <= 0)
    		return false;
    	// at least one of dest_w or dest_h must be specific
    	if ($dest_w <= 0 && $dest_h <= 0)
    		return false;
    
    	if ( $crop ) {
    		// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
    		$aspect_ratio = $orig_w / $orig_h;
    		$new_w = min($dest_w, $orig_w);
    		$new_h = min($dest_h, $orig_h);
    		if (!$new_w) {
    			$new_w = intval($new_h * $aspect_ratio);
    		}
    		if (!$new_h) {
    			$new_h = intval($new_w / $aspect_ratio);
    		}
    
    		$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
    
    		$crop_w = ceil($new_w / $size_ratio);
    		$crop_h = ceil($new_h / $size_ratio);
    
    		$s_x = floor(($orig_w - $crop_w)/2);
    		$s_y = floor(($orig_h - $crop_h)/2);
    	}
    	else {
    		// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
    		$crop_w = $orig_w;
    		$crop_h = $orig_h;
    
    		$s_x = 0;
    		$s_y = 0;
    
    		list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
    	}
    
    	// if the resulting image would be the same size or larger we don't want to resize it
    	if ($new_w >= $orig_w && $new_h >= $orig_h)
    		return false;
    
    	// the return array matches the parameters to imagecopyresampled()
    	// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
    	return array(0, 0, $s_x, $s_y, $new_w, $new_h, $crop_w, $crop_h);
    
    }

    And I think the important part is:

    if ( $crop ) {
    		// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
    		$aspect_ratio = $orig_w / $orig_h;
    		$new_w = min($dest_w, $orig_w);
    		$new_h = min($dest_h, $orig_h);
    		if (!$new_w) {
    			$new_w = intval($new_h * $aspect_ratio);
    		}
    		if (!$new_h) {
    			$new_h = intval($new_w / $aspect_ratio);
    		}
    
    		$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
    
    		$crop_w = ceil($new_w / $size_ratio);
    		$crop_h = ceil($new_h / $size_ratio);
    
    		$s_x = floor(($orig_w - $crop_w)/2);
    		$s_y = floor(($orig_h - $crop_h)/2);
    	}

    But I'm not sure exactly how to change the code because I don't know eactly what parts of this code determines what is cut from where. I'll only need to change something in $crop_h and %s_y, but again, I'm not sure how. I want the amoutn cropped from the ceiling / top to be 0 and from the floor to be the total height -- I could try just deleting the "/2" but I don't know where the top crop is done so I'm worried that would end up in cropping more image than I have.

    Any help is much appreciated!

    Kath

  2. kath
    Member
    Posted 5 years ago #

    After more reading and thinking, I have figured this out more. The part of the code that actually does the cropping is:
    imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);

    ... in the media.php file I was talking about in my previous post. According to http://ca3.php.net/imagecopyresampled :

    In other words, imagecopyresampled() will take an rectangular area from src_image of width src_w and height src_h at position (src_x ,src_y ) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x ,dst_y ).

    ... So I still want it to be centered horizontally at this point, but I want it to crop from the top of the images, so I will change $scr_y in media.php to 0 and we'll see what we get ...

    I tried it and I can't tell if it's worked yet, but I didn't ruin anything. I will update when I can figure it out. I would prefer to keep the variable and write some sort of plugin that will let me choose how to create the thumbnails per image ... but that's probably beyond me.

  3. kath
    Member
    Posted 5 years ago #

    Update: Yes, that worked.

  4. grollaz
    Member
    Posted 4 years ago #

    Hi, can you post the edited working code?
    Is working with wp 2.8.6?
    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic