-
Notifications
You must be signed in to change notification settings - Fork 205
Examples
Maxim Zubarev edited this page May 2, 2013
·
7 revisions
Put this in the single.php file
<?php
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
$image = aq_resize( $img_url, 560, 310, true ); //resize & crop the image
?>
<article <?php post_class()?> id="post-<?php the_ID(); ?>">
<?php if($image) : ?>
<img src="<?php echo $image ?>"/>
<?php endif; ?>
....
If you want to output a gallery, or a slider from a post. Let's say you're using this in a file called format-gallery.php
<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'orderby' => 'menu_order',
'numberposts' => -1,
);
$attachments = get_posts($args);
?>
<div id="slider">
<?php
if ($attachments) {
foreach ($attachments as $attachment) {
$attachment_url = wp_get_attachment_url($attachment->ID , 'full');
$image = aq_resize($attachment_url, 600, 350, false); //resize & retain image proportions (soft crop)
echo '<div class="slide"><img src="'.$image.'"/></div>';
}
}
?>
</div>
On some occasions, you may want to get width & height of the image. This is mostly the case for image types like logo, or if you're using the older non-HTML5 markup. In this example, let's say you're using an image uploaded from SMOF
<?php
$logo_img = $data['logo_img']; //get the original logo url
$logo_w = $data['logo_width']; ////
$logo_h = $data['logo_height']; // user defined width & height
$crop = false; //resize but retain proportions
$single = false; //return array
$logo = aq_resize($logo, $logo_w, $logo_h, $crop, $single);
?>
<?php if($logo) : ?>
<div id="logo" style="width:<?php echo $logo[1] ?>px;height:<?php echo $logo[2] ?>px">
<img src="<?php echo $logo[0] ?>" width="<?php echo $logo[1] ?>" height="<?php echo $logo[2] ?>"/>
</div>
<?php endif; ?>
It desirable (especially in the crop mode) to get only the image with the size You specified or no image at all and then react on that fact.
<?php
$original_image = $data['original_image']; // let's assume this image has the size 100x100px
$width = 110; // note, how this exceeds the original image size
$height = 90; // some pixel less than the original
$crop = true; // if this would be false, You would get a 90x90px image. For users of prior
// Aqua Resizer users, You would have get a 100x90 image here with $crop = true
$new_image = aq_resize($original_image, $logo_w, $logo_h, $crop);
// here comes the cool part: $new_image will be false in this specific example because of $width
if($new_image !== false) {
// do whatever You wanted to do with that image
echo '<img src="' . $new_image . '" alt="" />";
} else {
// You didn't get the size You wanted. Now see, what You want to do. For example, try another
// size or something...
}
?>