Reputation: 21
Does anybody know how to get Magento to use the actual base URL of images instead of the cached ones?
Using Magento Community V 1.6.2.0
If I look at the URL of a product image you get something like this…
/media/catalog/product/cache/1/image/390x/5e06319eda06f020e43594a9c230972d/1/1/1101012001-J-1Front-Man/.jpg
I need for it to be more like:
/media/catalog/product/1101012001-J-1Front-Man/.jpg
Reason is that I'm using the M2EPro extension which allows you to sync products for sale/sold in Magento with eBay/Amazon. Problem is that eBay does not allow image URLs that are longer that 150 characters. The MD5 hash and other mixed in variables (that I believe originate at /app/code/core/Mage/Catalog/Helper/Image.php) make the URL too long for it to be usable for many of my images.
When M2EPro runs it pulls the cached image (because that is what Magento designates as the main image). I believe that I need to have only the absolute URL referenced and have not been able to put this all together yet.
I have seen lots and lots of similar questions that span years with no definite answer on this one. If I find the answer first I will post here, but any help is really, really appreciated!
Current code from Image.php file mentioned above:
$this->_baseFile = $baseFile;
// build new filename (most important params)
$path = array(
Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath(),
Mage::app()->getStore()->getId(),
$path[] = $this->getDestinationSubdir()
);
if((!empty($this->_width)) || (!empty($this->_height)))
$path[] = "{$this->_width}x{$this->_height}";
// add misk params as a hash
$miscParams = array(
($this->_keepAspectRatio ? '' : 'non') . 'proportional',
($this->_keepFrame ? '' : 'no') . 'frame',
($this->_keepTransparency ? '' : 'no') . 'transparency',
($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
$this->_rgbToString($this->_backgroundColor),
'angle' . $this->_angle,
'quality' . $this->_quality
);
// if has watermark add watermark params to hash
if ($this->getWatermarkFile()) {
$miscParams[] = $this->getWatermarkFile();
$miscParams[] = $this->getWatermarkImageOpacity();
$miscParams[] = $this->getWatermarkPosition();
$miscParams[] = $this->getWatermarkWidth();
$miscParams[] = $this->getWatermarkHeigth();
}
$path[] = md5(implode('_', $miscParams));
// append prepared filename
$this->_newFile = implode('/', $path) . $file; // the $file contains heading slash
return $this;
}
public function getBaseFile()
{
return $this->_baseFile;
}
public function getNewFile()
{
return $this->_newFile;
}
/**
* @return Mage_Catalog_Model_Product_Image
*/
public function setImageProcessor($processor)
{
$this->_processor = $processor;
return $this;
}
/**
* @return Varien_Image
*/
public function getImageProcessor()
{
if( !$this->_processor ) {
// var_dump($this->_checkMemory());
// if (!$this->_checkMemory()) {
// $this->_baseFile = null;
// }
$this->_processor = new Varien_Image($this->getBaseFile());
}
$this->_processor->keepAspectRatio($this->_keepAspectRatio);
$this->_processor->keepFrame($this->_keepFrame);
$this->_processor->keepTransparency($this->_keepTransparency);
$this->_processor->constrainOnly($this->_constrainOnly);
$this->_processor->backgroundColor($this->_backgroundColor);
$this->_processor->quality($this->_quality);
return $this->_processor;
}
/**
* @see Varien_Image_Adapter_Abstract
* @return Mage_Catalog_Model_Product_Image
*/
public function resize()
{
if (is_null($this->getWidth()) && is_null($this->getHeight())) {
return $this;
}
$this->getImageProcessor()->resize($this->_width, $this->_height);
return $this;
}
/**
* @return Mage_Catalog_Model_Product_Image
*/
public function rotate($angle)
{
$angle = intval($angle);
$this->getImageProcessor()->rotate($angle);
return $this;
}
/**
* Set angle for rotating
*
* This func actually affects only the cache filename.
*
* @param int $angle
* @return Mage_Catalog_Model_Product_Image
*/
public function setAngle($angle)
{
$this->_angle = $angle;
return $this;
}
/**
* Add watermark to image
* size param in format 100x200
*
* @param string $file
* @param string $position
* @param string $size
* @param int $width
* @param int $heigth
* @param int $imageOpacity
* @return Mage_Catalog_Model_Product_Image
*/
public function setWatermark($file, $position=null, $size=null, $width=null, $heigth=null, $imageOpacity=null)
{
if ($this->_isBaseFilePlaceholder)
{
return $this;
}
if ($file) {
$this->setWatermarkFile($file);
} else {
return $this;
}
if ($position)
$this->setWatermarkPosition($position);
if ($size)
$this->setWatermarkSize($size);
if ($width)
$this->setWatermarkWidth($width);
if ($heigth)
$this->setWatermarkHeigth($heigth);
if ($imageOpacity)
$this->setImageOpacity($imageOpacity);
$filePath = $this->_getWatermarkFilePath();
if($filePath) {
$this->getImageProcessor()
->setWatermarkPosition( $this->getWatermarkPosition() )
->setWatermarkImageOpacity( $this->getWatermarkImageOpacity() )
->setWatermarkWidth( $this->getWatermarkWidth() )
->setWatermarkHeigth( $this->getWatermarkHeigth() )
->watermark($filePath);
}
return $this;
}
/**
* @return Mage_Catalog_Model_Product_Image
*/
public function saveFile()
{
$filename = $this->getNewFile();
$this->getImageProcessor()->save($filename);
Mage::helper('core/file_storage_database')->saveFile($filename);
return $this;
}
/**
* @return string
*/
public function getUrl()
{
$baseDir = Mage::getBaseDir('media');
$path = str_replace($baseDir . DS, "", $this->_newFile);
return Mage::getBaseUrl('media') . str_replace(DS, '/', $path);
}
public function push()
{
$this->getImageProcessor()->display();
}
/**
* @return Mage_Catalog_Model_Product_Image
*/
public function setDestinationSubdir($dir)
{
$this->_destinationSubdir = $dir;
return $this;
}
/**
* @return string
*/
public function getDestinationSubdir()
{
return $this->_destinationSubdir;
}
public function isCached()
{
return $this->_fileExists($this->_newFile);
}
/**
* Set watermark file name
*
* @param string $file
* @return Mage_Catalog_Model_Product_Image
*/
public function setWatermarkFile($file)
{
$this->_watermarkFile = $file;
return $this;
}
/**
* Get watermark file name
*
* @return string
*/
public function getWatermarkFile()
{
return $this->_watermarkFile;
}
/**
* Get relative watermark file path
* or false if file not found
*
* @return string | bool
*/
protected function _getWatermarkFilePath()
{
$filePath = false;
if (!$file = $this->getWatermarkFile())
{
return $filePath;
}
$baseDir = Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath();
if( $this->_fileExists($baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file) ) {
$filePath = $baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file;
} elseif ( $this->_fileExists($baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file) ) {
$filePath = $baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file;
} elseif ( $this->_fileExists($baseDir . '/watermark/default/' . $file) ) {
$filePath = $baseDir . '/watermark/default/' . $file;
} elseif ( $this->_fileExists($baseDir . '/watermark/' . $file) ) {
$filePath = $baseDir . '/watermark/' . $file;
} else {
$baseDir = Mage::getDesign()->getSkinBaseDir();
if( $this->_fileExists($baseDir . $file) ) {
$filePath = $baseDir . $file;
}
}
return $filePath;
}
Upvotes: 1
Views: 7339
Reputation: 6003
I guess you can use
echo Mage::getModel('catalog/product_media_config')
->getMediaUrl( $product->getImage() );
You can also use getSmallImage()
or getThumbnail()
methods instead.
Upvotes: 2
Reputation: 46
What version of M2e Pro are you using, my current version is 6 and it has an option to use base image as opposed to gallery. Works great in my magento 1.6.2.0 if you have your base image at least 500 pixals
Upvotes: 0