public function getAction()
 {
     if (!$this->getRequest()->has('guid')) {
         return;
     }
     $guid = $this->getRequest()->get('guid');
     $width = 0;
     if ($this->getRequest()->has('width')) {
         $width = (int) $this->getRequest()->get('width');
     }
     $height = 0;
     if ($this->getRequest()->has('height')) {
         $height = (int) $this->getRequest()->get('height');
     }
     //Check if width or height is setted
     if ($width + $height != 0) {
         $guid = str_replace('.', "_{$width}x{$height}.", $guid);
     }
     $url = Mage::getStoreConfig('webtoprint/settings/url') . '/thumb/' . $guid;
     $response = zetaprints_get_content_from_url($url);
     if (!zetaprints_has_error($response)) {
         $headers = $response['content']['header'];
         if (is_array($headers)) {
             $this->getResponse()->setHeader('Last-Modified', $headers['Last-Modified'], true)->setHeader('ETag', $headers['ETag'], true)->setHeader('Pragma', '', true)->setHeader('Cache-Control', 'public', true)->setHeader('Cache-Control', $headers['Cache-Control'])->setHeader('Expires', '', true)->setHeader('Content-Type', $headers['Content-Type'], true)->setHeader('Content-Length', $headers['Content-Length'], true);
         } else {
             $type = explode('.', $guid);
             if (count($type) == 2) {
                 $type = $type[1];
             }
             if ($type == 'jpg') {
                 $type = 'jpeg';
             }
             $this->getResponse()->setHeader('Content-Type', 'image/' . $type);
         }
         $this->getResponse()->setBody($response['content']['body']);
     }
 }
function zetaprints_get_content_from_url($url, $post = null)
{
    _zetaprints_debug();
    $error = null;
    do {
        $_data = _zp_curl_retrieve_data($url, $post);
        if (zetaprints_has_error($_data)) {
            return $_data;
        }
        //Extract $info, $headers and $body variables
        extract($_data['content']);
        $headers = function_exists('http_parse_headers') ? http_parse_headers($headers) : _zetaprints_parse_http_headers($headers);
        $error = _zp_process_error($info, $headers, $error);
        if ($error) {
            _zetaprints_debug(compact('error', 'info', 'headers', 'body'));
            if (isset($error['update_request'])) {
                extract($error['update_request']);
            }
        }
    } while ($error && _zp_repeat($error));
    if ($error) {
        return _zetaprints_error($error['message']);
    }
    //Do not output images to logs
    $_body = $body;
    if (isset($info['content_type']) && strpos($info['content_type'], 'image') === 0) {
        $_body = $info['content_type'];
    }
    _zetaprints_debug(compact('headers', '_body'));
    return _zetaprints_ok(compact('headers', 'body'));
}
 public function downloadAction()
 {
     $request = $this->getRequest();
     if (!$request->has('guid')) {
         return;
     }
     $guid = $request->get('guid');
     $mediaConfig = Mage::getModel('catalog/product_media_config');
     $path = str_replace('preview/', 'previews/', $guid);
     $path = $mediaConfig->getTmpMediaPath($path);
     //Check that preview was already downloaded
     //to prevent subsequent downloads
     if (file_exists($path)) {
         echo json_encode('OK');
         return;
     }
     $url = Mage::getStoreConfig('webtoprint/settings/url') . $guid;
     //Download preview image from ZetaPrinrs
     $response = zetaprints_get_content_from_url($url);
     $errorMsg = 'Error was occurred while preparing preview image';
     if (zetaprints_has_error($response)) {
         echo json_encode($this->__($errorMsg));
         return;
     }
     //Save preview image on M. server
     if (file_put_contents($path, $response['content']['body']) === false) {
         echo json_encode($this->__($errorMsg));
         return;
     }
     echo json_encode('OK');
 }