WordPress.org

Ready to get started?Download WordPress

Forums

Upload image with the same canvas size (4 posts)

  1. dmitrym
    Member
    Posted 2 years ago #

    Hey guys, could someone hint me the way or plugin to make uploaded images to have the same canvas size. This should be done at uploading stage.
    The image should be resized to fit the maximum sizes but without cropping and the rest of the canvas should be filled with white background in order the full image stay the exact sizes I need.
    Hope the explanation is clear. Is there a way to do this?

  2. dmitrym
    Member
    Posted 2 years ago #

    Seems I have found the solution. It is not really good as I modified the core wordpress files. So the solution is as the following:
    find the file wp-includes/media.php and find the function "image_resize" inside this function there is a line of code:

    $newimage = wp_imagecreatetruecolor( $dst_w, $dst_h );

    This line creates a new blank image where the cropped and resized uploaded image will be placed. Replaced this line with the following line:

    $newimage = wp_imagecreatetruecolor( $max_w, $max_h );

    This will create the blank image with exact required thumbnail sizes.
    The created canvas size will be black colored. In order to change the canvas color... In the same file find the function

    function wp_imagecreatetruecolor($width, $height) {
    	$img = imagecreatetruecolor($width, $height);
    	if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
    		imagealphablending($img, false);
    		imagesavealpha($img, true);
    	}
    	return $img;
    }

    and add two lines in order function become like the following:

    function wp_imagecreatetruecolor($width, $height) {
    	$img = imagecreatetruecolor($width, $height);
    	if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
    		imagealphablending($img, false);
    		imagesavealpha($img, true);
    	}
    $white = imagecolorallocate($img, 255, 255, 255);
    	imagefill($img, 0, 0, $white);
    	return $img;
    }
  3. noirode
    Member
    Posted 1 year ago #

    Hi,
    I have the same problem but I would rather not touch the WordPress Core files if possible.
    Does anyone knows about a plugin or a javascript that will do that?
    Thanks

  4. igloobob
    Member
    Posted 1 year ago #

    I'd be very interested if there was a plugin that could do this!

Topic Closed

This topic has been closed to new replies.

About this Topic