So I know WordPress uploads different image sizes every time you upload an image.
So for example I upload an image called background_main.jpg and in my uploads folder 'll have :
background_main-1024x512.jpg
background_main-150x150.jpg
background_main-300x150.jpg
background_main-768x384.jpg
So currently to pull out an image I handle this like:
$classImg = wp_get_attachment_url( get_post_thumbnail_id($key->ID) );
Then I can spit it out by just using $classImg.
How can I pull out the same image but a smaller size for instance the 150x150 image or the 300x300 image?
Thanks!
If you take a look at API, there is a function:
wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
'large' can be:
large
medium
full
thumbnail
array ( n, m ) where n and m are pixels
xyz where 'xyz' is custom defined size
Related
I got products from product and product_description tables. I also found image column in product table. But I can't get resized images? How can I?
Thanks in Advance..
Resized images are stored in image/cache/ folder.
Resized images are created only when resize method is called. Like in catalog/controller/product/product.php you can see following code which will resize the main image.
$this->load->model('tool/image');
if ($product_info['image']) {
$data['popup'] = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
} else {
$data['popup'] = '';
}
Here the resize method takes database product's table image field and height and width given in setting of admin.
This will create resized image at image/cache/"IF ANY FOLDER"/ and the name of the image will be like "NAME_OF_THE_IMAGE"-width*height."EXTENSION_OF_IMAGE"
For eg:
If you product table's image column holds catalog/demo/imac.jpg then you resize it with width=100 and height=100 then it will be cache/catalog/demo/imac-100*100.jpg
Take into consideration that resized images are created only when you load the controller, once it is created it will be there.
So taking your problem into consideration, you have to recreate your image urls as described above and create resized images(only if you need images also).
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);
I need to three size of images uploads for display in my page: 1-large 2-medium 3-small now i have Two choices and two way .
1 - when upload images, PHP GD Library generate three size of images and put in any folder : example : in /images/ folder
test-larg.jpg
test-medium.jpg
test-small.jpg
And Display :
<img src="/images/test-small.jpg" alt="">
2 - i upload original images and put in any folder. then, for each size (large/medium/small) generate image using GD library from images. example : TimThumbs / phpthumbs() etc...
method :
<img src="/scripts/timthumb.php?src=/images/whatever.jpg&h=150&w=150&zc=1" alt="">
better way ? better choices ? Thanks.
My recommendation is to combine the two approaches:
On image upload, do nothing (apart from maybe adding the image metadata to a databse if so desired)
On a call to "thumbnail.php?src=/images/whatever.jpg&h=150&w=150&foo=bar" see, if you already have this image in this size - if yes, give it back, if no create it and store it as a file
This means,
you only create images you really need, thus saving CPU and storage
you have to clean up your cache, if you delete/replace an image
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
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>