Serve smaller images in ACF image loop - php

I have a product overview page with 1 image per product:
https://axces.be/nl/product/
You can add multiple images to a products. All these images are shown on the detail page. But on the product page, you only see the first image.
My template looks like this:
<a href="<?php the_permalink(); ?>" class="single-prod">
<div class="single-prod__image">
<?php
$productImages = get_field('product_images');
?>
<img src="<?php echo $productImages[0]['product_image']?>" />
</div>
</a>
But now the images are served on the size they are uploaded. This causes a slower load time. So I want to serve smaller images.
I've tried to add the image size thumbnail to it, in order to see if the load time is better. But that doesn't work:
<a href="<?php the_permalink(); ?>" class="single-prod">
<div class="single-prod__image">
<?php
$image_size = 'thumbnail';
$productImages = get_field('product_images');
?>
<img src="<?php echo $productImages[0]['product_image']['sizes'][$image_size]; ?>" />
</div>
<div class="single-prod__information">
<span class="single-prod__name">
<?php the_title(); ?>
</span>
<span class="single-prod__short">
<?php the_field('product_short'); ?>
</span>
</div>
</a>
So eventually, I want to achieve that the images are smaller. Not necessary the to see the thumbnail because that's probably going to crop the image.
Thanks in advance.

Related

How to retrieve images after clicking thumbnail from a repeat region using Mysql and PHP

I need some help, I have a repeat region which displays pics from a database, when I click on the image I would like to go to another page where there is a photo gallery to display my pictures bigger.
My problem, no matter which thumb I click I get the same set of pictures from the database, which is usually the last thumb listed.
I am new to php, so i am wondering if anyone can help me, thank you.
Code for thumb page
`
<?php do { ?>
<div class="display-property-pic">
<input type="image" id="imageIn1" src="upload/images/<?php echo $row_repeatRegion['thumb']; ?>" alt="Submit" width="100" height="100" ><input name="picHid" type="hidden" value="<?php echo $row_repeatRegion['property_pic1']; ?>">
</div>
<?php } while ($row_repeatRegion = mysqli_fetch_assoc($repeatRegion)); ?>
`
Code for Gallery page
if(isset($_POST['picHid'])){
$pic = $_POST['picHid'];
}
mysqli_select_db($wiplisttest, $database_wiplisttest);
$query_getPics = "SELECT photos.photo_id, photos.pic1, photos.pic2, photos.pic3 FROM photos WHERE photos.property_pic1 LIKE '%$pic%'";
$getPics = mysqli_query($wiplisttest, $query_getPics) or die(mysql_error($wiplisttest));
$row_getPics = mysqli_fetch_assoc($getPics);
$totalRows_getPics = mysqli_num_rows($getPics);
<div class="album">
<div class="description">fixed images</div>
<ol>
<?php do { ?>
<li>
<h4><?php echo "Pic"." ".$row_getPics['pic3']; ?></h4>
<div class="description">Salzburg, Austria</div>
<a href="upload/images/<?php echo $row_getPics['pic3']; ?>">
<img src="upload/images/<?php echo $row_getPics['pic2']; ?>" />
</a>
</li>
<?php } while ($row_getPics = mysqli_fetch_assoc($getPics)); ?>
</ol>
</div>
As you have stored images info in database, there should be id assigned to each image, so when you click on any specific image, get id of that image and retrieve that image on another page using the id.

WordPress post image to show not the featured image

I have a site in WordPress, and I want to display the WordPress post first image which is displayed in the post but remember not the featured images. so how to show it in the post and i want my post like below
this is how blog design should be
This is the code
<div class="col-md-3 small-post-img">
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
It not taking the the_post_thumbnail code because there is no any featured image in the post, but I have added the images in the inside post content. I want that post images as featured image.
Put this in your theme's functions.php file
function ash_post_first_img($post_content){
preg_match_all('/<img[^>]+>/i', $post_content, $content_all_images);
preg_match_all('/src="([^"]+)"/i', $content_all_images[0][0], $content_image);
if (isset($content_image[1][0])) {
return $content_image[1][0];
} else{
return 'http://example.com/defaultimage.jpg';
}
}
and use this function ash_post_first_img(get_the_content('')); where you want to display image's url "single.php or post.php"
<div class="col-md-3 small-post-img">
<a href="<?php the_permalink() ?>">
<img src="<?php echo ash_post_first_img(get_the_content('')); ?>">
</a>
</div>
Change "http://example.com/defaultimage.jpg" to the default image url if no image is found.

Fotorama thumbnails creation dynamically using php

<div class="fotorama" data-width="700" data-ratio="700/467" data-max-width="100%" align="center" data-arrows="true" data-click="false" data-swipe="false" data-autoplay="2000" data-nav="thumbs">
<?php
$query="select * from released_movies" ;
$queryr=$con->query($query);
while($row=$queryr->fetch_assoc()) { ?>
<div>
<a href="single.php?id=<?php echo $row['rel_id'];?>">
<img u="image" src="../AbaamAdmin/Released_Movies/<?php echo $row['rel_movies_pics'];?>" />
</a>
</div>
<?php }?>
</div>
I need to create a fotorama gallery slideshow with thumbnails of the larger image. But by giving the above code, no image appears as thumbnail instead I got only a thumbnail border. How I will get the thumbnail image there dynamically. Thanks in advance.

Can I make the images in the product view of Magento have a set width, but a fluid height?

