Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.38 KB | None | 0 0
  1. public function createThumbsAction() {
  2.      
  3.       $post_data = $this->getRequest()->getPost();
  4.      
  5.       $session = Mage::getSingleton('checkout/session');
  6.      
  7.      
  8.       //with session -- start your request
  9.       $quote_products = $session->getQuote()->getAllItems();
  10.       $product_count = count($quote_products);
  11.      
  12.      
  13.       $product = $quote_products[$post_data['serial']]->getProduct();
  14.       $options_array = $product->getTypeInstance(true)->getOrderOptions($product);
  15.      
  16.      
  17.       //get the paper size to calculate the crop/bleed image
  18.       //$out .= html_entity_decode(Zend_Debug::dump($options_array, 'session',false));
  19.       foreach ( $options_array["options"] as $options){
  20.         if ($options['label'] == 'Size'){
  21.           $size = html_entity_decode($options['value']);
  22.           break;
  23.         }
  24.       }
  25.      
  26.       $size_array = explode('x',$size);
  27.       foreach ($size_array as &$number){ //filter out the bad stuff
  28.         $number = str_replace(' ','',$number);
  29.         $number = str_replace('"','',$number);
  30.       }
  31.      
  32.      
  33.       $filename = $product->getId()."_".time(). str_replace(" ","_",$_FILES['file']['name']);
  34.       $file_ext = strrchr($filename, '.');
  35.       $file_ext = strtolower($file_ext);
  36.       $file_base = substr($filename, 0, -(strlen($file_ext)));
  37.      
  38.       $target_path = getcwd() . '/skin/frontend/printlion/default/checkoutimage';
  39.       $target_file = $target_path . '/' . $filename;
  40.      
  41.       $large_file = $target_path . '/large_' . $file_base . '.png';
  42.       $thumb_file = $target_path . '/thumbnail_' . $file_base . '.jpg';
  43.       $crop_file  = $target_path . '/cropbleed_' . $file_base . '.png';
  44.       //copy($tmpf,$target_file);
  45.       move_uploaded_file($_FILES['file']['tmp_name'],$target_file);
  46.      
  47.       if ($file_ext == '.pdf'){
  48.         $tmp_pdf = $target_path . '/' . $file_base . '_firstpage_tmp.pdf';
  49.        
  50.         $cmd = "pdftk {$target_file} cat 1-1 output {$tmp_pdf}";
  51.         exec($cmd);
  52.        
  53.         $cmd = "convert {$tmp_pdf} {$large_file}";
  54.         exec($cmd);
  55.        
  56.         unlink($tmp_pdf);
  57.       } elseif ($file_ext == '.eps'){
  58.         $cmd = "convert {$target_file} {$large_file}";
  59.         exec($cmd);
  60.       }else {
  61.         die('bad format');
  62.       }
  63.      
  64.       $this->createCropBleedImage($large_file, $crop_file, $size_array);
  65.       $this->resizeImages($large_file, $thumb_file, $crop_file);
  66.      
  67.      
  68.       $result = $this->getOnepage()->saveUploadDesign($file_base, $post_data);
  69.      
  70.       $this->getResponse()->setBody('');
  71.      
  72.     }
  73.    
  74.     public function resizeImages($large_file, $thumb_file, $crop_file){
  75.       $im = imagecreatefrompng($large_file);
  76.       $input_w = imagesx($im);
  77.       imagedestroy($im);
  78.    
  79.       if($input_w > 600){
  80.         $cmd = "convert  -geometry 600 {$large_file}  {$large_file}";
  81.         exec($cmd);
  82.         $cmd = "convert -geometry 600 {$crop_file} {$crop_file}";
  83.         exec($cmd);
  84.       }else {
  85.         $cmd = "convert  -quality 85 -geometry {$input_w} {$crop_file} {$crop_file}";
  86.         exec($cmd);
  87.       }
  88.    
  89.       $cmd = "convert   -quality 80 -geometry 250 {$large_file}  {$thumb_file}";
  90.       exec($cmd);
  91.      
  92.      
  93.      
  94.     }
  95.    
  96.     public function createCropBleedImage($large_file, $crop_file, $size_array){
  97.      
  98.       // basing previews on the assumption that 72 dots per inch
  99.       $print_w = $size_array[0] *  72;
  100.       $print_h = $size_array[1] * 72;
  101.  
  102.       $bleed = 0.25 * 72; //pixels
  103.       $bleed_img_h = ($print_h + (2*$bleed));
  104.       $bleed_img_w = ($print_w + (2*$bleed));
  105.    
  106.       $uploaded_preview = imagecreatefrompng($large_file);
  107.       $input_w = imagesx($uploaded_preview);
  108.       $input_h = imagesy($uploaded_preview);
  109.      
  110.       $safe_zone = imagecreatefrompng('skin/frontend/printlion/default/checkoutimage/safe_zone_red.png');
  111.       $trim_line = imagecreatefrompng('skin/frontend/printlion/default/checkoutimage/trim_line_blue.png');
  112.      
  113.       if (($input_w > $bleed_img_w) && ($input_h > $bleed_img_h)){
  114.         //setup
  115.         $canvas = imagecreatetruecolor($bleed_img_w+1, $bleed_img_h+1);
  116.         $blue = imagecolorallocate($canvas, 0, 0, 255);
  117.         $red = imagecolorallocate($canvas, 255, 0, 0);
  118.         $bg  = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
  119.         $this->allow_alpha($canvas);
  120.         imagesetthickness($canvas,3);
  121.        
  122.         $style_red = array($red, $red, $bg , $bg , $bg);
  123.         $style_blue = array($blue, $blue, $bg, $bg, $bg);
  124.        
  125.        
  126.         // create background transparent rectangle
  127.         imagefilledrectangle($canvas, 0, 0, ($bleed_img_w+1), ($bleed_img_h+1), $bg);
  128.        
  129.         //create blue line and trim label
  130.         imagesetstyle($canvas,  $style_blue);
  131.         imagerectangle($canvas, 0, 0, ($bleed_img_w), ($bleed_img_h),  IMG_COLOR_STYLED);
  132.         imagecopy($canvas,$trim_line, 0, 0, 0,0, 60, 15);
  133.        
  134.         //create red line and safe label
  135.         imagesetstyle($canvas,  $style_red);
  136.         imagerectangle($canvas, $bleed, $bleed, ($bleed_img_w-$bleed), ($bleed_img_h-$bleed), IMG_COLOR_STYLED);
  137.         imagecopy($canvas,$safe_zone, $bleed, $bleed, 0, 0, 60, 15);
  138.        
  139.         $overlay = imagecreatetruecolor($input_w,$input_h);
  140.         $this->allow_alpha($overlay);
  141.         $x1 = (($input_w/2)-($bleed_img_w/2));
  142.         $y1 = (($input_h/2)-($bleed_img_h/2));
  143.        
  144.         $x2 = (($input_w/2)+($bleed_img_w/2));
  145.         $y2 = (($input_h/2)+($bleed_img_h/2));
  146.        
  147.         $bg = imagecolorallocatealpha($overlay, 0, 0, 0, 64);
  148.         $transparent = imagecolorallocatealpha($overlay, 0, 0, 0, 127);
  149.        
  150.         //$bg
  151.         imagefilledrectangle($overlay, 0,0, $input_w, $input_h,$transparent);
  152.         imagefilledrectangle($overlay, 0,0, $x1, $input_h,$bg);
  153.         imagefilledrectangle($overlay, $x1,0, $input_w, $y1,$bg);
  154.         imagefilledrectangle($overlay, $x1,$y2, $input_w, $input_h,$bg);
  155.         imagefilledrectangle($overlay, $x2,$y1, $input_w, $input_h,$bg);
  156.        
  157.         imagecopy($overlay, $canvas, $x1, $y1, 0, 0, $bleed_img_w+1, $bleed_img_h+1);
  158.         imagecopy($uploaded_preview, $overlay, 0, 0, 0, 0, $input_w, $input_h);
  159.         imagedestroy($canvas);
  160.         imagedestroy($overlay);
  161.        
  162.         $out = &$uploaded_preview;
  163.       } else {
  164.        
  165.         $w = $bleed_img_w;
  166.         $h = $bleed_img_h;
  167.        
  168.         if ($input_h > $bleed_img_h){
  169.           $h = $input_h;
  170.         }
  171.         if ($input_w > $bleed_img_w){
  172.           $w = $input_w;
  173.         }
  174.        
  175.         //create blank canvas large enough in which we will center the uploaded image
  176.         $canvas = imagecreatetruecolor($w+1, $h+1);
  177.         $bg  = imagecolorallocate($canvas, 255, 255, 255);
  178.         imagefilledrectangle($canvas, 0, 0, $w+1, $h+1, $bg); // create background
  179.         imagecopy($canvas, $uploaded_preview, (($w/2)-($input_w/2)), (($h/2)-($input_h/2)), 0, 0, $input_w, $input_h);
  180.      
  181.        
  182.        
  183.        
  184.         //create the opacaque overlay inwhich we draw some darkness and the bleed/cut lines
  185.         $overlay = imagecreatetruecolor($w+1,$h+1);
  186.         $this->allow_alpha($overlay);
  187.         $bg = imagecolorallocatealpha($overlay, 0, 0, 0, 64);
  188.         $transparent = imagecolorallocatealpha($overlay, 0, 0, 0, 127);
  189.         $blue = imagecolorallocate($overlay, 0, 0, 255);
  190.         $red = imagecolorallocate($overlay, 255, 0, 0);
  191.        
  192.         $style_red = array($red, $red, $transparent , $transparent , $transparent);
  193.         $style_blue = array($blue, $blue, $transparent, $transparent, $transparent);
  194.        
  195.        
  196.         //$bg
  197.         $x1_postion = ($w-$bleed_img_w)/2;
  198.         $y1_postion = ($h-$bleed_img_h)/2;
  199.        
  200.         //color in the darkness surrounding the crop bleed marks
  201.         imagefilledrectangle($overlay, 0,0, $w, $h,$transparent);
  202.         imagefilledrectangle($overlay, 0,0, $w, $y1_postion,$bg);
  203.         imagefilledrectangle($overlay, 0,($y1_postion+$bleed_img_h), $w, $h,$bg);
  204.         imagefilledrectangle($overlay, 0,$y1_postion, $x1_postion, ($y1_postion+$bleed_img_h),$bg);
  205.         imagefilledrectangle($overlay, ($x1_postion+$bleed_img_w),0, $w, $h,$bg);
  206.        
  207.         imagesetthickness($overlay,3);
  208.        
  209.         //draw the lines
  210.         imagesetstyle($overlay,  $style_blue);
  211.         imagerectangle($overlay, $x1_postion,$y1_postion,($x1_postion+$bleed_img_w),($y1_postion+$bleed_img_h), IMG_COLOR_STYLED);
  212.         imagecopy($overlay,$trim_line, $x1_postion,$y1_postion, 0,0, 60, 15);
  213.        
  214.         imagesetstyle($overlay,  $style_red);
  215.         imagerectangle($overlay, $x1_postion+$bleed,$y1_postion+$bleed,($x1_postion+$bleed_img_w)-$bleed,($y1_postion+$bleed_img_h)-$bleed, IMG_COLOR_STYLED);
  216.         imagecopy($canvas,$safe_zone, $x1_postion+$bleed, $y1_postion+$bleed, 0, 0, 60, 15);
  217.        
  218.         //put the overlay on top of the canvas so that we have the uploaded image inside of the crop/bleed marks
  219.         imagecopy($canvas, $overlay, 0, 0, 0, 0, $w+1,$h+1);
  220.        
  221.         $out = &$canvas;
  222.       }
  223.      
  224.       //save the image to the predetermined file name
  225.       // Set the content type header - in this case image/jpeg
  226.      
  227.       imagejpeg($out, $crop_file,100);
  228.      
  229.       //free the memory taken up by all of these images
  230.       imagedestroy($safe_zone);
  231.       imagedestroy($trim_line);
  232.       imagedestroy($out);
  233.       imagedestroy($uploaded_preview);
  234.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement