Yii2 how to display images saved in web folder - image

I have images saved in /web/uploads/PROJECT in Yii2. How do i display them on page? below is my codes but its broken link image.
Controller:
$img = Yii::$app->request->baseUrl.'/web/uploads/PROJECT/';
$image = Html::img($img.$img_obj['AVATAR'],["width"=>"600px"]);
View:
echo $data;
Do i have to set any rights to display it? Right-clicking and opening image in new tab shows the url "/web/uploads/PROJECT/3fioobapJ5vRk_wdCEzDJbQWyO66inWO.jpg" which seems to be correct but page displays "Not Found (#404)"
Can anyone kindly advise? Thanks!

Strangely, using the below codes displays the images correctly.
$img = Url::to('#web/uploads/PROJECT/').$img_obj['AVATAR'];
$image = '<img src="'.$img.'" width="600" />';
<img src="<?= Yii::$app->request->baseUrl . '/backend/web/uploads/' . $model->profile_photo ?>" class=" img-responsive" >
<?php echo Html::img('#web/img/icon.png', ['class' => 'pull-left img-responsive']); ?>

The static images should be placed inside web/any_folder_name
to access in YII 2:
<?= Html::img('#web/any_folder_name/bg1.jpg', ['alt'=>'some', 'class'=>'thing']);?>

Add this code in your Grid View:
[
'attribute'=>'photo',
'value' => Html::a(Html::img(Yii::getAlias('#web').'/image/'.$st_data->photo, ['alt'=>'some', 'class'=>'thing', 'height'=>'100px', 'width'=>'100px']), ['site/zoom']),
'format' => ['raw'],
],

Related

Magento Custom Image Attribute from Related Products

So from the product view i am getting related products to display. For reasons i wont get into i need to display the image of the related product but it has to be a custom image attribute. So i have created a custom image attribute called colour_swatch and i need to access it and display it on screen. I have done some reading on the issue and have not managed to find a solution to fit my needs.
So here is my code:
<?php foreach($this->getItems() as $_item): ?>
<div class="eachProductRelated">
<a href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_item, 'colour_swatch') ?>"
width="20px" alt="<?php echo $this->htmlEscape($_item->getName()) ?>"/>
</a>
</div>
<?php endforeach ?>
If we replace colour_swatch with small image that of course will work. I have read that you can't access a custom image attribute if you aren't calling it directly from the product view. And a lot of the solutions i have seen seem to be massively overkill, Surely there is a simpler solution???
Might be worth telling you that i get the following error message:
a:5:{i:0;s:25:"Image file was not found.";i:1;s:4015:"#0
/Users/Frank/Sites/bosideng/app/code/core/Mage/Catalog/Helper/Image.php(163):
Mage_Catalog_Model_Product_Image->setBaseFile(NULL)
Is it possible that i have to do some kind of if no_selected check ?
I have figured out this issue and pardon my French but it was a ridiculous and a stupid oversight by Magento.
When setting up your new custom image attribute you must set 'Use In Product Listing' to yes in the drop down before you select the attribute type 'Media Image' because guess what!? When you select Media Image, Magento hides that option.
Credit to this guy whose post i found after ages of searching: http://thedistance.co.uk/journal/2011-10/missing-magento-media-images
Hello i think you add new image url in custom image attribute then call may be help you
<img src="<?php echo Mage::getModel('catalog/product')->load($product)->getData('colour_swatch'); ?>"
For those of you creating custom image type using installer, add the following to make image visible on both listing and view pages:
$this->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'colour_swatch', array(
'group' => 'Images',
'type' => 'varchar',
'frontend' => 'catalog/product_attribute_frontend_image',
'label' => 'Color Swatch',
'input' => 'media_image',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'default' => '',
'used_in_product_listing' => true,
));
If Flat Catalog Category and Use Flat Catalog Product is set to Yes, then you can add the specific attributes to the collection
Like
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToSelect('*');
$collection->addAttributeToSelect('attributes_name');
This is working perfect for me

Carousel with dynamic content

I came across a carousel from Twitter bootstrap here. http://twitter.github.com/bootstrap/javascript.html#carousel
It looks really good. But all the images / content in the example are hard-coded. How can I request the server for new content and show it in the carousel?
The answer would have to be adjusted for each language, but assuming you're programing in PHP, it may look something like this:
If you have an array of images:
$images = ['path/to/image.jpg', 'path/to/image2.jpg', 'path/to/image3.jpg']
Then you'd loop through those images, and place each one in the appropriate HTML markup, like so:
<?php foreach ($images as $image) { ?>
<div class="item">
<img src="<?php echo $image ?>" />
</div>
<?php } ?>

Printing an image to the bottom of each page using TCPDF?

is there a way to print an image to the bottom of each page? Some sort of footer alternative so to say. My image code looks like that right now:
'<p><img src="' . $footerImage . '" /></p>';
Is that possible? If not, is it possible to print an image to the tcpdf footer?
Thanks!
If your using Php you'd echo out $footerImage and have a separate php included file with $footerImage pointed to the absolute footer image location. So for example:
home.php:
<?php include('vd.php'); ?>
<p><img src="<?php echo $footerImage; ?>" /></p>
vd.php:
<?php $footerImage = 'app/view/img1.html'; ?>
Is this of any help?

Wordpress: show featured image with subcategory

I'm using this to generate a list of sub-pages that are a parent of 10 and images:
<ul>
<?php wp_list_pages('title_li=&child_of=10&link_after=<img src="http://mydomain.com/image.gif" alt="" />'); ?>
</ul>
It works, but the problem is I don't know how to get the post featured image there instead. I tried this but it did not work:
<?php wp_list_pages('title_li=&child_of=10&link_after=<img src="' . the_post_thumbnail(array(100,50)) . '" alt="" />'); ?>
Obviously I'm missing something.
Any suggestions would be appreciated.
To display the page titles along with the images, you should use get_pages()
<?php
$pages = get_pages('child_of=10');
if ($pages) {
echo '<ul>';
foreach ($pages as $page) {
echo '<li><a href="'.get_permalink($page->ID).'">';
echo get_the_title($page->ID);
echo get_the_post_thumbnail($page->ID);
echo '</a></li>';
}
echo '</ul>';
}
?>
The get_the_post_thumbnail function returns HTML not the image url.
Instead use
<?php $image_url = wp_get_attachment_image_src( get_post_thumbnail_id(10), array(100,50) ); ?>
<?php wp_list_pages('title_li=&child_of=10&link_after=<img src="' . $image_url . '" alt="" />'); ?>
This will list all sub-pages of page id 10, with thumbnail of page id 10. If you want the thumbnails of the sub-pages instead of parent page, you will have to write custom code instead of wp_list_pages function(as explained by Indranil).

Magento display multiple product images on details page

I am uploading 2 images for each product. Only 2 no more, no less.
1st image is product base image and will be displayed on product details page.
I am using following line to display this image:
$_img = '<img src="'.$this->helper('catalog/image')->init($_product, 'image')->resize(180, 300).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';
echo $_helper->productAttribute($_product, $_img, 'image');
I am able to display the above image correctly.
Second image will be displayed in a pop up when a particular link is clicked. I do not want to display a gallery. Just want to display each image separately.
I think I found a way to retrieve array of images uploaded:
$productData = $_product->getData();
$secondImage = $productData['media_gallery']['images'][1];
Now, I am not sure how to display this image. I mean how to use helper, like I used for first image to make is display second image.
I've option to hardcode the complete path of image and use:
$secondImage['file'];
to display the image etc, but, I want to know if there is way, I can do it magento way ?
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'scrollbars=yes,width=200,height=200,resizable=yes');return false;">
<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(68,68); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/>
</a>
</li>
<?php endforeach; ?>
Code lifted from:
http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/adding_lightbox_to_magento_v2
Note exactly the answer but obviously you can remove the foreach and simply supply the array's index if you know your only going to have one file.

Resources