I'm developing a website for an art gallery so all of their product images have different widths and heights depending on the art piece. I want the image to display at 630px wide every time but depending on the image, extend longer down the page than others. Currently, I know that I can adjust the $bigImageX and $bigImageY of the product images from the media.php file located in
/app/design/frontend/default/theme706/template/ecommerceteam/cloud-zoom/catalog/product/view
but this is only allowing me to set a specific width and height. If I set only the height, it shrinks so that the width is the same as the height.
<div class="product-img-box">
<script type="text/javascript" src="http://citizen.bluelotuscreative.com/skin/frontend/default/theme706/js/klass.min.js"></script>
<script type="text/javascript" src="http://citizen.bluelotuscreative.com/skin/frontend/default/theme706/js/code.photoswipe.jquery-3.0.5.js"></script>
<div class="product-box-customs">
<p class="product-image">
<img src="http://citizen.bluelotuscreative.com/media/catalog/product/cache/1/image/630x630/9df78eab33525d08d6e5fb8d27136e95/placeholder/default/Citizen_Atelier_-_Ashley_Woodson_Bailey_2__2.jpg" alt="Test Product" title="Test Product" /> </p>
</div>
</div>
$bigImageX = 630;
$bigImageY = ???;
<div class="product-box-customs">
<?php if ($product->getImage() != 'no_selection' && $product->getImage()): ?>
<p class="product-image">
<a href='<?php echo $this->helper('catalog/image')->init($product, 'image')->resize($bigImageWidth, $bigImageHeight);?>' class = 'cloud-zoom' id='zoom1' rel="<?php echo implode($config, ',');?>">
<img class="big" src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize($bigImageX, $bigImageY);?>" alt='' title="<?php echo $this->htmlEscape($this->getImageLabel());?>" />
</a>
</p>
<?php else: ?>
<p class="product-image">
<?php
$_img = '<img src="'.$this->helper('catalog/image')->init($product, 'image')->resize($bigImageX, $bigImageY).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';
echo $outputHelper->productAttribute($product, $_img, 'image');
?>
</p>
Replace:
$this->helper('catalog/image')->init($product, 'image')->resize($bigImageWidth, $bigImageHeight);
With this:
$this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize($bigImageWidth,null);

Switching a Base image with a thumbnail in Magento

On a custom made product view page i'm working off from there is the base image (the large one) and a list of thumbnails which are other images associated with the product in the media gallery (they are just normal images and not the defined thumbnail), what i've been tasked to to is get it so that when you click on a thumbnail it'll change the base image above
i've got that working however i have a problem, when i change the image the image it changes to is very pixelated one, the base image is 737x578 originally so i understand that if the image is smaller it'll be stretched, however the images the thumbnails came from are roughly the same size as the original base image, it's just that they have been re-sized to be 48x48
looking at information in "view image info" in Firefox shows that the image's src is coming from the magento cache (media/catalog/product/cache/1/thumbnail/48x/9df78eab33525d08d6e5fb8d27136e95/images/) and not from the original file i have in the media folder
the base image is being created like this
<a class="product-image image-zoom" id="main-image" title="<?php echo $this->htmlEscape($_product->getImageLabel()); ?>" href="<?php echo $this->helper('catalog/image')->init($_product, 'image'); ?>">
<?php
$_img = '<img src="'.$this->helper('catalog/image')->init($_product, 'image')->resize(737, 578).'" width="737" height="578" alt="'.$this->htmlEscape($_product->getImageLabel()).'" title="'.$this->htmlEscape($_product->getImageLabel()).'" />';
echo $_helper->productAttribute($_product, $_img, 'image');
?>
</a>
while the thumbnails are being generated like this
<ul id="image-list">
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(48); ?>" width="48" height="48" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
</li>
<?php endforeach; ?>
</ul>
and this is the javascript i'm using to switch the images
jQuery(document).ready(function()
{
jQuery("#image-list li img").click(function()
{
jQuery("#main-image img").attr("src", jQuery(this).attr("src"));
});
});
what change would i need to make to my javascript in order to replace the base image with the original images used by the thumbnails, obviously just changing the src attribute in the tag isn't enough
When you click the thumbnail image, your jQuery is setting the src of the main image to the thumbnail image src (which is 48x48). A click on the thumbnail should set the main image to the large size of the thumbnail image.
So you need a way to reference the large image src from within the thumbnail image element. You can create an attribute called something like data-main-image-src inside the thumbnail image element so that you can reference that later in jQuery:
<ul id="image-list">
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<img data-main-image-src="<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(737, 578)?>" src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(48); ?>" width="48" height="48" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
</li>
<?php endforeach; ?>
</ul>
Then you would modify your jQuery like this so that you change the main image src to be the larger image:
jQuery(document).ready(function()
{
jQuery("#image-list li img").click(function()
{
jQuery("#main-image img").attr("src", jQuery(this).attr("data-main-image-src"));
});
});
Manishie's answer almost worked for me, I guess with version 1.9 of Magento things might be a little different. I've updated the PHP as follows:
<ul class="product-image-thumbs">
<?php $i=0; foreach ($this->getGalleryImages() as $_image): ?>
<?php if ($this->isGalleryImageVisible($_image)): ?>
<li>
<img data-main-image-src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'large', $_image->getFile())->resize(560, 393)?>" src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(75); ?>" width="75" height="75" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
</li>
<?php endif; ?>
<?php $i++; endforeach; ?>
</ul>
The main change is in how I've called data-main-image-src For some reason Manishie's version was calling in the src of the current main img for every thumb. Instead I've used the same method he used to call in the thumbs but called the large image instead:
data-main-image-src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'large', $_image->getFile())->resize(560, 393)?>"
The jQuery was fine I just had to update the classes being targeted:
$j(".product-image-thumbs li img").click(function(){
$j("#image-main").attr("src", $j(this).attr("data-main-image-src"));
});

Categories