使用Magento将图像png转换为jpg

时间:2015-03-26 16:35:20

标签: image magento png jpeg

我尝试使用Magento在自定义模块中转换jpg中的图像png。 上传工作,但当我尝试转换图片时,有一个prblm。在Varien_file_uploader中没有转换方法,但在Varien_image中有转换方法。 我试着这样说:

$uploader = new Varien_File_Uploader('image');              
$uploader->setAllowedExtensions(array('jpg','png','gif','jpeg'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$uploader->save($path, $_FILES['image']['name']);   

$image = new Varien_Image($path . DS . $_FILES['image']['name']);
$image->convert('jpeg');
$image->save($path, 'mypic.jpeg');

有什么想法吗? THX

2 个答案:

答案 0 :(得分:1)

我创建了一个函数,你可以将它用作帮助器。如果有人需要它:)

class YourCompany_YourModule_Helper_Image_Data extends Mage_Core_Helper_Abstract{

    public function convert($ext, $path, $name, $newname = NULL)
    {
        $exploded = explode('.',$name);
        $extoriginal = $exploded[count($exploded) - 1];

        switch($extoriginal)
        {
            case 'jpg':
            case 'jpeg':
                $image = imagecreatefromjpeg($path . $name);
            break;
            case 'png':
                $image = imagecreatefrompng($path . $name);
            break;
            case 'gif':
                $image = imagecreatefromgif($path . $name);
            break;
            case 'bmp':
                $image = imagecreatefrombmp($path . $name);
            break;
        }

        $bg = imagecreatetruecolor(imagesx($image), imagesy($image));
        imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
        imagealphablending($bg, TRUE);
        imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
        imagedestroy($image);
        $quality = 100;

        $newname = ($newname == NULL) ? $exploded[0] : $newname;

        switch($ext)
        {
            case 'jpg':
            case 'jpeg':
                $newimage = $path . $newname . ".jpg";
                imagejpeg($bg, $newimage, $quality);
            break;
            case 'png':
                $newimage = $path . $newname . ".png";
                imagepng($bg, $newimage, $quality);
            break;
            case 'gif':
                $newimage = $path . $newname . ".gif";
                imagegif($bg, $newimage, $quality);
            break;
            case 'bmp':
                $newimage = $path . $newname . ".bmp";
                imagewbmp($bg, $newimage, $quality);
            break;
        }
        imagedestroy($bg);
        return $newimage;
    }
}

在config.xml(自定义模块)中,将其添加到全局节点

    <helpers>
        <news>
            <class>YourCompany_YourModule_Helper_Image</class>
        </news>
    </helpers>

并使用

$helper = Mage::helper('YourModule');
$helper->convert('jpg', $path, $yourimage);

享受:)

答案 1 :(得分:0)

没有关于magento功能的想法,但是下面是php函数,它将PNG转换为JPG,透明度为白色。

$image = imagecreatefrompng($filePath);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
$quality = 50; // 0 = worst / smaller file, 100 = better / bigger file 
imagejpeg($bg, $filePath . ".jpg", $quality);
imagedestroy($bg);