Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento:调整图像大小

您可以使用固定的高度和可变的宽度调整图像大小。或者,您可以使用固定宽度和可变高度调整大小。以下代码显示了如何在Magento中进行操作。

固定宽度600px

<?php echo $this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(600,null)
?>

固定高度600px

<?php echo $this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(null,600)
?>

以下代码将按比例调整图像大小,并且不让图像大于指定的高度和宽度。

<?php echo $this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(400,400)
?>

上面使用的功能的说明:-

constrainOnly(bool $ flag)
保证图片图像不会比原来大。在调用resize()之前适用。默认为false

keepAspectRatio(bool $ flag)
确保图像图片的宽度/高度不会失真。在调用resize()之前适用。默认情况下为true。

keepFrame(bool $ flag)
确保图片的尺寸设置为$ width / $ height。在调用resize()之前适用,如果keepAspectRatio(false)不适用。

您也可以使用Varien_Image类调整图像的大小。当调整与目录产品无关的图像的大小时,这是最合适的。

// actual path of image
$imageUrl = Mage::getBaseDir('media').DS."myimage".DS.$post->getThumbnail();
 
// path of the resized image to be saved
// here, the resized image is saved in media/resized folder
$imageResized = Mage::getBaseDir('media').DS."myimage".DS."resized".DS.$post->getThumbnail();
 
// resize image only if the image file exists and the resized image file doesn't exist
// the image is resized proportionally with the width/height 135px
if (!file_exists($imageResized)&&file_exists($_imageUrl)) :
    $imageObj = new Varien_Image($_imageUrl);
    $imageObj->constrainOnly(TRUE);
    $imageObj->keepAspectRatio(TRUE);
    $imageObj->keepFrame(FALSE);
    $imageObj->resize(135, 135);
    $imageObj->save($imageResized);
endif;

您现在可以将调整大小后的图像用作:

<img src="<?php echo Mage::getBaseUrl('media')."myimage/resized/".$post->getThumbnail() ?>" />

另一种情况

假设您有一个图像链接。现在,您要调整它的大小。该图像可能来自任何地方。在此示例中,我假设图像链接像http ://localhost/magento/media/catalog/ category/test_image .jpg

现在,我必须调整它的大小。为此,我将在media / catalog / category内部创建一个名为resize的目录。并且,我将调整大小的文件保存到新创建的调整大小的目录中。

// my sample image[/source]
 
$imageUrl = "http://localhost/magento/media/catalog/category/test_image.jpg";
 
// create folder
 if(!file_exists("./media/catalog/category/resized"))     mkdir("./media/catalog/category/resized",0777);
 
 // get image name
 $imageName = substr(strrchr($imageUrl,"/"),1);
 
 // resized image path (media/catalog/category/resized/IMAGE_NAME)
 $imageResized = Mage::getBaseDir('media').DS."catalog".DS."category".DS."resized".DS.$imageName;
 
 // changing image url into direct path
 $dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($imageUrl,'/media'));
 
 // if resized image doesn't exist, save the resized image to the resized directory
 if (!file_exists($imageResized)&&file_exists($dirImg)) :
 $imageObj = new Varien_Image($dirImg);
 $imageObj->constrainOnly(TRUE);
 $imageObj->keepAspectRatio(TRUE);
 $imageObj->keepFrame(FALSE);
 $imageObj->resize(120, 120);
 $imageObj->save($imageResized);
 endif;
 
 $newImageUrl = Mage::getBaseUrl('media')."catalog/category/resized/".$imageName;

显示新创建的调整大小的图像。

	
<img src="<?php echo Mage::getBaseUrl('media')."catalog/category/resized/".$newImageUrl ?>" />

您可以检查Varien_Image的其他功能

http://docs.magentocommerce.com/Varien/elementindex_Varien_Image.html

希望这可以帮助。谢谢。

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论