crop in add_image_size() wordpress - php

Why does crop array in add_image_size() does not work properly unless the featured image is again uploaded?
Like here is the code
<?php add_image_size('banner-image', 920, 210, array('center','center')); ?>
This works perfectly for the first time but when I change the above code to
<?php add_image_size('banner-image', 920, 210, array('left','top')); ?>
It does not work on normal or hard refresh but image is cropped to left top when the featured image is again uploaded.
Any solution for this without cropping the image when uploading...

Related

Image gets distorted along the edges after using imagerotate function

I am using imagerotate() function to rotate the images according to the orientation.
The following image is rotated via $realignedImage=imagerotate($temp,-90,0);
This is the result of the rotation

Cropping into an image in wordpress

Is it possible without messing with core to get WP to crop 'into' an image. So for example I have an image which is 800 x 800 and I would like it to crop it to 400 x400 by cropping top / bottom and left/right, in effect losing 200px off each side of the image.
In this example image. The cropping I am trying to achieve would give me a close-up of the mountains and part of the sun only.
Hoping not to reply on a plugin.Thnx
i think this function will help you.
<?php add_image_size( $name, $width, $height, $crop ); ?>

Wordpress select image size as PHP Variable

Wordpress creates a new image URL for each of the sizes of images it creates when an image is uploaded. It adds the image size in pixels to the end of the string before the image extension. The problem is that if say an image size for a medium image is 300x300, wordpress may crop it to say 300x180 to keep it in dimension.
This makes it difficult when programatically fetching a larger version of an image thumbnail as the file ending -300x300.jpg may not exist.
Is there a way of fetching the image by using the URL for the full image, with a PHP variable eg. ?size=medium on the end that will load the correct version. I'm sure I have done this with older versions of wordpress but cannot find any documentation to prove this.
From the documentation:
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
$size
Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32)
https://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
Add this to functions PHP so that the line above works for the first image if the post does not have a featured image.
function set_first_as_featured($attachment_ID){
$post_ID = get_post($attachment_ID)->post_parent;
if(!has_post_thumbnail($post_ID)){
set_post_thumbnail($post_ID, $attachment_ID);
}
}
add_action('add_attachment', 'set_first_as_featured');
add_action('edit_attachment', 'set_first_as_featured');
You can register a image size to use anywhere in your theme with add_image_size, see:
add_image_size( "your-custom-image-size", 300, 300, true );
then you can use in your theme as well:
the_post_thumbnail("your-custom-image-size", $attr);

WordPress Post Thumbnail Resizing Issues

I have recently developed a website in WordPress, I have a few thumbnail sizes defined by add_image_size();
For example if you look here: http://bit.ly/kSTU0Q
Images in the right hand channel at the very bottom under 'MORE PRODUCT NEWS' we have this defined for
the_post_thumbnail()
add_image_size( 'side-excerpt', 86, 93);
So would be:
the_post_thumbnail( 'side-excerpt',
array('class' => 'alignleft') );
If you look WordPress does not seem to adhere to my sizing specifications and I have used the 'Regenerate Thumbnails' plugin and this seems to make near as no difference.
Hopefully you guys can shed some light on the situation.
Thanks in advanced!
Looks to me like your issue is with your crop style.
add_image_size has a third property that is crop style and defaults to false (soft proportional cropping.
"Box resizing shrinks an image proportionally (that is, without distorting it), until it fits inside the “box” you’ve specified with your width and height parameters."
The reason that your regenerate thumbnails is not working correctly, is because you are using this soft mode, and it seems like the result you are actually looking for is hard crop.
"in this mode, the image is cropped to match the target aspect ratio, and is then shrunk to fit in the specified dimensions exactly."
so you will want to change
add_image_size( 'side-excerpt', 86, 93);
to
add_image_size( 'side-excerpt', 86, 93, true);
For more information see:
http://markjaquith.wordpress.com/2009/12/23/new-in-wordpress-2-9-post-thumbnail-images/
http://codex.wordpress.org/Function_Reference/add_image_size

WordPress Post Thumbnail Problem

I have two types of posts in the theme I am creating Trending and Normal. Post thumbnail image size is: 300x169 and I show it as that size when a trending post is displayed. But when this post is not trending I want the thumbnail size to be: 145x80. I tried the_post_thumbnail( array(145,80) ); but it doesn't work. Instead this crops the image in squarish dimensions. I don't want it to crop but decrease in size via HTML.
Can anyone please help me with this.
Thanks! Appreciate all the help.
You could try adding a new image size and then when you call the_post_thumbnail you pass it the name of the new image size you have set. Set the hardcrop flag to false if you don't wan't the image resizing via hard crop.
http://codex.wordpress.org/Function_Reference/add_image_size
You can change the size from CSS by doing this:
<div style="width:145px;height:80px;"><?php the_post_thumbnail();?></div>

Categories