1

我正在电子商务网站上在线购买 iphone 手机壳,客户可以上传和自定义图像。

因此,在 magento 管理面板中,我将该产品图像转换为 jpg 并保存。但是我想在打印时在图像下方显示订单号,以便在打印的图像中我可以打印带有订单号的图像。

我的 jQuery 代码是:

jQuery.post('https://'+window.location.hostname+'/artmanager/index/pngtojpg',{imagedata:img}).done(
    function(data)
    {
            jimg=data;                  
            window.open(data);
            jQuery("#loading-image").hide();
    }); 

我的控制器代码是:

public function pngtojpgAction()  
    {       
        //Code to convert png to jpg image
        $input = imagecreatefrompng($this->getRequest()->getParam('imagedata'));
        $width=imagesx($input);
        $height=imagesy($input);
        $output = imagecreatetruecolor($width, $height);
        $white = imagecolorallocate($output,  255, 255, 255);
        imagefilledrectangle($output, 0, 0, $width, $height, $white);
        imagecopy($output, $input, 0, 0, 0, 0, $width, $height);


        $mypath=Mage::getBaseDir('media').'/custom_product_preview/predefined_images/temporary_processing/test.jpg' ;//Saving file as temporary file        

        imagejpeg($output,$mypath,100); 



        $filename = $mypath; //Reading the temporary file           

        // Get new sizes
        list($width, $height) = getimagesize($filename);
        $newwidth = 3056;
        $newheight = 4861;

        // Load
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromjpeg($filename);

        // Resize
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        ob_start();
        imagejpeg($thumb);
        $contents =  ob_get_contents();         
        $contents = substr_replace($contents, pack("cnn", 1, 72, 72), 13, 5);   //Converting Image DPI to 72DPI                 
        ob_end_clean();     
        echo 'data:image/jpeg;base64,'.base64_encode($contents);        
}
4

1 回答 1

0

您正在寻找的功能是imagestringhttp://php.net/manual/en/function.imagestring.php

imagestring($image, $font, $x, $y, $string, $color);

$image将会$input

$font是 1 到 5 之间的整数(一些内置字体)。

$x可以是 10 用于一些基本填充或$width / 2 - $widthOfImageString / 2将其居中。

$y可以$height - 20把它放在底部。

$string将是您的订单 ID(或增量 ID)

$color必须是使用imagecolorallocate()创建的颜色标识符。

于 2013-07-02T21:08:56.080 回答