Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function createThumbsAction() {
- $post_data = $this->getRequest()->getPost();
- $session = Mage::getSingleton('checkout/session');
- //with session -- start your request
- $quote_products = $session->getQuote()->getAllItems();
- $product_count = count($quote_products);
- $product = $quote_products[$post_data['serial']]->getProduct();
- $options_array = $product->getTypeInstance(true)->getOrderOptions($product);
- //get the paper size to calculate the crop/bleed image
- //$out .= html_entity_decode(Zend_Debug::dump($options_array, 'session',false));
- foreach ( $options_array["options"] as $options){
- if ($options['label'] == 'Size'){
- $size = html_entity_decode($options['value']);
- break;
- }
- }
- $size_array = explode('x',$size);
- foreach ($size_array as &$number){ //filter out the bad stuff
- $number = str_replace(' ','',$number);
- $number = str_replace('"','',$number);
- }
- $filename = $product->getId()."_".time(). str_replace(" ","_",$_FILES['file']['name']);
- $file_ext = strrchr($filename, '.');
- $file_ext = strtolower($file_ext);
- $file_base = substr($filename, 0, -(strlen($file_ext)));
- $target_path = getcwd() . '/skin/frontend/printlion/default/checkoutimage';
- $target_file = $target_path . '/' . $filename;
- $large_file = $target_path . '/large_' . $file_base . '.png';
- $thumb_file = $target_path . '/thumbnail_' . $file_base . '.jpg';
- $crop_file = $target_path . '/cropbleed_' . $file_base . '.png';
- //copy($tmpf,$target_file);
- move_uploaded_file($_FILES['file']['tmp_name'],$target_file);
- if ($file_ext == '.pdf'){
- $tmp_pdf = $target_path . '/' . $file_base . '_firstpage_tmp.pdf';
- $cmd = "pdftk {$target_file} cat 1-1 output {$tmp_pdf}";
- exec($cmd);
- $cmd = "convert {$tmp_pdf} {$large_file}";
- exec($cmd);
- unlink($tmp_pdf);
- } elseif ($file_ext == '.eps'){
- $cmd = "convert {$target_file} {$large_file}";
- exec($cmd);
- }else {
- die('bad format');
- }
- $this->createCropBleedImage($large_file, $crop_file, $size_array);
- $this->resizeImages($large_file, $thumb_file, $crop_file);
- $result = $this->getOnepage()->saveUploadDesign($file_base, $post_data);
- $this->getResponse()->setBody('');
- }
- public function resizeImages($large_file, $thumb_file, $crop_file){
- $im = imagecreatefrompng($large_file);
- $input_w = imagesx($im);
- imagedestroy($im);
- if($input_w > 600){
- $cmd = "convert -geometry 600 {$large_file} {$large_file}";
- exec($cmd);
- $cmd = "convert -geometry 600 {$crop_file} {$crop_file}";
- exec($cmd);
- }else {
- $cmd = "convert -quality 85 -geometry {$input_w} {$crop_file} {$crop_file}";
- exec($cmd);
- }
- $cmd = "convert -quality 80 -geometry 250 {$large_file} {$thumb_file}";
- exec($cmd);
- }
- public function createCropBleedImage($large_file, $crop_file, $size_array){
- // basing previews on the assumption that 72 dots per inch
- $print_w = $size_array[0] * 72;
- $print_h = $size_array[1] * 72;
- $bleed = 0.25 * 72; //pixels
- $bleed_img_h = ($print_h + (2*$bleed));
- $bleed_img_w = ($print_w + (2*$bleed));
- $uploaded_preview = imagecreatefrompng($large_file);
- $input_w = imagesx($uploaded_preview);
- $input_h = imagesy($uploaded_preview);
- $safe_zone = imagecreatefrompng('skin/frontend/printlion/default/checkoutimage/safe_zone_red.png');
- $trim_line = imagecreatefrompng('skin/frontend/printlion/default/checkoutimage/trim_line_blue.png');
- if (($input_w > $bleed_img_w) && ($input_h > $bleed_img_h)){
- //setup
- $canvas = imagecreatetruecolor($bleed_img_w+1, $bleed_img_h+1);
- $blue = imagecolorallocate($canvas, 0, 0, 255);
- $red = imagecolorallocate($canvas, 255, 0, 0);
- $bg = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
- $this->allow_alpha($canvas);
- imagesetthickness($canvas,3);
- $style_red = array($red, $red, $bg , $bg , $bg);
- $style_blue = array($blue, $blue, $bg, $bg, $bg);
- // create background transparent rectangle
- imagefilledrectangle($canvas, 0, 0, ($bleed_img_w+1), ($bleed_img_h+1), $bg);
- //create blue line and trim label
- imagesetstyle($canvas, $style_blue);
- imagerectangle($canvas, 0, 0, ($bleed_img_w), ($bleed_img_h), IMG_COLOR_STYLED);
- imagecopy($canvas,$trim_line, 0, 0, 0,0, 60, 15);
- //create red line and safe label
- imagesetstyle($canvas, $style_red);
- imagerectangle($canvas, $bleed, $bleed, ($bleed_img_w-$bleed), ($bleed_img_h-$bleed), IMG_COLOR_STYLED);
- imagecopy($canvas,$safe_zone, $bleed, $bleed, 0, 0, 60, 15);
- $overlay = imagecreatetruecolor($input_w,$input_h);
- $this->allow_alpha($overlay);
- $x1 = (($input_w/2)-($bleed_img_w/2));
- $y1 = (($input_h/2)-($bleed_img_h/2));
- $x2 = (($input_w/2)+($bleed_img_w/2));
- $y2 = (($input_h/2)+($bleed_img_h/2));
- $bg = imagecolorallocatealpha($overlay, 0, 0, 0, 64);
- $transparent = imagecolorallocatealpha($overlay, 0, 0, 0, 127);
- //$bg
- imagefilledrectangle($overlay, 0,0, $input_w, $input_h,$transparent);
- imagefilledrectangle($overlay, 0,0, $x1, $input_h,$bg);
- imagefilledrectangle($overlay, $x1,0, $input_w, $y1,$bg);
- imagefilledrectangle($overlay, $x1,$y2, $input_w, $input_h,$bg);
- imagefilledrectangle($overlay, $x2,$y1, $input_w, $input_h,$bg);
- imagecopy($overlay, $canvas, $x1, $y1, 0, 0, $bleed_img_w+1, $bleed_img_h+1);
- imagecopy($uploaded_preview, $overlay, 0, 0, 0, 0, $input_w, $input_h);
- imagedestroy($canvas);
- imagedestroy($overlay);
- $out = &$uploaded_preview;
- } else {
- $w = $bleed_img_w;
- $h = $bleed_img_h;
- if ($input_h > $bleed_img_h){
- $h = $input_h;
- }
- if ($input_w > $bleed_img_w){
- $w = $input_w;
- }
- //create blank canvas large enough in which we will center the uploaded image
- $canvas = imagecreatetruecolor($w+1, $h+1);
- $bg = imagecolorallocate($canvas, 255, 255, 255);
- imagefilledrectangle($canvas, 0, 0, $w+1, $h+1, $bg); // create background
- imagecopy($canvas, $uploaded_preview, (($w/2)-($input_w/2)), (($h/2)-($input_h/2)), 0, 0, $input_w, $input_h);
- //create the opacaque overlay inwhich we draw some darkness and the bleed/cut lines
- $overlay = imagecreatetruecolor($w+1,$h+1);
- $this->allow_alpha($overlay);
- $bg = imagecolorallocatealpha($overlay, 0, 0, 0, 64);
- $transparent = imagecolorallocatealpha($overlay, 0, 0, 0, 127);
- $blue = imagecolorallocate($overlay, 0, 0, 255);
- $red = imagecolorallocate($overlay, 255, 0, 0);
- $style_red = array($red, $red, $transparent , $transparent , $transparent);
- $style_blue = array($blue, $blue, $transparent, $transparent, $transparent);
- //$bg
- $x1_postion = ($w-$bleed_img_w)/2;
- $y1_postion = ($h-$bleed_img_h)/2;
- //color in the darkness surrounding the crop bleed marks
- imagefilledrectangle($overlay, 0,0, $w, $h,$transparent);
- imagefilledrectangle($overlay, 0,0, $w, $y1_postion,$bg);
- imagefilledrectangle($overlay, 0,($y1_postion+$bleed_img_h), $w, $h,$bg);
- imagefilledrectangle($overlay, 0,$y1_postion, $x1_postion, ($y1_postion+$bleed_img_h),$bg);
- imagefilledrectangle($overlay, ($x1_postion+$bleed_img_w),0, $w, $h,$bg);
- imagesetthickness($overlay,3);
- //draw the lines
- imagesetstyle($overlay, $style_blue);
- imagerectangle($overlay, $x1_postion,$y1_postion,($x1_postion+$bleed_img_w),($y1_postion+$bleed_img_h), IMG_COLOR_STYLED);
- imagecopy($overlay,$trim_line, $x1_postion,$y1_postion, 0,0, 60, 15);
- imagesetstyle($overlay, $style_red);
- imagerectangle($overlay, $x1_postion+$bleed,$y1_postion+$bleed,($x1_postion+$bleed_img_w)-$bleed,($y1_postion+$bleed_img_h)-$bleed, IMG_COLOR_STYLED);
- imagecopy($canvas,$safe_zone, $x1_postion+$bleed, $y1_postion+$bleed, 0, 0, 60, 15);
- //put the overlay on top of the canvas so that we have the uploaded image inside of the crop/bleed marks
- imagecopy($canvas, $overlay, 0, 0, 0, 0, $w+1,$h+1);
- $out = &$canvas;
- }
- //save the image to the predetermined file name
- // Set the content type header - in this case image/jpeg
- imagejpeg($out, $crop_file,100);
- //free the memory taken up by all of these images
- imagedestroy($safe_zone);
- imagedestroy($trim_line);
- imagedestroy($out);
- imagedestroy($uploaded_preview);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement