Thursday, February 28, 2013
Wednesday, February 27, 2013
How to improve your magento store performance?
There a are few tips for getting your magento store running at much faster speed:
- Enabled magento caching
- MySQL Query caching
- Enable Gzip Compression
- Disable any unused modules
- Disable the Magento log
- Optimise your images
- Combine external CSS/JS into one file
- Enable Apache KeepAlives: Make sure your Apache configuration has KeepAlives enabled.
Sunday, February 24, 2013
How to check the remote image file is valid or not
Recently i just come across the situation to check that whether the image on the URL is valid image file or not because i was running in the situation where some images are exists but they were broken just broken files...
So here the php code to check that image file directly from the URL
[php]
$src = "http://www.yoursite.com/full/path/to/your/image.jpg";
list($width, $height, $type, $attr) = @getimagesize($src);
[/php]
The getimagesize function will determine the size of any given image then you can check with your code whether is width and height is set or not
[php]
if(!isset($width) || is_null($width)){
echo "Not a valid image";
}
else{
echo "Valid image";
}[/php]
Happy coding :-)
So here the php code to check that image file directly from the URL
[php]
$src = "http://www.yoursite.com/full/path/to/your/image.jpg";
list($width, $height, $type, $attr) = @getimagesize($src);
[/php]
The getimagesize function will determine the size of any given image then you can check with your code whether is width and height is set or not
[php]
if(!isset($width) || is_null($width)){
echo "Not a valid image";
}
else{
echo "Valid image";
}[/php]
Happy coding :-)
Saturday, February 23, 2013
Magento : How to get products for which image is added
Here is the sample code for getting all the products for which base image is added.
[php]</pre>
$collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToFilter('image', array('neq' =>'no_selection'))
->addAttributeToSelect('*');
<pre>[/php]
May be this could help some one who have some requirements like this.
[php]</pre>
$collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToFilter('image', array('neq' =>'no_selection'))
->addAttributeToSelect('*');
<pre>[/php]
May be this could help some one who have some requirements like this.
Magento : Load some products which comes under products Id range
Here is simple code for filtering product collection based on the on product id range, suppose you want to filter the collection
Example you want to filter the collection where product id comes under the range of 1 and 100
[php]
$collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToFilter('entity_id', array('lt' =>500,'gt'=>1))
->addAttributeToSelect('*');
[/php]
Example you want to filter the collection where product id comes under the range of 1 and 100
[php]
$collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToFilter('entity_id', array('lt' =>500,'gt'=>1))
->addAttributeToSelect('*');
[/php]
How to open a you tube video in custom light box
You can open a video in pop up window. Simply copy paste the html code shown below into an html file. Attached jquery library file (default jquery library). Copy paste the code for css into a css file and link that file with the html file. Copy paste the javascript code in the <head> tag in html file.
How it works?
When a user simply clicks on the image (image is under anchor tag with id="clickme"), video gets open up into lightbox. As autoplay value is true for the video, you do not need to click on play button, video starts automatically. Tested this code on mozilla firefox, google chrome, IE.
[css language="true"]
<div class="featured_video">
<div class="head_featured_video">Featured Video</div>
<a id="clickme" href="javascript:popup()"><img alt="" src="http://www.magegurus.com/wp-content/uploads/2013/02/video.jpg" /> </a>
<div id="floatingDiv">
<div id="floatinginner">
<div id="innerfloatingdiv"><a id="closing" href="javascript:popclose()"></a>
<iframe src="http://www.youtube.com/embed/lIza2Dh8o3M?wmode=transparent?&autoplay=0" height="350" width="425" allowfullscreen="" frameborder="0" scrolling="no"></iframe>
</div>
</div>
</div>
</div>
[/css]
[css autolinks="false" classname="myclass" language="false"]
<style type="text/css">
#floatingDiv { display: none; position: fixed; top: 0px; left: 0px; z-index: 1005; opacity: 1; width: 100%; height: 100%;
background-image: none; background-color: transparent; }
#floatingDiv #floatinginner { width: 995px; height: 100%; position: absolute; background-color: #ffffff; }
#floatingDiv #innerfloatingdiv { position: absolute; border: 4px solid #000000; height: 350px; width: 425px; }
#innerfloatingdiv a#closing { color: #990000; width: 17px; height: 17px; background-color: #ededed; position: absolute; z-index: 9; top: 5px; right: 4px;
background-image: url(01fbf%2B3Ef4L.gif); background-repeat: no-repeat; background-position: left top; }
</style>
[/css]
[code lang="js"]
<script type="text/javascript">
function popup(){
jQuery("#floatinginner").fadeIn(3000);
jQuery("#innerfloatingdiv").css("display", "none");
var disp = document.getElementById('floatingDiv').style.display;
if(disp == 'block')
disp = 'none';
else disp = 'block';
document.getElementById('floatingDiv').style.display = disp;
var window_width = j(window).width();
var floatinginner_width = 497;
var half_win_width = window_width/2;
var leftnum = half_win_width - floatinginner_width;
var lef = Math.abs(parseInt(leftnum));
var left = parseInt(lef) + 'px';
var elemen = document.getElementById('floatinginner');
elemen.style.left=left;
setTimeout(function(){
jQuery("#innerfloatingdiv").css("display", "block");
var autoply = jQuery("#floatingDiv #innerfloatingdiv iframe").attr("src");
autoply = autoply.replace("autoplay=0","autoplay=1");
jQuery("#floatingDiv #innerfloatingdiv iframe").attr("src",autoply);
}, 3000);
var video_width = 213;
var floatinginner_full_wd = 994;
var pop_left = floatinginner_width - video_width;
var le = Math.abs(parseInt(pop_left));
//if number is negative then make it positive.
var window_height = j(window).height();
var half_window_height = window_height/2;
var half_innerfloatingdiv_height = 172;
var fromtop = half_window_height - half_innerfloatingdiv_height;
var toppx = Math.abs(parseInt(fromtop));
var top = parseInt(toppx) + 'px';
var lee = parseInt(le) + 'px';
var elem = document.getElementById('innerfloatingdiv');
elem.style.left=lee;
elem.style.top=top;
}
function popclose() {
var clos = document.getElementById('floatingDiv').style.display;
if(clos == 'block') {
var innr = document.getElementById('floatingDiv');
if(innr) {
var m = jQuery("#floatingDiv #innerfloatingdiv iframe").attr("src");
m = m.replace("autoplay=1","autoplay=0");
jQuery("#floatingDiv #innerfloatingdiv iframe").attr("src",m);
var have;
have = innr.innerHTML; innr.innerHTML = '';
innr.innerHTML = have;
}
clos = 'none';
// document.getElementById('floatingDiv').style.display = clos;
jQuery('#innerfloatingdiv').css("display","none");
jQuery('#floatinginner').fadeOut(2000);
setTimeout(function(){ jQuery('#floatingDiv').css("display","none"); },2000); }
}
</script>
[/code]
How it works?
When a user simply clicks on the image (image is under anchor tag with id="clickme"), video gets open up into lightbox. As autoplay value is true for the video, you do not need to click on play button, video starts automatically. Tested this code on mozilla firefox, google chrome, IE.
[css language="true"]
<div class="featured_video">
<div class="head_featured_video">Featured Video</div>
<a id="clickme" href="javascript:popup()"><img alt="" src="http://www.magegurus.com/wp-content/uploads/2013/02/video.jpg" /> </a>
<div id="floatingDiv">
<div id="floatinginner">
<div id="innerfloatingdiv"><a id="closing" href="javascript:popclose()"></a>
<iframe src="http://www.youtube.com/embed/lIza2Dh8o3M?wmode=transparent?&autoplay=0" height="350" width="425" allowfullscreen="" frameborder="0" scrolling="no"></iframe>
</div>
</div>
</div>
</div>
[/css]
[css autolinks="false" classname="myclass" language="false"]
<style type="text/css">
#floatingDiv { display: none; position: fixed; top: 0px; left: 0px; z-index: 1005; opacity: 1; width: 100%; height: 100%;
background-image: none; background-color: transparent; }
#floatingDiv #floatinginner { width: 995px; height: 100%; position: absolute; background-color: #ffffff; }
#floatingDiv #innerfloatingdiv { position: absolute; border: 4px solid #000000; height: 350px; width: 425px; }
#innerfloatingdiv a#closing { color: #990000; width: 17px; height: 17px; background-color: #ededed; position: absolute; z-index: 9; top: 5px; right: 4px;
background-image: url(01fbf%2B3Ef4L.gif); background-repeat: no-repeat; background-position: left top; }
</style>
[/css]
[code lang="js"]
<script type="text/javascript">
function popup(){
jQuery("#floatinginner").fadeIn(3000);
jQuery("#innerfloatingdiv").css("display", "none");
var disp = document.getElementById('floatingDiv').style.display;
if(disp == 'block')
disp = 'none';
else disp = 'block';
document.getElementById('floatingDiv').style.display = disp;
var window_width = j(window).width();
var floatinginner_width = 497;
var half_win_width = window_width/2;
var leftnum = half_win_width - floatinginner_width;
var lef = Math.abs(parseInt(leftnum));
var left = parseInt(lef) + 'px';
var elemen = document.getElementById('floatinginner');
elemen.style.left=left;
setTimeout(function(){
jQuery("#innerfloatingdiv").css("display", "block");
var autoply = jQuery("#floatingDiv #innerfloatingdiv iframe").attr("src");
autoply = autoply.replace("autoplay=0","autoplay=1");
jQuery("#floatingDiv #innerfloatingdiv iframe").attr("src",autoply);
}, 3000);
var video_width = 213;
var floatinginner_full_wd = 994;
var pop_left = floatinginner_width - video_width;
var le = Math.abs(parseInt(pop_left));
//if number is negative then make it positive.
var window_height = j(window).height();
var half_window_height = window_height/2;
var half_innerfloatingdiv_height = 172;
var fromtop = half_window_height - half_innerfloatingdiv_height;
var toppx = Math.abs(parseInt(fromtop));
var top = parseInt(toppx) + 'px';
var lee = parseInt(le) + 'px';
var elem = document.getElementById('innerfloatingdiv');
elem.style.left=lee;
elem.style.top=top;
}
function popclose() {
var clos = document.getElementById('floatingDiv').style.display;
if(clos == 'block') {
var innr = document.getElementById('floatingDiv');
if(innr) {
var m = jQuery("#floatingDiv #innerfloatingdiv iframe").attr("src");
m = m.replace("autoplay=1","autoplay=0");
jQuery("#floatingDiv #innerfloatingdiv iframe").attr("src",m);
var have;
have = innr.innerHTML; innr.innerHTML = '';
innr.innerHTML = have;
}
clos = 'none';
// document.getElementById('floatingDiv').style.display = clos;
jQuery('#innerfloatingdiv').css("display","none");
jQuery('#floatinginner').fadeOut(2000);
setTimeout(function(){ jQuery('#floatingDiv').css("display","none"); },2000); }
}
</script>
[/code]
Thursday, February 14, 2013
Magento : How to delete all images which are not exists in the directory
Here is a simple code for those who want to delete all images from the product for which images are not exists in the media/catalog/product folder
[php]
<pre><em id="__mceDel">
ini_set('memory_limit', '-1');
set_time_limit(0);
require_once ('app/Mage.php');
Mage::app('default');
$imgfolder = 'media/catalog/product';
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*');
foreach ($collection as $product) {
$img = $product->getImage();
if(!file_exists($imgfolder.$img)){
$products = Mage::getModel('catalog/product')->load($product->getId());
$entityTypeId = $products->getEntityTypeId();
$mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode($entityTypeId, 'media_gallery');
$gallery = $products->getMediaGalleryImages();
foreach ($gallery as $image)
$mediaGalleryAttribute->getBackend()->removeImage($products, $image->getFile());
$products->save();
}
}
[/php]
Explanation of code :
1) ini_set('memory_limit', '-1');
There could be thousand of products that we can not load them in one shot.
2) set_time_limit(0);
Setting script execution time limit to no-limit as it could stop in between
[php]
<pre><em id="__mceDel">
ini_set('memory_limit', '-1');
set_time_limit(0);
require_once ('app/Mage.php');
Mage::app('default');
$imgfolder = 'media/catalog/product';
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*');
foreach ($collection as $product) {
$img = $product->getImage();
if(!file_exists($imgfolder.$img)){
$products = Mage::getModel('catalog/product')->load($product->getId());
$entityTypeId = $products->getEntityTypeId();
$mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode($entityTypeId, 'media_gallery');
$gallery = $products->getMediaGalleryImages();
foreach ($gallery as $image)
$mediaGalleryAttribute->getBackend()->removeImage($products, $image->getFile());
$products->save();
}
}
[/php]
Explanation of code :
1) ini_set('memory_limit', '-1');
There could be thousand of products that we can not load them in one shot.
2) set_time_limit(0);
Setting script execution time limit to no-limit as it could stop in between
Subscribe to:
Comments (Atom)