Hot answers tagged

17 votes
Accepted

Image asset url is not being output

So after looking through other craft websites to see how they where able to get images to work I discovered what I did wrong when setting up the asset source. The thing is though that the craft ...
matthew's user avatar
  • 333
12 votes

Image alignment and size in Redactor 2

No need for a plugin. Just add: "imageResizable": true, "imagePosition": true, to your chosen redactor config file i.e. (/craft/config/redactor/standard.json)
Rattikarl's user avatar
  • 393
11 votes
Accepted

Looping through images in a Asset field type

An Asset field – like all relational fields, including Entries, Categories etc – always return an array-like object which is an instance of a class called ElementCriteriaModel. To output the actual ...
Mats Mikkel Rummelhoff's user avatar
8 votes

Is there a way to create a "sticky" asset

Based on adding a lightswicht ("sticky") as suggested by @carlcs I think you should be able to solve this by just using two order fields. {% set images = craft.assets.folderId(18).order('sticky desc, ...
Pascal Mollet's user avatar
8 votes
Accepted

Using Image Transform as another custom field

The imageTransform value is actually an object, representing the entire dropdown field. Update the first line to this... {% set imageTransform = storyEntry.storyThumbnailTransform.value %} This ...
Lindsey D's user avatar
  • 23.8k
7 votes

How can I automatically generate responsive images from a single hi-res asset?

There is a great article here which I thoroughly recommend reading. It goes into this in great detail. It even includes a twig macro that deals with both retina images as well as responsive images: ...
j00lz's user avatar
  • 769
7 votes

Output image in template

When you are working with an asset field you are actually looking at the object so you need to call {{asset.url}} in order to actually output the path to the image or whatever asset you are working ...
mcclaskiem's user avatar
7 votes
Accepted

Craft 3 image asset getUrl() not showing

The solution is to enable "assets for this source have public URLs" in the volume settings.
nmsdvid's user avatar
  • 305
6 votes
Accepted

Load an asset at random each page load

Simply using 'RAND()' in the order parameter does the trick. {% for image in entry.banner.limit(1).order('RAND()') %} {{ image.url }} {% endfor %}
John Johnson's user avatar
5 votes
Accepted

Is there a way to create a "sticky" asset

Add a lightswitch field to your assets and query for assets with the lightswitch enabled first. Use the ids() method to get just the assets' IDs and merge them with the rest of your assets' IDs to ...
carlcs's user avatar
  • 36.1k
5 votes
Accepted

Global Image Variables?

seo.twittercard is an ElementCriteriaModel. To access the actual asset, you need to use seo.twittercard.first (or equivalently: seo.twittercard.first()). However, you should always check that this ...
Marion Newlevant's user avatar
5 votes
Accepted

Importing images from external url and saving to entry using plugin

Here's how I did it in a recent project: $fileIds = array(); // get remote image and store in temp path $imageInfo = pathinfo($data['image_url']); $tempPath = CRAFT_STORAGE_PATH . '...
André Elvan's user avatar
  • 7,268
5 votes

Upload any size image into carousel

You shouldn't need to create a custom plugin to do that. You can easily create a Matrix content field for your carousel. Create a new block called slide. From there you can create an asset field to ...
Rob Erskine's user avatar
5 votes
Accepted

Is it possible to create a new asset from an image URL?

Download the image into a temp file, then upload it as an asset. You can find $iAssetSourceId by looking at the URL when editing an asset source $iAssetSourceId = 4; $iAssetFolderId = (int)craft()-&...
michael's user avatar
  • 228
5 votes

I am unable to correctly return the url of an uploaded asset

I found the answer in a similar post here: Image asset url is not being output My problem was with my Asset Source settings. Now that I think about it, it's obvious: I had no public URL declared for ...
Steven Thate's user avatar
5 votes
Accepted

Images containing EXIF orientation tag are not rotated on upload

After some mail exchanges with Brad and Andris from Craft CMS, it turned out that there is an edge case where the image rotation on upload will not work if the EXIF extension is not installed (which ...
kant312's user avatar
  • 496
4 votes
Accepted

Images lose quality when I upload to Craft

There is a way to get Craft uploading my pixel-perfect images without re-save them? You can upload via FTP and then run the Update Asset Indexes tool from the Settings page in the control panel. If ...
Brad Bell's user avatar
  • 67k
4 votes

Load an asset at random each page load

I wanted to see if the same thing could be accomplished using Craft's shuffle function. At first, I was looping through the image array and using an if statement to grab the first one: {% for image ...
Mike Pringle's user avatar
4 votes

Is there any way to prevent Craft from doing anything with uploaded GIF assets?

When you upload an image to Craft, the image is automatically “cleansed” using the same basic process that Craft uses to create image transforms. That happens to ensure that there are no malicious ...
Brandon Kelly's user avatar
4 votes
Accepted

Getting image for product in Cart Screen

This actually depends where the image is stored - if the asset (image) field is on the product, and is called myImage, then as per Brad's example you'd use: lineitem.purchasable.product.myImage ......
Jeremy Daalder's user avatar
4 votes

Redactor image caption not saved and formatting lost

I copied your Redactor settings to a clean installation of Craft (2.6.2945) and added an image to a Rich Text field set to use those specific settings. Initially I encountered the same behaviour. ...
Stephen Hamilton's user avatar
4 votes

Adding images directly to public folder

You need to use the Update Asset Indexes tool in Settings in the control panel: Whenever there are files within your asset source that were not added directly by Craft (e.g. via FTP), you will need ...
Marion Newlevant's user avatar
4 votes

404 Error showing for Asset Images

Figured it out. I assumed that my assets folder needed 744 just as the install instructions mentioned for the Craft CMS folders. But, I need to change the permissions to the standard 755. Once I did, ...
DevServe's user avatar
  • 303
4 votes

Using Focuspoint with eager loading

After some research I found out that this problem has no simple solution (unfortunately). Check https://github.com/smcyr/Craft-FocusPoint/issues/10. A solution would be not to use eager loading (...
Mr. Sam's user avatar
  • 287
4 votes
Accepted

Craft 3 Image quality is terrible after asset upload from admin panel

Converting my comment(s) into an answer... Make sure you are using the ImageMagick library to handle image manipulation (instead of the default GD library). It provides much better results. Craft ...
Lindsey D's user avatar
  • 23.8k
4 votes

How do I add Alt Image Tags to my photos?

Out of the box, you can use the image "Title" as alt text. Since it is already configured - you don't need to add any additional fields. Then you can output this in your twig template as: {% set ...
gjhead's user avatar
  • 123
3 votes

How to add dynamic watermarks to images in Craft/Twig

You'd definitely need a plugin for this. Craft's native abilities don't cover watermarks. I've never attempted it myself, so these are just "best guesses"... but give the following plugins a shot: ...
Lindsey D's user avatar
  • 23.8k
3 votes

Global Image Variables?

This is a general structure to display global images in any template: <img src="{{ globalHandle.globalAssetHandle.one().getUrl }}" alt=""> You have to set your global handle ...
Alexis Dalai Waldo Jiménez's user avatar
3 votes

403 Forbidden Assets

This article will help you set permissions properly for Craft CMS installs: Hardening Craft CMS Permissions It leverages Craft-Scripts to do the heavy lifting.
andrew.welch's user avatar
  • 11.3k
3 votes

Image Transforms - Image not processed until second page refresh

I'm having this problem too. This can't be expected behaviour, can it? EDIT (SOLVED): I've learned from MildlyGeeky in the Craft Discord server that this issue is caused by Twig being picky about ...
user2157969's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible